Saturday, 22 June 2019

Some tips and tricks to start modeling with R for quantitative and empirical finance

chartSeries(ClCl(MSFT))

https://www.mathclasstutor.online


Hit <Return> to see next plot:
> MSFT.ret <- diff(log(Ad(MSFT)))
> SNP.ret <- diff(log(Ad(SNP)))
> dataDaily <- na.omit(merge(SNP.ret,MSFT.ret,DTB3.sub), join='inner')
getSymbols('DTB6', src='FRED')
[1] "DTB6"
> plot(DTB3.sub)

Error in plot(DTB3.sub) : object 'DTB3.sub' not found
> DTB3.sub = DTB3['1984-01-02/2014-03-31']
> DTB6.sub = DTB6['1984-01-02/2014-03-31']
> plot(DTB3.sub)
Hit <Return> to see next plot:
> lines(DTB6.sub, col='red')
Hit <Return> to see next plot:
> DTB3.sub = DTB3['1984-01-02/2019-06-21']
> DTB6.sub = DTB6['1984-01-02/2019-06-21']
> plot(DTB3.sub)
Hit <Return> to see next plot:
> lines(DTB6.sub, col='red')
Hit <Return> to see next plot:
> x1=as.numeric(na.omit(DTB3.sub))
> x2=as.numeric(na.omit(DTB6.sub))
> y = cbind(x1,x2)
> cregr <- lm(x1 ~ x2)
> r = cregr$residuals
> library('tseries')
yJoTest = ca.jo(y, type = c("trace"), ecdet = c("none"), K = 2)
> Eigenvalues (lambda)
Error in Eigenvalues(lambda) : could not find function "Eigenvalues"
> getSymbols("SNP", from="2014-01-01", to=Sys.Date())
[1] "SNP"
> chartSeries(Cl(SNP))
Hit <Return> to see next plot:
> ret <- dailyReturn(Cl(SNP), type='log')
> chartSeries(ret)
Hit <Return> to see next plot:
> par(mfrow=c(2,2))
> acf(ret, main="Return ACF");
Hit <Return> to see next plot:
> pacf(ret, main="Return PACF");
> acf(ret^2, main="Squared return ACF");
> pacf(ret^2, main="Squared return PACF")
> par(mfrow=c(1,1))
> m=mean(ret);s=sd(ret);
> par(mfrow=c(1,2))
> hist(ret, nclass=40, freq=FALSE, main='Return histogram');curve(dnorm(x, mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
Hit <Return> to see next plot:
> plot(density(ret), main='Return empirical distribution');curve(dnorm(x,mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> par(mfrow=c(1,1))
> kurtosis(ret)
[1] 1.890591
attr(,"method")
[1] "excess"
> plot(density(ret), main='Return EDF - upper tail', xlim = c(0.1, 0.2),
+      ylim=c(0,2));
Hit <Return> to see next plot:
> curve(dnorm(x, mean=m,sd=s), from = -0.3, to = 0.2, add=TRUE, col="red")
> plot(density(ret), xlim=c(-5*s,5*s),log='y', main='Density on log-scale')
Hit <Return> to see next plot:
> curve(dnorm(x, mean=m,sd=s), from=-5*s, to=5*s, log="y", add=TRUE,
+       col="red")
> qqnorm(ret);qqline(ret);
Hit <Return> to see next plot:
> library('rugarch')
Loading required package: parallel

Attaching package: ‘rugarch’

The following object is masked from ‘package:stats’:

    sigma

Warning message:
package ‘rugarch’ was built under R version 3.5.2
> getSymbols("AAPL", from="2006-01-01", to="2019-06-21")
[1] "AAPL"
> ret.aapl <- dailyReturn(Cl(AAPL), type='log')
> chartSeries(ret.aapl)
Hit <Return> to see next plot:
> garch11.spec = ugarchspec(variance.model = list(model="sGARCH",garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)))
> aapl.garch11.fit = ugarchfit(spec=garch11.spec, data=ret.aapl)
> coef(aapl.garch11.fit)
          mu        omega       alpha1
1.702899e-03 1.139891e-05 9.261603e-02
       beta1
8.796936e-01
> coef(msft.garch11.fit)
Error in coef(msft.garch11.fit) : object 'msft.garch11.fit' not found
> ni.garch11 <- newsimpact(aapl.garch11.fit)
> plot(ni.garch11$zx, ni.garch11$zy, type="l", lwd=2, col="blue",
+      main="GARCH(1,1) - News Impact", ylab=ni.garch11$yexpr, xlab=ni.
+      garch11$xexpr)
Error: unexpected symbol in:
"     main="GARCH(1,1) - News Impact", ylab=ni.garch11$yexpr, xlab=ni.
     garch11"
> plot(ni.garch11$zx, ni.garch11$zy, type="l", lwd=2, col="blue",main="GARCH(1,1) - News Impact", ylab=ni.garch11$yexpr, xlab=ni.garch11$xexpr)
Hit <Return> to see next plot:
> egarch11.spec = ugarchspec(variance.model = list(model="eGARCH",garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)))
> aapl.egarch11.fit = ugarchfit(spec=egarch11.spec, data=ret.aapl)
> coef(aapl.egarch11.fit)
          mu        omega       alpha1
 0.001289495 -0.289748223 -0.097118457
       beta1       gamma1
 0.963128034  0.159079971
> ni.egarch11 <- newsimpact(aapl.egarch11.fit)
> plot(ni.egarch11$zx, ni.egarch11$zy, type="l", lwd=2, col="blue",
+      main="EGARCH(1,1) - News Impact",
+      ylab=ni.egarch11$yexpr, xlab=ni.egarch11$xexpr)
Hit <Return> to see next plot:
> tgarch11.spec = ugarchspec(variance.model = list(model="fGARCH",submodel="TGARCH", garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)))
> aapl.tgarch11.fit = ugarchfit(spec=tgarch11.spec, data=ret.aapl)
> coef(aapl.egarch11.fit)
          mu        omega       alpha1
 0.001289495 -0.289748223 -0.097118457
       beta1       gamma1
 0.963128034  0.159079971
> ni.tgarch11 <- newsimpact(aapl.tgarch11.fit)
> plot(ni.tgarch11$zx, ni.tgarch11$zy, type="l", lwd=2, col="blue",
+      main="TGARCH(1,1) - News Impact",
+      ylab=ni.tgarch11$yexpr, xlab=ni.tgarch11$xexpr)
Hit <Return> to see next plot:
> garch11.spec = ugarchspec(variance.model = list(garchOrder=c(1,1)), mean.model = list(armaOrder=c(0,0)),fixed.pars=list(mu = 0, omega=0.1, alpha1=0.1,beta1 = 0.7))
> garch11.sim = ugarchpath(garch11.spec, n.sim=1000)
aapl.garch11.fit = ugarchfit(spec=garch11.spec, data=ret.aapl, out.sample=20)
Warning message:
In .sgarchfit(spec = spec, data = data, out.sample = out.sample,  :
ugarchfit-->warning: all parameters fixed...returning ugarchfilter object instead

> plot(aapl.garch11.fcst, which='all')
Error in plot(aapl.garch11.fcst, which = "all") :
  object 'aapl.garch11.fcst' not found
> library("rugarch", lib.loc="~/R/win-library/3.5")
> library(rugarch)
> garch11.spec = ugarchspec(variance.model = list(garchOrder=c(1,1)),mean.model = list(armaOrder=c(0,0)),fixed.pars=list(mu = 0, omega=0.1, alpha1=0.1,beta1 = 0.7))
> garch11.sim = ugarchpath(garch11.spec, n.sim=1000)

No comments:

Post a comment

Intersection of subring

How to prove the intersection of two subrings is a subring? Let S1 and S2  be two subrings of a ring R. Then S1∩S2 is not empty since ...