public CrossPRESSEvaluator(
   int[] spectralRegions,
   int numFactors,
   ICrossValidationGroupingStrategy groupingStrategy,
   SpectralPreprocessingOptions preprocessOptions,
   MultivariateRegression analysis
   )
   : base(spectralRegions,numFactors,groupingStrategy,preprocessOptions,analysis)
 {
 }
 public CrossPRESSEvaluator(
     int[] spectralRegions,
     int numFactors,
     ICrossValidationGroupingStrategy groupingStrategy,
     SpectralPreprocessingOptions preprocessOptions,
     MultivariateRegression analysis
     )
     : base(spectralRegions, numFactors, groupingStrategy, preprocessOptions, analysis)
 {
 }
 public CrossPredictedXResidualsEvaluator(
     int numberOfPoints,
     int[] spectralRegions,
     int numFactors,
     ICrossValidationGroupingStrategy groupingStrategy,
     SpectralPreprocessingOptions preprocessOptions,
     MultivariateRegression analysis
     )
     : base(spectralRegions, numFactors, groupingStrategy, preprocessOptions, analysis)
 {
     _numberOfPoints = numberOfPoints;
 }
 public CrossPredictedYEvaluator(
     int[] spectralRegions,
     int numFactors,
     ICrossValidationGroupingStrategy groupingStrategy,
     SpectralPreprocessingOptions preprocessOptions,
     MultivariateRegression analysis,
     IMatrix YCrossValidationPrediction
     )
     : base(spectralRegions, numFactors, groupingStrategy, preprocessOptions, analysis)
 {
     _YCrossValidationPrediction = YCrossValidationPrediction;
 }
 public CrossValidationWorker(
     int[] spectralRegions,
     int numFactors,
     ICrossValidationGroupingStrategy groupingStrategy,
     SpectralPreprocessingOptions preprocessOptions,
     MultivariateRegression analysis
     )
 {
     _spectralRegions   = spectralRegions;
     _numFactors        = numFactors;
     _groupingStrategy  = groupingStrategy;
     _preprocessOptions = preprocessOptions;
     _analysis          = analysis;
 }
 public CrossValidationWorker(
   int[] spectralRegions,
   int numFactors,
   ICrossValidationGroupingStrategy groupingStrategy,
   SpectralPreprocessingOptions preprocessOptions,
   MultivariateRegression analysis
   )
 {
   _spectralRegions = spectralRegions;
   _numFactors = numFactors;
   _groupingStrategy = groupingStrategy;
   _preprocessOptions = preprocessOptions;
   _analysis = analysis;
 }
 public CrossPredictedYEvaluator(
   int[] spectralRegions,
   int numFactors,
   ICrossValidationGroupingStrategy groupingStrategy,
   SpectralPreprocessingOptions preprocessOptions,
   MultivariateRegression analysis,
   IMatrix YCrossValidationPrediction
   )
   : base(spectralRegions,numFactors,groupingStrategy,preprocessOptions,analysis)
 {
   _YCrossValidationPrediction = YCrossValidationPrediction;
 }
 public CrossPredictedXResidualsEvaluator(
   int numberOfPoints,
   int[] spectralRegions,
   int numFactors,
   ICrossValidationGroupingStrategy groupingStrategy,
   SpectralPreprocessingOptions preprocessOptions,
   MultivariateRegression analysis
   )
   : base(spectralRegions,numFactors,groupingStrategy,preprocessOptions,analysis)
 {
   _numberOfPoints = numberOfPoints;
 }
    /// <summary>
    /// Calculates the spectral residuals obtained from cross validation.
    /// </summary>
    /// <param name="xOfX">The spectral wavelength values corresponding to the spectral bins.</param>
    /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param>
    /// <param name="Y">Matrix of y values (e.g. concentrations).</param>
    /// <param name="numFactors">Number of factors used for calculation.</param>
    /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param>
    /// <param name="preprocessOptions">Information how to preprocess the data.</param>
    /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param>
    /// <param name="crossXResiduals">Returns the matrix of spectral residuals</param>
    /// <returns>Mean number of spectra used for prediction.</returns>
    public static double GetCrossXResiduals(
      IROVector xOfX,
      IROMatrix X, // matrix of spectra (a spectra is a row of this matrix)
      IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix)
      int numFactors,
      ICrossValidationGroupingStrategy groupingStrategy,
      SpectralPreprocessingOptions preprocessOptions,
      MultivariateRegression regress,

      out IROMatrix crossXResiduals
      )
    {
      return GetCrossXResiduals(
        SpectralPreprocessingOptions.IdentifyRegions(xOfX),
        X, // matrix of spectra (a spectra is a row of this matrix)
        Y, // matrix of concentrations (a mixture is a row of this matrix)
        numFactors,
        groupingStrategy,
        preprocessOptions,
        regress,

        out crossXResiduals);
    }
