Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
braverock
GitHub Repository: braverock/portfolioanalytics
Path: blob/master/src/residualcokurtosisSF.c
1433 views
1
#include <R.h>
2
#include <Rinternals.h>
3
4
SEXP residualcokurtosisSF(SEXP NN, SEXP sstockM2, SEXP sstockM4, SEXP mfactorM2, SEXP bbeta){
5
6
/*
7
arguments
8
NN : integer, number of assets
9
sstockM2 : vector of length NN, 2nd moment of the model residuals
10
sstockM4 : vector of length NN, 4th moment of the model residuals
11
mfactorM2 : double, 2nd moment of the model factors
12
bbeta : vector of length NN, factor loadings of model
13
14
Note that this function was orignally written in C++ (using Rcpp) by
15
Joshua Ulrich and re-written using the C API by Ross Bennett
16
*/
17
18
// declare pointers for the vectors
19
double *stockM2, *stockM4, *beta;
20
21
// Do I need to protect these if they are function arguments?
22
// use REAL() to access the C array inside the numeric vector passed in from R
23
stockM2 = REAL(sstockM2);
24
stockM4 = REAL(sstockM4);
25
beta = REAL(bbeta);
26
27
// Coerce length one R vectors into C scalars
28
double factorM2 = asReal(mfactorM2);
29
int N = asInteger(NN);
30
31
32
// Allocate and initialize matrix values to 0
33
// Do I need to fill the matrix with zeros here?
34
SEXP M4SF = PROTECT(allocMatrix(REALSXP, N, N*N*N));
35
double *rM4SF = REAL(M4SF);
36
// Note that the matrix is stored in column-major order and is represented
37
// as a 1-dimensional array. Access the element in X(row, col) with:
38
// element = row + column * nrows
39
// outer loop over the columns and inner loop down the row
40
for (int jj = 0; jj < N*N*N; jj++) {
41
for (int ii = 0; ii < N; ii++) {
42
rM4SF[ii + jj * N] = 0.0;
43
}
44
}
45
46
// Rcpp declarations
47
// NumericVector stockM2(sstockM2);
48
// NumericVector stockM4(sstockM4);
49
// double factorM2 = as<double>(mfactorM2);
50
// NumericVector beta(bbeta);
51
// int N = beta.size();
52
// NumericMatrix M4SF(N, pow(N,3));
53
54
double kijkl=0;
55
56
for(int i=0; i<N; i++) {
57
for(int j=0; j<N; j++) {
58
for(int k=0; k<N; k++) {
59
for(int l=0; l<N; l++) {
60
// in the general case: i, j , k and l are all different
61
kijkl = 0;
62
// if at least one of them are equal:
63
if( (i==j) || (i==k) || (i==l) || (j==k) || (j==l) || (k==l) ) {
64
if( (i==j) && (i==k) && (i==l) ) {
65
// These are the kurtosis estimates of the individual assets: E[u^4]
66
// kijkl = 6*R_pow_di(beta[i],2)*factorM2*stockM2[i]+stockM4[i];
67
kijkl = 6*beta[i]*beta[i]*factorM2*stockM2[i]+stockM4[i];
68
} else {
69
if( ((i==j) && (i==k)) || ((i==j) && (i==l)) || ((i==k) && (i==l)) || ((j==k) && (j==l)) ) {
70
// kiij E[ U[,i]^3*U[,j] ] = r3*sqrt( vm6[i]*vm2[j] )
71
if( (i==j) && (i==k) ) {
72
kijkl = 3*beta[i]*beta[l]*factorM2*stockM2[i];
73
} else
74
if( (i==j) && (i==l) ) {
75
kijkl = 3*beta[i]*beta[k]*factorM2*stockM2[i];
76
} else
77
if( (i==k) && (i==l) ) {
78
kijkl = 3*beta[i]*beta[j]*factorM2*stockM2[i];
79
} else
80
if( (j==k) && (j==l) ) {
81
kijkl = 3*beta[j]*beta[i]*factorM2*stockM2[j];
82
}
83
} else {
84
if( ((i==j) && (k==l)) || ((i==k) && (j==l)) || ((i==l) && (j==k)) ) {
85
// kiijj = E[ U[,i]^2*U[,j]^2 ] = r5*sqrt( vm4[i]*vm4[j] )
86
if( (i==j) && (k==l) ) {
87
//kijkl = R_pow_di(beta[i],2)*factorM2*stockM2[k] + R_pow_di(beta[k],2)*factorM2*stockM2[i]+stockM2[i]*stockM2[k];
88
kijkl = beta[i]*beta[i]*factorM2*stockM2[k] + beta[k]*beta[k]*factorM2*stockM2[i]+stockM2[i]*stockM2[k];
89
} else
90
if( (i==k) && (j==l) ) {
91
//kijkl = R_pow_di(beta[i],2)*factorM2*stockM2[j] + R_pow_di(beta[j],2)*factorM2*stockM2[i]+stockM2[i]*stockM2[j];
92
kijkl = beta[i]*beta[i]*factorM2*stockM2[j] + beta[j]*beta[j]*factorM2*stockM2[i]+stockM2[i]*stockM2[j];
93
} else
94
if( (i==l) && (j==k) ) {
95
//kijkl = R_pow_di(beta[i],2)*factorM2*stockM2[j] + R_pow_di(beta[j],2)*factorM2*stockM2[i]+stockM2[i]*stockM2[j];
96
kijkl = beta[i]*beta[i]*factorM2*stockM2[j] + beta[j]*beta[j]*factorM2*stockM2[i]+stockM2[i]*stockM2[j];
97
}
98
} else {
99
// kiijk = E[ U[,i]^2*U[,j]*U[,k] ] = r6*sqrt( vm4[i]*r5*sqrt( vm4[j]*vm4[k] ) )
100
if( i==j ) {
101
kijkl = beta[k]*beta[l]*factorM2*stockM2[i];
102
} else
103
if( i==k ) {
104
kijkl = beta[j]*beta[l]*factorM2*stockM2[i];
105
} else
106
if( i==l ) {
107
kijkl = beta[j]*beta[k]*factorM2*stockM2[i];
108
} else
109
if( j==k ) {
110
kijkl = beta[i]*beta[l]*factorM2*stockM2[j];
111
} else
112
if( j==l ) {
113
kijkl = beta[i]*beta[k]*factorM2*stockM2[j];
114
} else
115
if( k==l ) {
116
kijkl = beta[i]*beta[j]*factorM2*stockM2[k];
117
}
118
}
119
}
120
} // no kurtosis estimates of individual assets
121
} // at least one of them are not equal
122
// i denotes the subcomoment matrix
123
// M4SF(k,i*pow(N,2)+j*N+l) = kijkl;
124
// element = row + column * nrows
125
rM4SF[k + (i * N * N + j * N + l) * N] = kijkl;
126
} // loop l
127
} // loop k
128
} // loop j
129
} // loop i
130
UNPROTECT(1);
131
return M4SF;
132
}
133
134