/// <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);
        }
Ejemplo n.º 2
0
        public double Evaluate(double[] pointA)
        {
            if (!alreadyEvaluated)
            {
                //Wi = -0.5 * inverseCovarianceMatrix;
                //wi = inverseCovarianceMatrix * meanVector;
                //w0Part1 = -0.5 * meanVector.Transpose() * inverseCovarianceMatrix * meanVector;
                //w0Part2 = -(0.5 * Math.Log(covarianceMatrix.Determinant().Re, Math.E));
                //w0 = w0Part1.Determinant().Re + w0Part2;
            }

            if (!alreadyEvaluated)
            {
                Wi = -0.5 * inverseCovarianceMatrix;
                wi = inverseCovarianceMatrix * meanVector;

                Matrix wi0Part1 = -0.5 * meanVector.Transpose() * inverseCovarianceMatrix * meanVector;
                double wi0Part2 = -(0.5 * Math.Log(covarianceMatrix.Determinant().Re, Math.E));
                double wi0Part3 = Math.Log(Prior);

                wi0 = wi0Part1.Determinant().Re + wi0Part2 + wi0Part3;
            }



            alreadyEvaluated = true;

            PatternTools.CSML.Matrix point = new PatternTools.CSML.Matrix(pointA);



            //Term 1
            PatternTools.CSML.Matrix t1 = point.Transpose() * Wi * point;

            //Term 2
            PatternTools.CSML.Matrix t2 = wi.Transpose() * point;

            double gx = t1.Determinant().Re + t2.Determinant().Re + wi0;

            return(gx);
        }
Ejemplo n.º 3
0
        public static List <int> Verify2(List <sparseMatrixRow> rows)
        {
            //We group rows by class, and then for each class we check if there is a dim that holds the same value for all
            //We then eliminate these dims from all rows of all classes and report the eliminated dims

            List <int> unstableDims = new List <int>();

            var rowsGroupedByClasses = from r in rows
                                       group r by r.Lable into g
                                       select new { label = g.Key, rows = g };

            Dictionary <int, List <sparseMatrixRow> > lableRowDict = rowsGroupedByClasses.ToDictionary(a => a.label, a => a.rows.ToList());

            foreach (KeyValuePair <int, List <sparseMatrixRow> > kvp in lableRowDict)
            {
                SparseMatrix matrix     = new SparseMatrix(rows);
                Matrix       meanVector = new PatternTools.CSML.Matrix(matrix.GetMeanVector().ToArray());
                double[,] covMatrix = PatternTools.SparseMatrix.CovarianceMatrix(matrix.ToDoubleArrayMatrix(), matrix.GetMeanVector().ToArray());

                unstableDims.AddRange(checkStability(covMatrix));
            }

            unstableDims = unstableDims.Distinct().ToList();
            unstableDims.Sort((a, b) => b.CompareTo(a));

            //Now we eliminate these dims from the sparse matrix
            foreach (int d in unstableDims)
            {
                int index = rows[0].Dims.FindIndex(a => a == d);

                foreach (sparseMatrixRow r in rows)
                {
                    r.Values.RemoveAt(index);
                }

                rows[0].Dims.RemoveAt(index);
            }

            return(unstableDims);
        }
Ejemplo n.º 4
0
        /// <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);
        }
 public static List <double> CorrectForSignal(PatternTools.CSML.Matrix cInverse, double[] signal)
 {
     //Mcorrected = c^-1 * signal
     PatternTools.CSML.Matrix correctedS = cInverse * new PatternTools.CSML.Matrix(signal);
     return(correctedS.GetRow(0));
 }