ningshuxia
2022-12-12 e78f5936fee9ab4fff600515bb20a41a28f329c4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// <copyright file="LocallyWeightedRegression.cs" company="Math.NET">
// 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.
// </copyright>
 
using System;
using System.Collections.Generic;
using IStation.Numerics.LinearAlgebra;
 
namespace IStation.Numerics.LinearRegression
{
    public static class WeightedRegression
    {
        /// <summary>
        /// Weighted Linear Regression using normal equations.
        /// </summary>
        /// <param name="x">Predictor matrix X</param>
        /// <param name="y">Response vector Y</param>
        /// <param name="w">Weight matrix W, usually diagonal with an entry for each predictor (row).</param>
        public static Vector<T> Weighted<T>(Matrix<T> x, Vector<T> y, Matrix<T> w) where T : struct, IEquatable<T>, IFormattable
        {
            return x.TransposeThisAndMultiply(w*x).Cholesky().Solve(x.TransposeThisAndMultiply(w*y));
        }
 
        /// <summary>
        /// Weighted Linear Regression using normal equations.
        /// </summary>
        /// <param name="x">Predictor matrix X</param>
        /// <param name="y">Response matrix Y</param>
        /// <param name="w">Weight matrix W, usually diagonal with an entry for each predictor (row).</param>
        public static Matrix<T> Weighted<T>(Matrix<T> x, Matrix<T> y, Matrix<T> w) where T : struct, IEquatable<T>, IFormattable
        {
            return x.TransposeThisAndMultiply(w*x).Cholesky().Solve(x.TransposeThisAndMultiply(w*y));
        }
 
        /// <summary>
        /// Weighted Linear Regression using normal equations.
        /// </summary>
        /// <param name="x">Predictor matrix X</param>
        /// <param name="y">Response vector Y</param>
        /// <param name="w">Weight matrix W, usually diagonal with an entry for each predictor (row).</param>
        /// <param name="intercept">True if an intercept should be added as first artificial predictor value. Default = false.</param>
        public static T[] Weighted<T>(T[][] x, T[] y, T[] w, bool intercept = false) where T : struct, IEquatable<T>, IFormattable
        {
            var predictor = Matrix<T>.Build.DenseOfRowArrays(x);
            if (intercept)
            {
                predictor = predictor.InsertColumn(0, Vector<T>.Build.Dense(predictor.RowCount, Vector<T>.One));
            }
 
            var response = Vector<T>.Build.Dense(y);
            var weights = Matrix<T>.Build.Diagonal(w);
            return predictor.TransposeThisAndMultiply(weights*predictor).Cholesky().Solve(predictor.TransposeThisAndMultiply(weights*response)).ToArray();
        }
 
        /// <summary>
        /// Weighted Linear Regression using normal equations.
        /// </summary>
        /// <param name="samples">List of sample vectors (predictor) together with their response.</param>
        /// <param name="weights">List of weights, one for each sample.</param>
        /// <param name="intercept">True if an intercept should be added as first artificial predictor value. Default = false.</param>
        public static T[] Weighted<T>(IEnumerable<Tuple<T[], T>> samples, T[] weights, bool intercept = false) where T : struct, IEquatable<T>, IFormattable
        {
            var xy = samples.UnpackSinglePass();
            return Weighted(xy.Item1, xy.Item2, weights, intercept);
        }
 
        /// <summary>
        /// Locally-Weighted Linear Regression using normal equations.
        /// </summary>
        [Obsolete("Warning: This function is here to stay but its signature will likely change. Opting out from semantic versioning.")]
        public static Vector<T> Local<T>(Matrix<T> x, Vector<T> y, Vector<T> t, double radius, Func<double, T> kernel) where T : struct, IEquatable<T>, IFormattable
        {
            // TODO: Weird kernel definition
            var w = Matrix<T>.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);
        }
 
        /// <summary>
        /// Locally-Weighted Linear Regression using normal equations.
        /// </summary>
        [Obsolete("Warning: This function is here to stay but its signature will likely change. Opting out from semantic versioning.")]
        public static Matrix<T> Local<T>(Matrix<T> x, Matrix<T> y, Vector<T> t, double radius, Func<double, T> kernel) where T : struct, IEquatable<T>, IFormattable
        {
            // TODO: Weird kernel definition
            var w = Matrix<T>.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);
        }
    }
}