Example #10
0
		/// <summary>
		///
		/// </summary>
		/// <param name="mcalib"></param>
		/// <param name="groupingStrategy"></param>
		/// <param name="preprocessOptions"></param>
		/// <param name="xOfX"></param>
		/// <param name="matrixX">Matrix of horizontal spectra, centered and preprocessed.</param>
		/// <param name="matrixY">Matrix of concentrations, centered.</param>
		/// <param name="numberOfFactors"></param>
		/// <param name="predictedY"></param>
		/// <param name="spectralResiduals"></param>
		public virtual void CalculateCrossPredictedY(
			IMultivariateCalibrationModel mcalib,
			ICrossValidationGroupingStrategy groupingStrategy,
			SpectralPreprocessingOptions preprocessOptions,
			IROVector xOfX,
			IMatrix matrixX,
			IMatrix matrixY,
			int numberOfFactors,
			IMatrix predictedY,
			IMatrix spectralResiduals)
		{
			MultivariateRegression.GetCrossYPredicted(xOfX,
				matrixX, matrixY, numberOfFactors, groupingStrategy, preprocessOptions,
				this.CreateNewRegressionObject(),
				predictedY);
		}
    /// <summary>
    /// Calculates the spectral residuals obtained from cross validation.
    /// </summary>
    /// <param name="spectralRegions">Array of ascending indices representing the starting indices of spectral regions.</param>
    /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param>
    /// <param name="Y">Matrix of y values (e.g. concentrations).</param>
    /// <param name="numFactors">Number of factors used for calculation.</param>
    /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param>
    /// <param name="preprocessOptions">Information how to preprocess the data.</param>
    /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param>
    /// <param name="crossXResiduals">Returns the matrix of spectral residuals</param>
    /// <returns>Mean number of spectra used for prediction.</returns>
    public static double GetCrossXResiduals(
      int[] spectralRegions,
      IROMatrix X, // matrix of spectra (a spectra is a row of this matrix)
      IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix)
      int numFactors,
      ICrossValidationGroupingStrategy groupingStrategy,
      SpectralPreprocessingOptions preprocessOptions,
      MultivariateRegression regress,

      out IROMatrix crossXResiduals
      )
    {
      CrossPredictedXResidualsEvaluator worker = new CrossPredictedXResidualsEvaluator(X.Rows,spectralRegions,numFactors,groupingStrategy,preprocessOptions,regress);
      double result = CrossValidationIteration(X,Y,groupingStrategy,new CrossValidationIterationFunction(worker.EhCrossValidationWorker));
      crossXResiduals = worker.XCrossResiduals;
      return result;
    }
    /// <summary>
    /// Calculates the cross predicted y values.
    /// </summary>
    /// <param name="xOfX">The spectral wavelength values corresponding to the spectral bins.</param>
    /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param>
    /// <param name="Y">Matrix of y values (e.g. concentrations).</param>
    /// <param name="numFactors">Number of factors used for calculation.</param>
    /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param>
    /// <param name="preprocessOptions">Information how to preprocess the data.</param>
    /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param>
    /// <param name="yCrossPredicted">Matrix of cross predicted y values. Must be of same dimension as the Y matrix.</param>
    /// <returns>Mean number of spectra used for cross prediction.</returns>
    public static double GetCrossYPredicted(
      IROVector xOfX,
      IROMatrix X, // matrix of spectra (a spectra is a row of this matrix)
      IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix)
      int numFactors,
      ICrossValidationGroupingStrategy groupingStrategy,
      SpectralPreprocessingOptions preprocessOptions,
      MultivariateRegression regress,

      IMatrix yCrossPredicted // vertical value of PRESS values for the cross validation
      )
    {
      return GetCrossYPredicted(
        SpectralPreprocessingOptions.IdentifyRegions(xOfX),
        X, // matrix of spectra (a spectra is a row of this matrix)
        Y, // matrix of concentrations (a mixture is a row of this matrix)
        numFactors,
        groupingStrategy,
        preprocessOptions,
        regress,
        yCrossPredicted );
    }
    /// <summary>
    /// Calculates the cross predicted y values.
    /// </summary>
    /// <param name="spectralRegions">Array of ascending indices representing the starting indices of spectral regions.</param>
    /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param>
    /// <param name="Y">Matrix of y values (e.g. concentrations).</param>
    /// <param name="numFactors">Number of factors used for calculation.</param>
    /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param>
    /// <param name="preprocessOptions">Information how to preprocess the data.</param>
    /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param>
    /// <param name="yCrossPredicted">Matrix of cross predicted y values. Must be of same dimension as the Y matrix.</param>
    /// <returns>Mean number of spectra used for cross prediction.</returns>
    public static double GetCrossYPredicted(
      int[] spectralRegions,
      IROMatrix X, // matrix of spectra (a spectra is a row of this matrix)
      IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix)
      int numFactors,
      ICrossValidationGroupingStrategy groupingStrategy,
      SpectralPreprocessingOptions preprocessOptions,
      MultivariateRegression regress,

      IMatrix yCrossPredicted // vertical value of PRESS values for the cross validation
      )
    {
      CrossPredictedYEvaluator worker = new CrossPredictedYEvaluator(spectralRegions,numFactors,groupingStrategy,preprocessOptions,regress,yCrossPredicted);
      double result = CrossValidationIteration(X,Y,groupingStrategy,new CrossValidationIterationFunction(worker.EhYCrossPredicted));

      return result;
    }
    /// <summary>
    /// Get the cross predicted error sum of squares for the number of factors=0...numFactors.
    /// </summary>
    /// <param name="spectralRegions">Array of ascending indices representing the starting indices of spectral regions.</param>
    /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param>
    /// <param name="Y">Matrix of y values (e.g. concentrations).</param>
    /// <param name="numFactors">Maximum number of factors to calculate the cross PRESS for.</param>
    /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param>
    /// <param name="preprocessOptions">Information how to preprocess the data.</param>
    /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param>
    /// <param name="crossPRESS">The vector of CROSS press values. Note that this vector has the length numFactor+1.</param>
    /// <returns>The mean number of spectra used for prediction.</returns>
    public static double GetCrossPRESS(
      int[] spectralRegions,
      IROMatrix X, // matrix of spectra (a spectra is a row of this matrix)
      IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix)
      int numFactors,
      ICrossValidationGroupingStrategy groupingStrategy,
      SpectralPreprocessingOptions preprocessOptions,
      MultivariateRegression regress,
      out IROVector crossPRESS // vertical value of PRESS values for the cross validation
      )
    {
      CrossPRESSEvaluator worker = new CrossPRESSEvaluator(spectralRegions,numFactors,groupingStrategy,preprocessOptions,regress);
      double result = CrossValidationIteration(X,Y,groupingStrategy,new CrossValidationIterationFunction(worker.EhCrossPRESS));

      crossPRESS = VectorMath.ToROVector(worker.CrossPRESS,worker.NumberOfFactors+1);

      return result;
    }
    /// <summary>
    /// This function separates the spectra into a bunch of spectra used for calibration and the rest of spectra
    /// used for prediction. This separation is repeated until all spectra are used exactly one time for prediction.
    /// </summary>
    /// <param name="X">Matrix of spectra (horizontal oriented).</param>
    /// <param name="Y">Matrix of y values.</param>
    /// <param name="groupingStrategy">The strategy how to separate the spectra into the calibration and prediction spectra.</param>
    /// <param name="crossFunction">The function that is called for each separation.</param>
    /// <returns>The mean number of spectra that was used for prediction.</returns>
    public static double CrossValidationIteration(
      IROMatrix X, // matrix of spectra (a spectra is a row of this matrix)
      IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix)
      ICrossValidationGroupingStrategy groupingStrategy,
      CrossValidationIterationFunction crossFunction
      )
    {

      //      int[][] groups = bExcludeGroups ? new ExcludeGroupsGroupingStrategy().Group(Y) : new ExcludeSingleMeasurementsGroupingStrategy().Group(Y);
      int[][] groups = groupingStrategy.Group(Y);


      IMatrix XX=null; 
      IMatrix YY=null; 
      IMatrix XU=null; 
      IMatrix YU=null; 
      

      for(int nGroup=0 ,prevNumExcludedSpectra = int.MinValue ;nGroup < groups.Length;nGroup++)
      {
        int[] spectralGroup = groups[nGroup];
        int numberOfExcludedSpectraOfGroup = spectralGroup.Length;

        if(prevNumExcludedSpectra != numberOfExcludedSpectraOfGroup)
        {
          XX = new MatrixMath.BEMatrix(X.Rows-numberOfExcludedSpectraOfGroup,X.Columns);
          YY = new MatrixMath.BEMatrix(Y.Rows-numberOfExcludedSpectraOfGroup,Y.Columns);
          XU = new MatrixMath.BEMatrix(numberOfExcludedSpectraOfGroup,X.Columns);
          YU = new MatrixMath.BEMatrix(numberOfExcludedSpectraOfGroup,Y.Columns);
          prevNumExcludedSpectra = numberOfExcludedSpectraOfGroup;
        }


        // build a new x and y matrix with the group information
        // fill XX and YY with values
        for(int i=0,j=0;i<X.Rows;i++)
        {
          if(Array.IndexOf(spectralGroup,i)>=0) // if spectral group contains i
            continue; // Exclude this row from the spectra
          MatrixMath.SetRow(X,i,XX,j);
          MatrixMath.SetRow(Y,i,YY,j);
          j++;
        }

        // fill XU (unknown spectra) with values
        for(int i=0;i<spectralGroup.Length;i++)
        {
          int j = spectralGroup[i];
          MatrixMath.SetRow(X,j,XU,i); // x-unkown (unknown spectra)
          MatrixMath.SetRow(Y,j,YU,i); // y-unkown (unknown concentration)
        }


        // now do the analysis
        crossFunction(spectralGroup,XX,YY,XU,YU);
        

      } // for all groups

      // calculate the mean number of excluded spectras
      return ((double)X.Rows)/groups.Length;
    }