Monday, 21 January 2019

Black-Scholes model in R

stock=10858.7
rf=0.0743
strike=stock-100
sigma=0.1281
TTM=0.0039683
d1<-(log(stock/strike)+(rf+0.5*sigma^2)*TTM)/(sigma*sqrt(TTM))
d2<-d1-(sigma*sqrt(TTM))
BS.call<-stock*pnorm(d1,mean=0,sd=1)-strike*exp(-rf*TTM)*pnorm(d2,mean=0,sd=1)
BS.call
BS.put<-BS.call-stock+strike*exp(-rf*TTM)
BS.put
for(TTM<0.08) 
  {
  TTM<-(TTM*2)
  }
d1<-(log(stock/strike)+(rf+0.5*sigma^2)*TTM)/(sigma*sqrt(TTM))
d2<-d1-(sigma*sqrt(TTM))
BS.call<-stock*pnorm(d1,mean=0,sd=1)-strike*exp(-rf*TTM)*pnorm(d2,mean=0,sd=1)
BS.call
BS.put<-BS.call-stock+strike*exp(-rf*TTM)
BS.put

MC simulation of Black Scholes in R


stock=398.79
sigma=0.32
strike=300
TTM=2.5
rf=0.01
num.sim<-100000
R<-(rf-0.5*sigma^2)*TTM
R
SD<-sigma*sqrt(TTM)
SD
TTM.price<-stock*exp(R+SD*rnorm(num.sim,0,1))
TTM.call<-pmax(0,TTM.price-strike)
PV.call<-TTM.call*(exp(-rf*TTM))
mean(PV.call)
TTM.put<-pmax(0,strike-TTM.price)
PV.put<-TTM.put*(exp(-rf*TTM))
mean(PV.put)
#Let's calculate Black-Scholes value fr call and put option#
d1<-(log(stock/strike)+(rf+0.5*sigma^2)*TTM)/(sigma*sqrt(TTM))
d2<-d1-(sigma*sqrt(TTM))
BS.call<-stock*pnorm(d1,mean=0,sd=1)-strike*exp(-rf*TTM)*pnorm(d2,mean=0,sd=1)
BS.call
BS.put<-BS.call-stock+strike*exp(-rf*TTM)
BS.put
 #”ctrl+shft+enter” to process#

Saturday, 19 January 2019

SPY versus BSM histogram plot in R

library("quantmod")
getSymbols("SPY")
#Stats of SPY
spy = Delt(as.numeric(SPY$SPY.Adjusted), k=20)
spy.mu= mean(spy, na.rm=TRUE)
spy.sd= sd(spy, na.rm=TRUE)
#Stats of SPY applied to Norm dist
norm = rnorm(1000, mean=spy.mu, sd=spy.sd)
#Visualize histograms
hist(spy, main="SPY versus Normal Distribution", breaks=50, col="blue", freq = FALSE, probability=TRUE)
hist(norm, col="yellow", breaks=50, add=T, freq = FALSE, probability=TRUE)

Thursday, 17 January 2019

Plotting data from 2 columns in R

***load the excel/txt file from R-Studio manually using import option**

plot(d5[c(9,6)])

//d5 is name of the file, 'c' implies column, 9 and 6 are column nos to be plotted//

Friday, 18 May 2018

MATLAB plot of data (spectro) with maximum indicator

load llspectro6.txt
x = llspectro6 (:,1);
y = llspectro6 (:,2);
[Peak, PeakIdx] = findpeaks(y);
plot(x,y, 'cyan', 'Linewidth', 2)
ylabel ('Intensity (Counts)')
xlabel ('Wavelength (nm)')
indexmin = find(min(y) == y);
xmin = x(indexmin);
ymin = y(indexmin);
indexmax = find(max(y) == y);
xmax = x(indexmax);
ymax = y(indexmax);
strmax = ['Maximum = ',num2str(xmax)];
text(xmax,ymax,strmax,'HorizontalAlignment','right');

Friday, 12 January 2018

MATLAB data plotting: x vs y plot and x vs y1,y2,y3 etc. plot in single frame

load plot2.txt
x = plot2 (:,1);
y = plot2 (:,2);
plot(x,y, 'r', 'Linewidth', 2)
title('Magnetization plot of 5x5')
ylabel ('Absolute value of Magnetization |M|')
xlabel ('Temperatuire (T)')


load plot2.txt
load plot2.txt
x = plot2 (:,1);
y1 = plot2 (:,2);
y2 = plot2 (:,3);
y3 = plot2 (:,4);
y4 = plot2 (:,5);
plot(x, y1, 'r', x, y2, 'm', x, y3, 'g', x, y4, 'b', 'Linewidth', 2)
title('Specific heat plot of of four lattice sizes')
ylabel ('Specific heat (Cv)')

xlabel ('Temperatuire (T)')