public CrossPRESSEvaluator(
   int[] spectralRegions,
   int numFactors,
   ICrossValidationGroupingStrategy groupingStrategy,
   SpectralPreprocessingOptions preprocessOptions,
   MultivariateRegression analysis
   )
   : base(spectralRegions,numFactors,groupingStrategy,preprocessOptions,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);
    }
    /// <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;
    }