0

I'm working through Funq by Nick Psaris but need a null aware function for making a covariance matrix. I will use it to create a mahalanobis distance matrix. He supplies some null aware functions:

navg = null aware avg

nvar null aware var

nsvar = null aware sample var

Any suggestions?

1 Answers1

0

Here's what I've come up with:

nSCovMatrix:{[matrix]
    //takes table or array
    //null aware Sample Covariance Matrix
    $[98h=type matrix;matrix:"f"$(flip matrix[cols matrix]);matrix:"f"$matrix];
    corMatrix:u cor/:\:u: flip matrix;
    sd:sqrt nsvar[matrix]; //standard dev of each variable
    diagMatrix:sd*{x=/:x}til first count each dataSet;
    :covMatrix:(diagMatrix) mmu corMatrix mmu (diagMatrix);
 };

I find the correlation matrix and turn it into the covariance matrix by taking the stnd dev of each variable, making a diagonal matrix with the s.d., and multiplying it against the correlation matrix twice. Far from optimized though