コード例 #1
0
        public ExponentialViewModel(double[] xdata, double[] ydata, Tuple <double, double> xlimit, Tuple <double, double> ylimit)
        {
            displayed = true;
            DirectRegressionMethod method = DirectRegressionMethod.QR;
            Tuple <double, double> p      = Fit.Exponential(xdata, ydata, method);
            double a = p.Item1;
            double r = p.Item2;

            if (!Double.IsNaN(a) && !Double.IsNaN(r))
            {
                this.FunctionModel = new PlotModel {
                    Title = "Exponential regresion [ f(x) = " + a + " * exp( " + r + " * x ) ]"
                };
                Func <double, double> exponentialFunction = (x) => a *Math.Exp(r *x);

                FunctionModel.Series.Add(new FunctionSeries(exponentialFunction, xlimit.Item1, xlimit.Item2, 0.0001));
                // view limits
                FunctionModel.Axes.Add(new LinearAxis {
                    Position = AxisPosition.Bottom, Minimum = xlimit.Item1, Maximum = xlimit.Item2
                });
                FunctionModel.Axes.Add(new LinearAxis {
                    Position = AxisPosition.Left, Minimum = ylimit.Item1, Maximum = ylimit.Item2
                });
            }
            else
            {
                displayed = false;
            }
        }
コード例 #2
0
 /// <summary>
 /// Performs an exponential regression.
 /// </summary>
 /// <param name="x">x data</param>
 /// <param name="y">y data</param>
 /// <param name="method">Regression method to use.</param>
 public static double[] ExponentialFit(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     double[] yHat   = Generate.Map(y, Math.Log);
     double[] pHat   = Fit.LinearCombination(x, yHat, method, t => 1.0, t => t);
     double[] coeffs = { Math.Exp(pHat[0]), pHat[1] };
     return(Generate.Map(x, xi => coeffs[0] * Math.Exp(coeffs[1] * xi)));
 }
コード例 #3
0
        public PowerViewModel(double[] xdata, double[] ydata, Tuple <double, double> xlimit, Tuple <double, double> ylimit)
        {
            DirectRegressionMethod method = DirectRegressionMethod.QR;
            Tuple <double, double> p      = Fit.Power(xdata, ydata, method);
            double a = p.Item1;
            double b = p.Item2;

            if (!Double.IsNaN(a) && !Double.IsNaN(b))
            {
                this.FunctionModel = new PlotModel {
                    Title = "Power regresion [ f(x) = " + a + " x ^ " + b + " ]"
                };
                Func <double, double> powerFunction = (x) => a *Math.Pow(x, b);

                FunctionModel.Series.Add(new FunctionSeries(powerFunction, xlimit.Item1, xlimit.Item2, 0.0001));
                // view limits
                FunctionModel.Axes.Add(new LinearAxis {
                    Position = AxisPosition.Bottom, Minimum = xlimit.Item1, Maximum = xlimit.Item2
                });
                FunctionModel.Axes.Add(new LinearAxis {
                    Position = AxisPosition.Left, Minimum = ylimit.Item1, Maximum = ylimit.Item2
                });
            }
            else
            {
                displayed = false;
            }
        }
コード例 #4
0
 /// <summary>
 /// Least-Squares fitting the points (x,y) to an exponential y : x -> a*exp(r*x),
 /// returning its best fitting parameters as (a, r) tuple.
 /// </summary>
 public static Tuple <double, double> Exponential(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     // Transformation: y_h := ln(y) ~> y_h : x -> ln(a) + r*x;
     double[] lny = Generate.Map(y, Math.Log);
     double[] p   = LinearCombination(x, lny, method, t => 1.0, t => t);
     return(Tuple.Create(Math.Exp(p[0]), p[1]));
 }
コード例 #5
0
ファイル: Fit.cs プロジェクト: stjordanis/mathnet-numerics
 /// <summary>
 /// Least-Squares fitting the points (x,y) to a power y : x -> a*x^b,
 /// returning its best fitting parameters as (a, b) tuple.
 /// </summary>
 public static (double A, double B) Power(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     // Transformation: y_h := ln(y) ~> y_h : x -> ln(a) + b*ln(x);
     double[] lny = Generate.Map(y, Math.Log);
     double[] p   = LinearCombination(x, lny, method, t => 1.0, Math.Log);
     return(Math.Exp(p[0]), p[1]);
 }
