Porfoliovariance sumcovarm porfoliovariance 1

Info icon This preview shows pages 9–13. Sign up to view the full content.

View Full Document Right Arrow Icon
PorfolioVariance = sum((covarM %*% Allocation)*Allocation) > PorfolioVariance [1] 0.01425122 #UNIT 7 ############################################################################## #Binomial Option Pricing Model #Call Example 7.1 genPriceLattice <- function(S=100, up=0.25, down=-0.1, N=1) { u=1+up d=1+down X <- c() X[1] <- S count <- 2 for (i in 1:N) { for (j in 0:i) { X[count] <- S * d^j * u^(i-j) count <- count + 1 } } count=1 M=matrix(0, rep( (N+1)*(N+1)), nrow=(N+1), ncol=(N+1)) for (i in 1:(N+1)) { for (j in 1:(N+1)) { if (j<=i) { M[j,i]=X[count] count=count+1 } } } return (M) } # genPriceLattice(S=40, up=+0.25, down=-0.1, N=1) # genPriceLattice(S=44.44, up=+0.25, down=-0.1, N=1) # genPriceLattice(S=60, u=0.1, d=-0.05, N=2) # r is interest rate getOnePeriodOptionsPrice=function(S_t0, K, r, up, down)
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
{ u=1+up d=1+down S_t1u=S_t0 * u S_t1d=S_t0 * d C_t1u=max(0, S_t1u - K) C_t1d=max(0, S_t1d - K) C_t1=matrix(c(C_t1u, C_t1d), nrow=2, ncol=1) M=matrix( c( (u*S_t0), (1+r), (d*S_t0), (1+r)), nrow=2, ncol=2, byrow=T) coeff=solve(M, C_t1) return (coeff[1,1]*S_t0+coeff[2,1]) } # getOnePeriodOptionsPrice(40, 45, 0.05, 0.25 ,-0.1) getOnePeriodPreviousOptionsPrice=function(S_t1u, S_t1d, C_t1u,C_t1d, r, up, down) { u=1+up d=1+down S_t0=S_t1u/u #be sure that S_t0 is also S_t1d/d #S_t1u=S_t0 * u #S_t1d=S_t0 * d #C_t1u=max(0, S_t1u - K) #C_t1d=max(0, S_t1d - K) C_t1=matrix(c(C_t1u, C_t1d), nrow=2, ncol=1) M=matrix( c( (S_t1u), (1+r), (S_t1d), (1+r)), nrow=2, ncol=2, byrow=T) coeff=solve(M, C_t1) return (coeff[1,1]*S_t0+coeff[2,1]) } # getOnePeriodPreviousOptionsPrice(50,36,5,0, 0.05, 0.25 ,-0.1) genOptionsPriceLattice = function(Sx=40, Kx=45, rx=0.05, upx=0.25, downx=-0.1, Nx=1) { P=genPriceLattice(Sx, upx, downx, Nx) M=matrix(0, rep( (Nx+1)*(Nx+1)), nrow=(Nx+1), ncol=(Nx+1)) for (i in 1:(Nx)) { ri=(Nx-i+1) for (j in 1:(Nx)) { rj=(Nx-j+1) if (ri<Nx & i<=j ) { M[rj,ri]= getOnePeriodPreviousOptionsPrice(P[rj,ri+1], P[rj+1,ri+1], M[rj,ri+1] ,M[rj+1,ri+1], rx, upx ,downx) } if (ri==Nx) { Ct1u =max(0, P[rj,ri+1]-K) M[rj,ri+1]=Ct1u Ct1d =max(0, P[rj+1,ri+1]-K) M[rj+1,ri+1]=Ct1d cat(P[rj,ri+1], P[rj+1,ri+1], Ct1u ,Ct1d, rx, upx ,downx,"\n") M[rj,ri]= getOnePeriodPreviousOptionsPrice(P[rj,ri+1], P[rj+1,ri+1], Ct1u ,Ct1d, rx, upx ,downx) } } }
Image of page 10
return (M) } # genOptionsPriceLattice(40, 45, 0.05, 0.25 ,-0.1, 1) # genOptionsPriceLattice(44.44,45, 0.05, 0.25 ,-0.1,2) #UNIT 9 ############################################################################## #Pair Trading by Co-integration #Example 9.1 install.packages("PairTrading") library(PairTrading) data(stock.price) price.pair=stock.price[,1:2]["2008-12-31::"] reg=EstimateParameters(price.pair, method=lm) plot(reg$spread, main="Estimate Spread") #Test integrated of order 1 PP.test(as.numeric(reg$spread)) params=EstimateParametersHistorically(price.pair, period=180) signal=Simple(params$spread, 0.05) barplot(signal, col="green", space=0, border="green", xaxt="n", yaxt="n", xlab="", ylab="") par(new=TRUE) plot(params$spread, main="Spread & Signal") #back test return.pairtrading=Return(price.pair, lag(signal), lag(params$hedge.ratio)) plot(100*cumprod(1+return.pairtrading), main="Performance of pair trading") #UNIT 10 ############################################################################# #One-output XOR #Example 10.1 x1=ts(c(1,1,0,0)) x2=ts(c(1,0,1,0)) d1=ts(c(0,1,1,0)) x=cbind(d1, x1, x2) require ("neuralnet") print(net.out <- neuralnet(d1~x1+x2, data=x, hidden=10, threshold=0.01)) plot(net.out) x1=ts(c(0.9,0.8,0.3,0.2)) x2=ts(c(0.76,0.4,0.7,0.22)) test=cbind( x1, x2) cbind(test, compute(net.out, test)$net.result) #NEURAL NET RESULT #Two-output XOR #Example 10.2 x1=ts(c(1,1,0,0)) x2=ts(c(1,0,1,0)) d1=ts(c(0,1,1,0)) d2=ts(c(1,0,0,1))
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
x=cbind(d1, d2, x1, x2) require ("neuralnet") net.out <- neuralnet(d1+d2~x1+x2, data=x, hidden=10, threshold=0.01) x1=ts(c(0.9,0.8,0.3,0.2)) x2=ts(c(0.76,0.4,0.7,0.22)) test=cbind( x1, x2) cbind(test, compute(net.out, test)$net.result) #NEURAL NET RESULT
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern