[TOC]
using System;
using CenterSpace.NMath.Core;
using CenterSpace.NMath.Stats;
namespace CenterSpace.NMath.Stats.Examples.CSharp
{
/// <summary>
/// This is a .NET example in C# showing Partial Least Squares (PLS) scores
/// and loadings.
/// In chemometrics one often wishes to know the chemical composition of a
/// sample of, say, a gas or liquid. One common technique is to study the absorption
/// spectrum of light passing through the sample. Partial Least Squares (PLS)
/// is often used to construct a predictive model in this situation. Suppose we
/// wish to measure the concentration of a constituant in the
/// substance and that we look at n different absorption spectra (an absorption
/// spectra measures the amount of light absorbed at each wavelength). Pick p
/// different wavelengths in the absorption spectra and let A = {aij} be the
/// be the nxp matrix where aij is the absorption measurement at the jth
/// wavelength in the ith sample. And let C = {ci} be the m vector where
/// ci is the concentration of the constituent in the ith sample.
/// This example constructs a predictive model for C given A using PLS. In
/// particular the scores and loadings for the response variable, C, and the
/// predictor variable, A, are examined.
/// </summary>
class PLS1ScoresAndLoadingsExample
{
static void Main( string[] args )
{
RandGenUniform rng = new RandGenUniform( 0x124 );
rng.LowerBound = 0.1;
rng.UpperBound = 1.1;
int n = 7; // number of samples (spectra)
int p = 10; // number of data points (wavelength)
int f = 4; // number of PLS eigenvectors
DoubleMatrix A = new DoubleMatrix( n, p, rng ); // spectral absorbances
DoubleVector C = new DoubleVector( n, rng ); // constituent concentrations
PLS1 pls = new PLS1();
Console.WriteLine("\nPerforming calculation...");
pls.Calculate( A, C, f );
Console.WriteLine();
Console.WriteLine("Is it good? {0}\n", pls.IsGood);
Console.WriteLine();
PLS1NipalsAlgorithm alg = (PLS1NipalsAlgorithm)pls.Calculator;
// Get the spectral scores
DoubleMatrix S = alg.Scores;
Console.WriteLine( "\nSpectral Scores ------------------------------\n" );
for ( int i = 0; i < f; ++i )
{
Console.WriteLine("spectral score {0}\n{1}\n", i, S.Col(i));
}
Console.WriteLine();
// Get spectral loadings
DoubleMatrix Bx = alg.Loadings;
Console.WriteLine( "\nSpectral Loadings ------------------------------\n" );
for (int i = 0; i < f; ++i)
{
Console.WriteLine( "spectral loading {0}\n{1}\n", i, Bx.Col( i ) );
}
Console.WriteLine();
// Predict the constituent concentrations from a vector of spectral responses.
// The regression vector, r, can be used for prediction as follows:
// Let Cu be the predicted concentration for the spectral response
// vector Au, let Abar the mean of the spectral data and Cbar be the mean
// of the concentrations data used to create the model. Then
// Cu = Cbar + (Au - Abar)'r.
DoubleVector Au = new DoubleVector( p, rng ); // spectral responses
double Cbar = alg.ResponseMean;
DoubleVector Abar = alg.PredictorMean;
DoubleVector r = alg.RegressionVector;
double Cu = Cbar + NMathFunctions.Dot( (Au - Abar), r );
Console.WriteLine( "\nPrediction Using Regression Vector ------------------" );
Console.WriteLine( "\nPredicted concentrations for spectral response \n\n{0} \n\nis \n\n{1}", Au, Cu );
Console.WriteLine();
Console.WriteLine("Press Enter Key");
Console.Read();
}
}
}
[TOC]