コード例 #6
0
        /// <summary>
        /// Least-Squares fitting the points (x,y) to a logarithm y : x -> a + b*ln(x),
        /// returning a function y' for the best fitting line.
        /// </summary>
        public static Func <double, double> LogarithmFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
        {
            var parameters = Logarithm(x, y, method);
            var a          = parameters.Item1;
            var b          = parameters.Item2;

            return(z => a + b * Math.Log(z));
        }
コード例 #7
0
        /// <summary>
        /// Least-Squares fitting the points (x,y) to a power y : x -> a*x^b,
        /// returning a function y' for the best fitting line.
        /// </summary>
        public static Func <double, double> PowerFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
        {
            var parameters = Power(x, y, method);
            var a          = parameters.Item1;
            var b          = parameters.Item2;

            return(z => a *Math.Pow(z, b));
        }
コード例 #8
0
        /// <summary>
        /// Least-Squares fitting the points (x,y) to an exponential y : x -> a*exp(r*x),
        /// returning a function y' for the best fitting line.
        /// </summary>
        public static Func <double, double> ExponentialFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
        {
            var parameters = Exponential(x, y, method);
            var a          = parameters.Item1;
            var r          = parameters.Item2;

            return(z => a *Math.Exp(r *z));
        }
コード例 #9
0
        /// <summary>
        /// Find the model parameters β such that their linear combination with all predictor-arrays in X become as close to their response in Y as possible, with least squares residuals.
        /// </summary>
        /// <param name="x">List of predictor-arrays.</param>
        /// <param name="y">List of responses</param>
        /// <param name="intercept">True if an intercept should be added as first artificial predictor value. Default = false.</param>
        /// <param name="method">The direct method to be used to compute the regression.</param>
        /// <returns>Best fitting list of model parameters β for each element in the predictor-arrays.</returns>
        public static T[] DirectMethod <T>(T[][] x, T[] y, bool intercept = false, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations) where T : struct, IEquatable <T>, IFormattable
        {
            switch (method)
            {
            case DirectRegressionMethod.NormalEquations:
                return(NormalEquations(x, y, intercept));

            case DirectRegressionMethod.QR:
                return(QR(x, y, intercept));

            case DirectRegressionMethod.Svd:
                return(Svd(x, y, intercept));

            default:
                throw new NotSupportedException(method.ToString());
            }
        }
コード例 #10
0
        /// <summary>
        /// Find the model parameters β such that X*β with predictor X becomes as close to response Y as possible, with least squares residuals.
        /// </summary>
        /// <param name="x">Predictor matrix X</param>
        /// <param name="y">Response matrix Y</param>
        /// <param name="method">The direct method to be used to compute the regression.</param>
        /// <returns>Best fitting vector for model parameters β</returns>
        public static Matrix <T> DirectMethod <T>(Matrix <T> x, Matrix <T> y, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations) where T : struct, IEquatable <T>, IFormattable
        {
            switch (method)
            {
            case DirectRegressionMethod.NormalEquations:
                return(NormalEquations(x, y));

            case DirectRegressionMethod.QR:
                return(QR(x, y));

            case DirectRegressionMethod.Svd:
                return(Svd(x, y));

            default:
                throw new NotSupportedException(method.ToString());
            }
        }
コード例 #11
0
        public void Analyze(List <ReceivedMeasurement> measurements, AnalysisVM viewmodel)
        {
            var measurementDates = measurements.Select(x => x.RecordCreateTime.ToOADate()).ToArray();

            DirectRegressionMethod regressionMethod = (DirectRegressionMethod)Enum.Parse(typeof(DirectRegressionMethod), viewmodel.regressionMethod);

            var polynomialCoefficients = Fit.Polynomial(measurementDates,
                                                        measurements.Select(y => y.Value).ToArray(), viewmodel.polynomialDegree, regressionMethod).ToList();

            var functionFormula = GetPolynomialFormula(polynomialCoefficients);

            var newDates = GenerateNewDates(measurementDates);

            var approximationResults = GetApproximationResults(newDates.ToArray(), polynomialCoefficients);

            _result = new ComputationResultVM()
            {
                X_values  = measurementDates.Select(x => DateTime.FromOADate(x).ToString("yyyy-MM-dd HH:mm:ss")).ToList(),
                X2_values = newDates.Select(x => DateTime.FromOADate(x).ToString("yyyy-MM-dd HH:mm:ss")).ToList(),
                Y_values  = measurements.Select(x => x.Value).ToList(),
                Y2_values = approximationResults,
                Formula   = functionFormula
            };
        }
