public void logarithm_learn()
{
#region doc_learn
// This is the same data from the example available at
// http://mathbits.com/MathBits/TISection/Statistics2/logarithmic.htm
// Declare your inputs and output data
double[] inputs = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 };
// Transform inputs to logarithms
double[] logx = Matrix.Log(inputs);
// Use Ordinary Least Squares to learn the regression
OrdinaryLeastSquares ols = new OrdinaryLeastSquares();
// Use OLS to learn the simple linear regression
SimpleLinearRegression lr = ols.Learn(logx, outputs);
// Compute predicted values for inputs
double[] predicted = lr.Transform(logx);
// Get an expression representing the learned regression model
// We just have to remember that 'x' will actually mean 'log(x)'
string result = lr.ToString("N4", CultureInfo.InvariantCulture);
// Result will be "y(x) = 6.1082x + 6.0993"
// The mean squared error between the expected and the predicted is
double error = new SquareLoss(outputs).Loss(predicted); // 0.261454
#endregion
Assert.AreEqual(0.26145460024250794, error, 1e-8);
Assert.AreEqual(6.1081800414945704, lr.Slope, 1e-8);
Assert.AreEqual(6.0993411396126653, lr.Intercept, 1e-8);
Assert.AreEqual("y(x) = 6.1082x + 6.0993", result);
}
}