Path: blob/master/src/residualcokurtosisMF.c
1433 views
1#include <R.h>2#include <Rinternals.h>34SEXP residualcokurtosisMF(SEXP NN, SEXP sstockM2, SEXP sstockM4, SEXP bbetacov){5/*6arguments7NN : integer, number of assets8sstockM2 : vector of length NN, 2nd moment of the model residuals9sstockM4 : vector of length NN, 4th moment of the model residuals10bbetacov : vector of length NN * NN, beta and factor covariance1112Note that this function was orignally written in C++ (using Rcpp) by13Joshua Ulrich and re-written using the C API by Ross Bennett14*/1516// // declare pointers for the vectors17double *stockM2, *stockM4, *betacov;1819// Do I need to protect these if they are function arguments?20// use REAL() to access the C array inside the numeric vector passed in from R21stockM2 = REAL(sstockM2);22stockM4 = REAL(sstockM4);23betacov = REAL(bbetacov);2425// Coerce length one R vector into C scalar26int N = asInteger(NN);2728// Allocate and initialize matrix values to 029// Do I need to fill the matrix with zeros here?30SEXP M4MF = PROTECT(allocMatrix(REALSXP, N, N*N*N));31double *rM4MF = REAL(M4MF);32// Note that the matrix is stored in column-major order and is represented33// as a 1-dimensional array. Access the element in X(row, col) with:34// element = row + column * nrows35// outer loop over the columns and inner loop down the row36for (int jj = 0; jj < N*N*N; jj++) {37for (int ii = 0; ii < N; ii++) {38rM4MF[ii + jj * N] = 0.0;39}40}4142// Rcpp declarations43// NumericVector stockM2(sstockM2);44// NumericVector stockM4(sstockM4);45// NumericVector betacov(bbetacov);46// int N = as<int>(NN);47// NumericMatrix M4MF(N, pow(N,3));4849double kijkl = 0.0;50int pos = 0;5152for(int i=0; i<N; i++) {53for(int j=0; j<N; j++) {54for(int k=0; k<N; k++) {55for(int l=0; l<N; l++) {56// in the general case: i, j , k and l are all different57kijkl = 0;58// if at least one of them are equal:59if( (i==j) || (i==k) || (i==l) || (j==k) || (j==l) || (k==l) ) {60if( (i==j) && (i==k) && (i==l) ) {61// These are the kurtosis estimates of the individual assets: 6 bi S bi E[ei^2] + E[ei^4]62// this is element i,i in the cov, therefore in the vectorized form it is i*N+i63pos = i*N+i;64kijkl = 6*betacov[pos]*stockM2[i]+stockM4[i];65} else {66if( ((i==j) && (i==k)) || ((i==j) && (i==l)) || ((i==k) && (i==l)) || ((j==k) && (j==l)) ) {67// 3 indices are identical68if( (i==j) && (i==k) ) {69// all indices are the same except l: 3 biSbl E[ei^2]70pos = i*N+l;71kijkl = 3*betacov[pos]*stockM2[i];72} else73if( (i==j) && (i==l) ) {74// all indices are the same except k: 3 biSbk E[ei^2]75pos = i*N+k;76kijkl = 3*betacov[pos]*stockM2[i];77} else78if( (i==k) && (i==l) ) {79// all indices are the same except j: 3 biSbj E[ei^2]80pos =i*N+j;81kijkl = 3*betacov[pos]*stockM2[i];82} else83if( (j==k) && (j==l) ) {84// all indices are the same except i: 3 biSbi E[ei^2]85pos =j*N+i;86kijkl = 3*betacov[pos]*stockM2[j];87}88}89else {90if( ((i==j) && (k==l)) || ((i==k) && (j==l)) || ((i==l) && (j==k)) ) {91// kiijj = E[ U[,i]^2*U[,j]^2 ] = r5*sqrt( vm4[i]*vm4[j] )92if( (i==j) && (k==l) ) {93// biSbi E[ek^2] + bkSbk E[ei^2] + E[ei^2]E[ek^2]94pos = i*N+i;95kijkl = betacov[pos]*stockM2[k];96pos = k*N+k;97kijkl = kijkl + betacov[pos]*stockM2[i]+stockM2[i]*stockM2[k];98} else99if( (i==k) && (j==l) ) {100// biSbi E[ej^2] + bjSbj E[ei^2] + E[ei^2]E[ej^2]101pos =i*N+i;102kijkl = betacov[pos]*stockM2[j];103pos = j*N+j;104kijkl = betacov[pos]*stockM2[i]+stockM2[i]*stockM2[j];105} else106if( (i==l) && (j==k) ) {107// biSbi E[ej^2] + bjSbj E[ei^2] + E[ei^2]E[ej^2]108pos =i*N+i;109kijkl = betacov[pos]*stockM2[j];110pos = j*N+j;111kijkl = kijkl + betacov[pos]*stockM2[i]+stockM2[i]*stockM2[j];112}113} else {114if( i==j ) {115// bkSbl E[ei^2]116pos = k*N+l;117kijkl = betacov[pos]*stockM2[i];118} else119if( i==k ) {120// bjSbl E[ei^2]121pos = j*N+l;122kijkl = betacov[pos]*stockM2[i];123} else124if( i==l ) {125// bjSbk E[ei^2]126pos = j*N+k;127kijkl = betacov[pos]*stockM2[i];128} else129if( j==k ) {130// biSbl E[ej^2]131pos = i*N+l;132kijkl = betacov[pos]*stockM2[j];133} else134if( j==l ) {135// biSbk E[ej^2]136pos = i*N+k;137kijkl = betacov[pos]*stockM2[j];138} else139if( k==l ) {140// biSbj E[ek^2]141pos = i*N+j;142kijkl = betacov[pos]*stockM2[k];143}144}145}146} // no kurtosis estimates of individual assets147} // at least one of them are not equal148// i denotes the subcomoment matrix149//M4MF(k,i*pow(N,2)+j*N+l) = kijkl;150// Element = row + column * nrows151rM4MF[k + (i * N * N + j * N + l) * N] = kijkl;152} // loop l153} // loop k154} // loop j155} // loop i156UNPROTECT(1);157return M4MF;158}159160