コード例 #12
0
        public void Analyze(List <ReceivedMeasurement> measurements, AnalysisVM viewmodel)
        {
            var dates = measurements.Select(x => x.RecordCreateTime.ToOADate()).ToArray();

            DirectRegressionMethod regressionMethod = (DirectRegressionMethod)Enum.Parse(typeof(DirectRegressionMethod), viewmodel.regressionMethod);

            var polynomialCoefficients = Fit.Polynomial(dates,
                                                        measurements.Select(y => y.Value).ToArray(), viewmodel.polynomialDegree, regressionMethod).ToList();

            var predictionDate    = DateTime.Parse(viewmodel.predictionDate);
            var predictionResults = GetPredictionResults(predictionDate.ToOADate(), polynomialCoefficients);

            List <string> predictionDates = new List <string>()
            {
                viewmodel.predictionDate
            };

            _result = new ComputationResultVM()
            {
                X_values = predictionDates,
                Y_values = predictionResults,
                Formula  = ""
            };
        }
コード例 #13
0
        /// <summary>
        /// Least-Squares fitting the points (T,y) = (T,y) to an arbitrary linear combination y : X -> p0*f0(T) + p1*f1(T) + ... + pk*fk(T),
        /// returning a function y' for the best fitting combination.
        /// </summary>
        public static Func <T, double> LinearGenericFunc <T>(T[] x, double[] y, DirectRegressionMethod method, params Func <T, double>[] functions)
        {
            var parameters = LinearGeneric(x, y, method, functions);

            return(z => functions.Zip(parameters, (f, p) => p * f(z)).Sum());
        }
コード例 #14
0
        /// <summary>
        /// Least-Squares fitting the points (T,y) = (T,y) to an arbitrary linear combination y : X -> p0*f0(T) + p1*f1(T) + ... + pk*fk(T),
        /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array.
        /// </summary>
        public static double[] LinearGeneric <T>(T[] x, double[] y, DirectRegressionMethod method, params Func <T, double>[] functions)
        {
            var design = Matrix <double> .Build.Dense(x.Length, functions.Length, (i, j) => functions[j](x[i]));

            return(MultipleRegression.DirectMethod(design, Vector <double> .Build.Dense(y), method).ToArray());
        }
コード例 #15
0
        /// <summary>
        /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to an arbitrary linear combination y : X -> p0*f0(x) + p1*f1(x) + ... + pk*fk(x),
        /// returning a function y' for the best fitting combination.
        /// </summary>
        public static Func <double[], double> LinearMultiDimFunc(double[][] x, double[] y, DirectRegressionMethod method, params Func <double[], double>[] functions)
        {
            var parameters = LinearMultiDim(x, y, method, functions);

            return(z => functions.Zip(parameters, (f, p) => p * f(z)).Sum());
        }
コード例 #16
0
        /// <summary>
        /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk,
        /// returning a function y' for the best fitting combination.
        /// If an intercept is added, its coefficient will be prepended to the resulting parameters.
        /// </summary>
        public static Func <double[], double> MultiDimFunc(double[][] x, double[] y, bool intercept = false, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations)
        {
            var parameters = MultipleRegression.DirectMethod(x, y, intercept, method);

            return(z => LinearAlgebraControl.Provider.DotProduct(parameters, z));
        }
コード例 #17
0
 /// <summary>
 /// Least-Squares fitting the points (X,y) = ((x0,x1,..,xk),y) to a linear surface y : X -> p0*x0 + p1*x1 + ... + pk*xk,
 /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array.
 /// If an intercept is added, its coefficient will be prepended to the resulting parameters.
 /// </summary>
 public static double[] MultiDim(double[][] x, double[] y, bool intercept = false, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations)
 {
     return(MultipleRegression.DirectMethod(x, y, intercept, method));
 }
コード例 #18
0
        /// <summary>
        /// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k,
        /// returning a function y' for the best fitting polynomial.
        /// A polynomial with order/degree k has (k+1) coefficients and thus requires at least (k+1) samples.
        /// </summary>
        public static Func <double, double> PolynomialFunc(double[] x, double[] y, int order, DirectRegressionMethod method = DirectRegressionMethod.QR)
        {
            var parameters = Polynomial(x, y, order, method);

            return(z => Evaluate.Polynomial(z, parameters));
        }
