• Home
  • Get Started
  • About
  • Services
    • Research Software
    • Research Datasets
    • Compute
    • Storage
  • Knowledge Base
  • FAQ
  • Resources
  • Contact Us
  • Home
  • Get Started
  • About
  • Services
    • Research Software
    • Research Datasets
    • Compute
    • Storage
  • Knowledge Base
  • FAQ
  • Resources
  • Contact Us
home/Knowledge Base/Slurm Batch Jobs/MATLAB Black-Scholes Tutorial

MATLAB Black-Scholes Tutorial

Run getSlurmExamples at the command line of rnd or vleda to have all the tutorials copied to your home directory.

Run a MATLAB program that calculates Black-Scholes Merton call and put values and outputs it to a file. Vary the volatility by passing in TASK_ID as an environment variable in SLURM.

MATLAB Script

% blkscholes.m
%
% Compute the call and put prices of a European stock
% on a non-dividend paying stock using Black_Scholes
%
% Suppose the stock price 3 months from the expiration of an option is $100,
% the exercise price is $95, the risk-free interest rate is 10% per annum,
% and we want to compute the call and put prices for volatilities
% specified at run time.
%
% Use the task id to vary the value of volatility
%
% task_id = getenv('SGE_TASK_ID')
  task_id = getenv('SLURM_ARRAY_TASK_ID')

  volat = str2num(task_id)

% task id's must be specified as whole numbers; convert task
% id number to decimal
  volat = volat/100;

% set the parameter values
  price = 100.0;
  strike = 95.0;
  rate = 0.1;
  time = 0.25;

% compute the call and put prices
  [call, put] = blsprice(price, strike, rate, time, volat)

% create an output file name for the results
% use the task ID in the file name to uniquely
% identify the results; blkscholes_#.out
  outfile = strcat('blkscholes_', task_id, '.out');

% write the results to the file
  fileID = fopen(outfile,'w');
  fprintf(fileID,'%-8s %-8s\n','call','put');
  fprintf(fileID,'%6.2f %6.2f\n', call, put);
  fclose(fileID);

task_id variable fetches the environment variable value passed in Slurm script.

Slurm Script

#!/bin/bash -xv
# [ blkscholes.sbatch ]
#SBATCH --job-name=blkscholes
#SBATCH --output=blkscholes_%a.out
#SBATCH --array=30-40:2
#SBATCH --export=ALL
#SBATCH --time=00:10:00
#SBATCH --mem=512m
#SBATCH --mail-type=END,FAIL
#SBATCH --partition=test
#
# This script runs a MATLAB program saving the output into a files called:
# blkscholes_#.out, where the # represents the taskID.
#
# The '--array=30-40:2' directive specifies a set of tasks.  Each task is a separate run
# of the  blkscholes.m  program each using a different  TASK_ID  which the program
# can access.  The specification  '30-40:2'  defines a set of tasks having
# TASK_IDs = (30,32,34,36,38, and 40).  The '--array='  parameter creates the
# environment variable:
#
#     SLURM_ARRAY_TASK_ID
#
# set equal to the current TASK_ID number.  In this case the first TASK_ID=30 and the 
# entire set of TASK_IDs is: 30,32,34,36,38,40.
#
# Submit blkscholes.sbatch to the grid with command:
#          sbatch blkscholes.sbatch
#
#
# Specify the software and version to use, and run the MATLAB program:

  module  purge
  module  load    matlab/2019a

  matlab -nodisplay -nojvm < blkscholes.m

You can run the Slurm script by typing in the following command:

sbatch blkscholes.sbatch
Related Articles
  • Creating a Python Virtual Environment
  • Stata Tutorial
  • R Monte Carlo Simulation Tutorial
  • R Fitspline Tutorial
  • Common SLURM commands
  • MATLAB GPU Tutorial

MATLAB GPU Tutorial  

  • Home
  • Get Started
  • About
  • Services
  • Knowledge Base
  • FAQ
  • Resources
  • Contact Us
  • Copyright © 2023 Stern Center for Research Computing