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.
# [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()
#!/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.
# -----------------------------------------------------------------------------
#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=netID@stern.nyu.edu # email TO
#SBATCH --output=helloJob%j.out # write output; "%j" = job ID
#SBATCH --partition=test # test partition max time 1hr
module purge
module load R/4.6.1
R CMD BATCH stock-price.R
To execute this script, run the following command:
sbatch stock-price.sbatch