Example 47 x n500 plotx which is ar and which is ma

Info icon This preview shows pages 6–10. Sign up to view the full content.

View Full Document Right Arrow Icon
#Example 4.7 x = arima.sim(list(order=c(0,1,0)), n=500) plot(x) #Which is AR and which is MA #Example 4.8 ar1 = arima.sim(list(order=c(0,0,1), ma=.9), n=500) ma1 = arima.sim(list(order=c(1,0,0), ar=.9), n=500) plot(ar1, col='red/green') lines(ma1, col='red/green') #Which is which #Example 4.9 arima1.1.0 = arima.sim(list(order=c(1,1,0), ar=.9), n=500) arima0.0.1 = arima.sim(list(order=c(0,0,1), ma=.9), n=500) plot(arima1.1.0 , col='red/green') lines(arima0.0.1 , col='red/green') #Spot the difference #Example 4.10 madiff=diff(ma0.1.1) ardiff=diff(ar1.1.0) HK0001=getSymbols("0001.HK", auto.assign=FALSE) l=length(HK0001[,4]) hk=HK0001[,4][c(1096:1596)] plot(diff(hk))
Image of page 6

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

View Full Document Right Arrow Icon
#Garch(1,1) Price Simulation #Example 4.11 T=500 #process parameters eta = 0.2 #eta = 0 is equivalent to Geometric Brownian Motion mu = 100 #the mean of the process #GARCH volatility model specs = garchSpec(model = list(omega = 0.000001, alpha = 0.5, beta = 0.4)) sigma = garchSim(spec = specs, n = T) P_0 = mu #starting price, known P = rep(P_0,T) for(i in 2:T) { P[i] = P[i-1] + eta * (mu - P[i-1]) + sigma[i] * P[i-1] } plot(P, type="l",xlab="Time", main="", ylab = "Price") #Forecast Volatilty #Example 4.12 predictVolatility =function(yahooStockCode) { stock=getSymbols(yahooStockCode, auto.assign=FALSE) lgstk <- diff(log(stock[,4])) lgs=lgstk[-1] fit = garchFit(~garch(1,1), data=lgs) predict(fit, n.ahead = 5, plot=T) } #UNIT 5 ############################################################################## #Test the 3-Soldier Candlestick Pattern #Example 5.1 a=getSymbols(x, auto.assign=F) cond1=with(a, ifelse( a[,4]>a[,1] & lag(a[,4],1)>lag(a[,1],1) & lag(a[,4],2)>lag(a[,1],2) , 1, -1) ) cond2=with(a, ifelse( a[,4]> lag(a[,4],1) & lag(a[,4],1)>lag(a[,4],2) , 1, -1) ) cond3=with(a, ifelse( lag(a[,4],2)> lag(a[,1],1) & lag(a[,1],1)>lag(a[,1],2) , 1, -1) ) cond4=with(a, ifelse( lag(a[,4],1)> lag(a[,1],0) & lag(a[,1],0)>lag(a[,1],1) , 1, -1) ) b=merge(cond1, cond2, cond3,cond4) pattern=with(b, ifelse(b[,1]==1 & b[,2]==1 & b[,3]==1 & b[,4]==1, 111, -1)) #111 means FOUND while -1 NOT FOUND sma3=SMA(a[,4] ,3) t=merge(a, sma3) singleDayTrend=with(t, ifelse( t[,1] > lag(t[,1],1), 1, -1)) cumulativeSum=function(vx,n) { with(vx, ifelse(vx == lag(vx,n) &
Image of page 7
(n<20), vx+cumulativeSum(vx,n+1), vx)) } trend=cumulativeSum(singleDayTrend[,1], 1) observedResult= lag( sma3 , -3)- a[,4] k=merge(trend, pattern, observedResult) names(k)[1]=paste("trend") names(k)[2]=paste("pattern") names(k)[3]=paste("result") subset(k, k[,2]==111) #UNIT 6 ############################################################################## #Markowitz Portfolio Theory #calculate the covariance matrix #Example 6.1 a=getSymbols("T", from="2012-03-01", auto.assign=F) #ATT b=getSymbols("GM",from="2012-03-01", auto.assign=F) #GMC c=getSymbols("X", from="2012-03-01", auto.assign=F) #us Steel (USX) a=monthlyReturn(a) b=monthlyReturn(b) c=monthlyReturn(c) mean.a=mean(a); mean.b=mean(b); mean.c=mean(c) sd.a=sd(a); sd.b= sd(b); sd.c= sd(c) var.a= sd.a^2 ; var.b= sd.b^2 ; var.c= sd.c^2 #get covariance matrix a.minus.mu=a - mean.a b.minus.mu=b - mean.b c.minus.mu=c - mean.c a.x.b = a.minus.mu*b.minus.mu b.x.c = b.minus.mu*c.minus.mu c.x.a = c.minus.mu*a.minus.mu covar.a.x.b = sum(a.x.b)/(nrow(a.x.b)-1) covar.b.x.c = sum(b.x.c)/(nrow(b.x.c)-1) covar.c.x.a = sum(c.x.a)/(nrow(c.x.a)-1) covarM = matrix( c( var.a , covar.a.x.b , covar.c.x.a, covar.a.x.b, var.b , covar.b.x.c, covar.c.x.a, covar.b.x.c , var.c ) , nrow=3, ncol=3, byrow=T) # Another Method x=NULL x=cbind(x, a) x=cbind(x, b) x=cbind(x, c) covarM=cov(x) #Calculate the return and variance of a multi asset portfolio #Example 6.2 #
Image of page 8

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

View Full Document Right Arrow Icon
# MeanReturns, covarM and porfolio allocation are given for four stocks A, B, C, D # # Asset A , Asset B , Asset c, Asset D MeanReturns=c(0.177416667, 0.216166667, 0.19625, 0.05 ) covarM= matrix( c( 0.0412268, 0.0226614 , 0.0305563, 3.502E-35, 0.0226614, 0.047918 , 0.015001, -2.45e-34 , 0.0305628, 0.015001 , 0.090306, -1.4e-34 , 3.50169E-35,-2.45118E-34,-1.40068E-34,5.25254E-35), nrow=4, ncol=4, byrow=T) Allocation=c(0.136, 0.392, 0.120, 0.352) # We can calculate the porfolio mean and variance as below # PorfolioMean=sum(MeanReturns* Allocation) >PorfolioMean [1] 0.150016
Image of page 9
Image of page 10
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