/// <summary> /// Each row should contain the correction factors in numbers that range from 1 to 100 /// The value for the monoisotopic should be entered as -1 /// </summary> /// <param name="correctionTable"></param> /// <returns></returns> public static PatternTools.CSML.Matrix GenerateInverseCorrectionMatrix(List <List <double> > correctionTable) { //We now need to normalize the -1 so the sum adds to 100 foreach (List <double> l in correctionTable) { double sum = l.Sum() + 1; int index = l.FindIndex(a => a == -1); l[index] = 100 - sum; } //Generate Correction Matrix //Prepare the coeficient matrix double[,] coeficientMatrix = new double[correctionTable.Count, correctionTable.Count]; int counter = -1; for (int i = 0; i < correctionTable.Count; i++) { counter++; for (int j = 0; j < 5; j++) { try { coeficientMatrix[j - 2 + counter, i] = correctionTable[i][j]; } catch (Exception e) { Console.WriteLine(e.Message + "\n i = {0}, j = {1}", i, j); } } } //Mcorrected = c^-1 * signal PatternTools.CSML.Matrix c = new PatternTools.CSML.Matrix(coeficientMatrix); PatternTools.CSML.Matrix cInverse = c.Inverse(); cInverse *= 100; return(cInverse); }
/// <summary> /// The method returns unstable dims that were elmininated from the sparse matrix and should be eliminated from future input vectors used for classification /// </summary> /// <param name="useRobustStatistics"></param> /// <param name="regularizationFactor"></param> /// <param name="saveMatrices"></param> /// <returns></returns> public List <int> ComputeMatrices(bool useRobustStatistics, bool saveMatrices) { //Get the mean vector double[,] covMatrix; if (useRobustStatistics) { meanVector = new PatternTools.CSML.Matrix(matrix.GetMedianVector().ToArray()); covMatrix = PatternTools.SparseMatrix.CovarianceMatrix(matrix.ToDoubleArrayMatrix(), matrix.GetMedianVector().ToArray()); } else { meanVector = new PatternTools.CSML.Matrix(matrix.GetMeanVector().ToArray()); covMatrix = PatternTools.SparseMatrix.CovarianceMatrix(matrix.ToDoubleArrayMatrix(), matrix.GetMeanVector().ToArray()); } unstableDims = checkStability(covMatrix); if (unstableDims.Count > 0) { unstableDims.Sort((a, b) => b.CompareTo(a)); double perturbationCounter = 0; if (unstableDims.Count > 0) { Console.WriteLine("**Warning:: I am adding a perturbation to the convariance matrix for stability reasons!!!"); foreach (int d in unstableDims) { foreach (sparseMatrixRow r in matrix.theMatrixInRows) { perturbationCounter += 0.0005; r.Values[d] += perturbationCounter; if (perturbationCounter > 0.05) { perturbationCounter = 0; } } } covMatrix = PatternTools.SparseMatrix.CovarianceMatrix(matrix.ToDoubleArrayMatrix(), matrix.GetMedianVector().ToArray()); } if (useRobustStatistics) { covMatrix = PatternTools.SparseMatrix.CovarianceMatrix(matrix.ToDoubleArrayMatrix(), matrix.GetMedianVector().ToArray()); } else { covMatrix = PatternTools.SparseMatrix.CovarianceMatrix(matrix.ToDoubleArrayMatrix(), matrix.GetMeanVector().ToArray()); } } covarianceMatrix = new CSML.Matrix(covMatrix); inverseCovarianceMatrix = covarianceMatrix.Inverse(); if (saveMatrices) { matrix.saveMatrix(matrix.theMatrixInRows[0].Lable + "FullMatrix.txt"); StreamWriter sr = new StreamWriter(matrix.theMatrixInRows[0].Lable + "_covMatrix.txt"); int top = (int)Math.Sqrt(covMatrix.Length); for (int i = 0; i < top; i++) { for (int j = 0; j < top; j++) { sr.Write(covMatrix[i, j] + "\t"); } sr.WriteLine(""); } sr.Close(); Console.WriteLine("Saving variance matrix and its inverse to disk."); } return(unstableDims); }