In this tutorial, we will run an R script. The R script runs a Monte Carlo simulation to estimate the path of a stock price using the Geometric Brownian stochastic process.
R Script
# [stock-price.R]
GeometricBrownian<-function()
{
paths<-10
count<-5000
interval<-5/count
mean<-0.06
sigma<-0.3
sample<-matrix(0,nrow=(count+1),ncol=paths)
for(i in 1:paths)
{
sample[1,i]<-100
for(j in 2:(count+1))
{
#Expression for Geometric Brownian Motion
sample[j,i]<-sample[j-1,i]*exp(interval*(mean-((sigma)^2)/2)+((interval)^.5)*rnorm(1)*sigma)
}
}
cat("E[W(2)] = ",mean(sample[2001,]),"\n")
cat("E[W(5)] = ",mean(sample[5001,]),"\n")
#pdf <- paste("result_", tid, ".pdf", sep = "")
#pdf(file="out.pdf")
hist(sample)
matplot(sample,main="Geometric Brownian",xlab="Time",ylab="Path",type="l")
dev.off()
}
GeometricBrownian()
SLURM Script
#!/bin/bash
#
# [ stock-price.sbatch ]
#
# This slurm submission script runs a Monte Carlo simulation to
# estimate the path of a stock price using the geometric
# Brownian motion stochastic process.
#
# -----------------------------------------------------------------------------
#
# slurm job scheduler directives begin with "#SBATCH"
#
#SBATCH --job-name=spJob # Job name
#SBATCH --time=00:10:00 # Wall-clock time limit for this job
#SBATCH --mem=4G # Request 4G RAM
#SBATCH --mail-type=END,FAIL # email user when job ENDs or FAILs
#SBATCH --mail-user=you@stern.nyu.edu # email TO
#SBATCH --output=helloJob%j.out # write output; "%j" = job ID
#SBATCH --partition=test # test partition max time 1hr
#
#
# **** Note: It is always advantages to specify conservative values
# for run time and memory. Jobs requesting fewer resources are more
# likely to be scheduled before jobs requesting more resources.
#
# --------------------
# How to submit job to Stern grid
#
# sbatch --array=5-15:5 fitspline.embedded-sbatch.sh
# --------------------------------------------------------------------
# Select stat package and version to use
module purge
module load R/4.0.2
# R CMD BATCH --slave fitspline.R fitspline.$SLURM_JOBID.Rout
# R CMD BATCH --slave fitspline.R fitspline.$SLURM_TASK_PID.Rout
R CMD BATCH stock-price.R
To execute this script, run the following command:
sbatch stock-price.sbatch