コード例 #19
0
        //Супер функция регрессии
        public static void /*double[]*/ MultiDim(Tuple <double, double> x, double[] y, bool intercept = false, DirectRegressionMethod method = DirectRegressionMethod.NormalEquations)
        {
            //return SimpleRegression.Fit(x);
            Matrix <double> m = Matrix <double> .Build.Random(3, 4);

            m = m * m;
            m.Transpose();
            m.Inverse();
        }
コード例 #20
0
 /// <summary>
 /// Least-Squares fitting the points (x,y) to an exponential y : x -> a*exp(r*x),
 /// returning a function y' for the best fitting line.
 /// </summary>
 public static Func <double, double> ExponentialFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     (double a, double r) = Exponential(x, y, method);
     return(z => a *Math.Exp(r *z));
 }
コード例 #21
0
 /// <summary>
 /// Least-Squares fitting the points (x,y) to a logarithm y : x -> a + b*ln(x),
 /// returning a function y' for the best fitting line.
 /// </summary>
 public static Func <double, double> LogarithmFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     (double a, double b) = Logarithm(x, y, method);
     return(z => a + b * Math.Log(z));
 }
コード例 #22
0
        /// <summary>
        /// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k
        /// </summary>
        public static Polynomial Fit(double[] x, double[] y, int order, DirectRegressionMethod method = DirectRegressionMethod.QR)
        {
            var pArr = MathNet.Numerics.Fit.Polynomial(x, y, order, method);

            return(new Polynomial(pArr));
        }
コード例 #23
0
 /// <summary>
 /// Least-Squares fitting the points (x,y) to a power y : x -> a*x^b,
 /// returning a function y' for the best fitting line.
 /// </summary>
 public static Func <double, double> PowerFunc(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     (double a, double b) = Power(x, y, method);
     return(z => a *Math.Pow(z, b));
 }
コード例 #24
0
        /// <summary>
        /// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k,
        /// returning its best fitting parameters as [p0, p1, p2, ..., pk] array, compatible with Evaluate.Polynomial.
        /// A polynomial with order/degree k has (k+1) coefficients and thus requires at least (k+1) samples.
        /// </summary>
        public static double[] Polynomial(double[] x, double[] y, int order, DirectRegressionMethod method = DirectRegressionMethod.QR)
        {
            var design = Matrix <double> .Build.Dense(x.Length, order + 1, (i, j) => Math.Pow(x[i], j));

            return(MultipleRegression.DirectMethod(design, Vector <double> .Build.Dense(y), method).ToArray());
        }
コード例 #25
0
ファイル: Polynomial.cs プロジェクト: Organwolf/visAR-Merge
        /// <summary>
        /// Least-Squares fitting the points (x,y) to a k-order polynomial y : x -> p0 + p1*x + p2*x^2 + ... + pk*x^k
        /// </summary>
        public static Polynomial Fit(double[] x, double[] y, int order, DirectRegressionMethod method = DirectRegressionMethod.QR)
        {
            var coefficients = Numerics.Fit.Polynomial(x, y, order, method);

            return(new Polynomial(coefficients));
        }
コード例 #26
0
 private double[] Exponential(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     double[] y_hat = Generate.Map(y, Math.Log);
     double[] p_hat = Fit.LinearCombination(x, y_hat, method, t => 1.0, t => t);
     return(new[] { Math.Exp(p_hat[0]), p_hat[1] });
 }
コード例 #27
0
 /// <summary>
 /// Least-Squares fitting the points (x,y) to a logarithm y : x -> a + b*ln(x),
 /// returning its best fitting parameters as (a, b) tuple.
 /// </summary>
 public static Tuple <double, double> Logarithm(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     double[] lnx = Generate.Map(x, Math.Log);
     double[] p   = LinearCombination(lnx, y, method, t => 1.0, t => t);
     return(Tuple.Create(p[0], p[1]));
 }
コード例 #28
0
 /// <summary>
 /// Least-Squares fitting the points (x,y) to a logarithm y : x -> a + b*ln(x),
 /// returning its best fitting parameters as (a, b) tuple.
 /// </summary>
 public static (double A, double B) Logarithm(double[] x, double[] y, DirectRegressionMethod method = DirectRegressionMethod.QR)
 {
     double[] lnx = Generate.Map(x, Math.Log);
     double[] p   = LinearCombination(lnx, y, method, _ => 1.0, t => t);
     return(p[0], p[1]);
 }