//
// Math.NET Numerics, part of the Math.NET Project
// http://numerics.mathdotnet.com
// http://github.com/mathnet/mathnet-numerics
//
// Copyright (c) 2009-2013 Math.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections.Generic;
using IStation.Numerics.LinearAlgebra;
namespace IStation.Numerics.LinearRegression
{
public static class WeightedRegression
{
///
/// Weighted Linear Regression using normal equations.
///
/// Predictor matrix X
/// Response vector Y
/// Weight matrix W, usually diagonal with an entry for each predictor (row).
public static Vector Weighted(Matrix x, Vector y, Matrix w) where T : struct, IEquatable, IFormattable
{
return x.TransposeThisAndMultiply(w*x).Cholesky().Solve(x.TransposeThisAndMultiply(w*y));
}
///
/// Weighted Linear Regression using normal equations.
///
/// Predictor matrix X
/// Response matrix Y
/// Weight matrix W, usually diagonal with an entry for each predictor (row).
public static Matrix Weighted(Matrix x, Matrix y, Matrix w) where T : struct, IEquatable, IFormattable
{
return x.TransposeThisAndMultiply(w*x).Cholesky().Solve(x.TransposeThisAndMultiply(w*y));
}
///
/// Weighted Linear Regression using normal equations.
///
/// Predictor matrix X
/// Response vector Y
/// Weight matrix W, usually diagonal with an entry for each predictor (row).
/// True if an intercept should be added as first artificial predictor value. Default = false.
public static T[] Weighted(T[][] x, T[] y, T[] w, bool intercept = false) where T : struct, IEquatable, IFormattable
{
var predictor = Matrix.Build.DenseOfRowArrays(x);
if (intercept)
{
predictor = predictor.InsertColumn(0, Vector.Build.Dense(predictor.RowCount, Vector.One));
}
var response = Vector.Build.Dense(y);
var weights = Matrix.Build.Diagonal(w);
return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.TransposeThisAndMultiply(weights*response)).ToArray();
}
///
/// Weighted Linear Regression using normal equations.
///
/// List of sample vectors (predictor) together with their response.
/// List of weights, one for each sample.
/// True if an intercept should be added as first artificial predictor value. Default = false.
public static T[] Weighted(IEnumerable> samples, T[] weights, bool intercept = false) where T : struct, IEquatable, IFormattable
{
var xy = samples.UnpackSinglePass();
return Weighted(xy.Item1, xy.Item2, weights, intercept);
}
///
/// Locally-Weighted Linear Regression using normal equations.
///
[Obsolete("Warning: This function is here to stay but its signature will likely change. Opting out from semantic versioning.")]
public static Vector Local(Matrix x, Vector y, Vector t, double radius, Func kernel) where T : struct, IEquatable, IFormattable
{
// TODO: Weird kernel definition
var w = Matrix.Build.Dense(x.RowCount, x.RowCount);
for (int i = 0; i < x.RowCount; i++)
{
w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius));
}
return Weighted(x, y, w);
}
///
/// Locally-Weighted Linear Regression using normal equations.
///
[Obsolete("Warning: This function is here to stay but its signature will likely change. Opting out from semantic versioning.")]
public static Matrix Local(Matrix x, Matrix y, Vector t, double radius, Func kernel) where T : struct, IEquatable, IFormattable
{
// TODO: Weird kernel definition
var w = Matrix.Build.Dense(x.RowCount, x.RowCount);
for (int i = 0; i < x.RowCount; i++)
{
w.At(i, i, kernel(Distance.Euclidean(t, x.Row(i))/radius));
}
return Weighted(x, y, w);
}
[Obsolete("Warning: This function is here to stay but will likely be refactored and/or moved to another place. Opting out from semantic versioning.")]
public static double GaussianKernel(double normalizedDistance)
{
return Math.Exp(-0.5*normalizedDistance*normalizedDistance);
}
}
}