public static TMatrix StandardizeGToCreateX <TMatrix>(int maxValue, Matrix <string, string, double> gMatrix, MatrixFactoryDelegate <TMatrix, string, string, double> zeroMatrixFractory, ParallelOptions parallelOptions, bool onlyMeanCenter = false) where TMatrix : Matrix <string, string, double> { Console.WriteLine("StandardizeGToCreateX"); //var xMatrix = DenseMatrix<string, string, double>.CreateDefaultInstance(gMatrix.RowKeys, gMatrix.ColKeys, double.NaN); //Inits to 0 TMatrix xMatrix = zeroMatrixFractory(gMatrix.RowKeys, gMatrix.ColKeys, double.NaN); //Inits to 0 Helper.CheckCondition(xMatrix.GetValueOrMissing(0, 0) == 0, "xMatrix must start init'ed to zeros, but even at xMatrix[0,0] it is not 0"); //First create x matrix //Parallel.ForEach(gMatrix.RowKeys, parallelOptions, var => var counterWithMessages = new CounterWithMessages("StandardizeGToCreateX: row #{0}", 100, gMatrix.RowCount); foreach (string var in gMatrix.RowKeys) { counterWithMessages.Increment(); //The paper divides by (2+2*Count) because its values are 0,1,2. Because ours are 0,1, we divide by (2+Count) var row = gMatrix.SelectRowsView(var); List <double> nonMissingValues = row.Values.ToList(); double rowSum = nonMissingValues.Sum(); double rowMean = rowSum / (double)nonMissingValues.Count; double piSmoothedCount = (1.0 + rowSum) / (2.0 + maxValue * (double)nonMissingValues.Count); double stdDevPi = Math.Sqrt(piSmoothedCount * (1 - piSmoothedCount)); Helper.CheckCondition(!double.IsNaN(stdDevPi), "stdDevPi is NaN outside loop, likely because data input was not in 0/1/2 format as expected"); Parallel.ForEach(row.RowKeyColKeyValues, parallelOptions, triple => { string cid = triple.ColKey; double gValue = triple.Value; double xValue; if (onlyMeanCenter) { xValue = (gValue - rowMean); } else { xValue = (gValue - rowMean) / stdDevPi; } xMatrix[var, cid] = xValue; //if (doubleUp) //{ // xMatrix[new Pair<string, bool>(var, true), cid] = -xValue; //} }); //}); } Console.WriteLine(); return(xMatrix); }
static public TMatrix Standardize <TMatrix>(this TMatrix matrix, int maxMatrixVal, MatrixFactoryDelegate <TMatrix, string, string, double> zeroMatrixFractory, ParallelOptions parallelOptions) where TMatrix : Matrix <string, string, double> { TMatrix snpMatrixNew = EigenstratMain.StandardizeGToCreateX(maxMatrixVal, matrix, zeroMatrixFractory, parallelOptions); return(snpMatrixNew); }