Data frames a very much like spreadsheets or tables,
but they are also a lot like databases. Some sort of happy medium. If you want to join two dataframes, it is the same a joining two databases.[1] "GOOGL" "AAPL" "CSCO"
> csco = as.matrix (CSCO [ , 6 ] ),
> ibm = as.ma trix ( IBM [ , 6 ] )
> aapl = as.matrix(AAPL [ , 6 ] )
> csco = as.matrix (CSCO [ , 6 ] )
> google =as.ma trix (GOOGL [ , 6 ] )
> google =as.matrix (GOOGL [ , 6 ] )
> stkdata = cbind ( google , aapl , csco )
> dim( stkdata )
[1] 3307 3
> n = length ( stkdata [ , 1 ] )
> n
[1] 3307
> n = length (stkdata [ , 1 ] )
> rets = log(stkdata [ 2 : n , ] / stkdata[ 1 :( n−1 ),] )
> colMeans ( rets )
GOOGL.Adjusted AAPL.Adjusted CSCO.Adjusted
0.0005585866 0.0010300534 0.0002325053
> cv = cov ( rets )
> print ( cv , 2 )
GOOGL.Adjusted AAPL.Adjusted CSCO.Adjusted
GOOGL.Adjusted 0.00032 0.00019 0.00016
AAPL.Adjusted 0.00019 0.00039 0.00018
CSCO.Adjusted 0.00016 0.00018 0.00033
>
> cr = cor(rets )
>
> print (cr,4 )
GOOGL.Adjusted AAPL.Adjusted CSCO.Adjusted
GOOGL.Adjusted 1.0000 0.5451 0.5030
AAPL.Adjusted 0.5451 1.0000 0.4929
CSCO.Adjusted 0.5030 0.4929 1.0000
>
> x = matrix(rnorm ( 12 ) , 4 , 3 )
> x
[,1] [,2] [,3]
[1,] 0.88410486 1.176699 1.3999856
[2,] 0.09300388 0.676594 -0.7676680
[3,] 0.51237100 1.673757 -0.1873588
[4,] -2.12444302 -1.384905 -0.4829591
> print ( t(x) , 3 )
[,1] [,2] [,3] [,4]
[1,] 0.884 0.093 0.512 -2.124
[2,] 1.177 0.677 1.674 -1.385
[3,] 1.400 -0.768 -0.187 -0.483
> print (t(x)%*%x , 3 )
[,1] [,2] [,3]
[1,] 5.57 4.90 2.10
[2,] 4.90 6.56 1.48
[3,] 2.10 1.48 2.82
> print( x%*%t(x) , 3 )
[,1] [,2] [,3] [,4]
[1,] 4.126 -0.196 2.16 -4.184
[2,] -0.196 1.056 1.32 -0.764
[3,] 2.160 1.324 3.10 -3.316
[4,] -4.184 -0.764 -3.32 6.664
>
> cv_inv = solve ( cv )
>
> print ( cv_inv , 3 )
GOOGL.Adjusted AAPL.Adjusted CSCO.Adjusted
GOOGL.Adjusted 5027 -1782 -1529
AAPL.Adjusted -1782 4045 -1288
CSCO.Adjusted -1529 -1288 4503
> print(cv_inv%*%cv , 3 )
GOOGL.Adjusted AAPL.Adjusted CSCO.Adjusted
GOOGL.Adjusted 1.00e+00 2.22e-16 1.67e-16
AAPL.Adjusted -2.78e-17 1.00e+00 -5.55e-17
CSCO.Adjusted 1.11e-16 0.00e+00 1.00e+00
>
> library("corpcor", lib.loc="~/R/win-library/3.6")
> is.positive.definite( cv )
[1] TRUE
> n = dim( data ) [ 1 ]
>
> n
NULL
> n = dim( google )
> n
[1] 3307 1
> s = data [ , 7 ]
kurtosis, skewness
> skewness ( rets )
GOOGL.Adjusted AAPL.Adjusted CSCO.Adjusted
0.4842460 -0.4681109 -0.5107760
>
> kurtosis (rets )
GOOGL.Adjusted AAPL.Adjusted CSCO.Adjusted
14.51975 10.18243 14.56774
>
> skewness (rnorm ( 1000000 ) )
[1] -0.0003929433
>
> kurtosis(rnorm ( 1000000 ) )
[1] 3.007031
> h = 1/252
>
> sigma = sd ( rets )/sqrt( h )
>
> sigma
[1] 0.2941101
> mu = mean( rets)/h+0.5*sigma^2
>
> mu
[1] 0.1962266
> LL = function (params,rets ) {
+ alpha = params [ 1 ] ; sigsq = params [ 2 ]
+ logf = −log ( sqrt(2*pi*sigsq ) )
+ − ( rets−alpha )^2 / (2*sigsq )
+ LL = −sum( logf ) }
>
> LL
function (params,rets ) {
alpha = params [ 1 ] ; sigsq = params [ 2 ]
logf = -log ( sqrt(2*pi*sigsq ) )
- ( rets-alpha )^2 / (2*sigsq )
LL = -sum( logf ) }
> params = c ( 0.001 , 0.001 )

0 Comments