← All NMath Code Examples
using System;
using CenterSpace.NMath.Core;
namespace CenterSpace.NMath.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 constituent 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 )
{
var 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
var A = new DoubleMatrix( n, p, rng ); // spectral absorbances
var C = new DoubleVector( n, rng ); // constituent concentrations
var pls = new PLS1();
Console.WriteLine();
Console.WriteLine( "Performing calculation..." );
pls.Calculate( A, C, f );
Console.WriteLine();
Console.WriteLine( "Is it good? {0}\n", pls.IsGood );
Console.WriteLine();
var alg = (PLS1NipalsAlgorithm) pls.Calculator;
// Get the spectral scores
DoubleMatrix S = alg.Scores;
Console.WriteLine();
Console.WriteLine( "Spectral Scores ------------------------------\n" );
for ( int i = 0; i < f; ++i )
{
Console.WriteLine( "spectral score {0}\n{1}\n", i, S.Col( i ).ToString( "G5" ) );
}
Console.WriteLine();
// Get spectral loadings
DoubleMatrix Bx = alg.Loadings;
Console.WriteLine();
Console.WriteLine( "Spectral Loadings ------------------------------\n" );
for ( int i = 0; i < f; ++i )
{
Console.WriteLine( "spectral loading {0}\n{1}\n", i, Bx.Col( i ).ToString( "G5" ) );
}
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.
var 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();
Console.WriteLine( "Prediction Using Regression Vector ------------------" );
Console.WriteLine();
Console.WriteLine( "Predicted concentrations for spectral response \n\n{0} \n\nis \n\n{1}", Au.ToString( "G5" ), Cu );
Console.WriteLine();
Console.WriteLine( "Press Enter Key" );
Console.Read();
}
}
}
← All NMath Code Examples