using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using HeuristicLab.Algorithms.DataAnalysis.Experimental; using HeuristicLab.Problems.DataAnalysis; namespace Main { class Program { static void Main(string[] args) { var xs = HeuristicLab.Common.SequenceGenerator.GenerateSteps(-3.5, 3.5, 0.1, includeEnd: true).ToList(); var ys = xs.Select(xi => 1.0 / Math.Sqrt(2 * Math.PI) * Math.Exp(-0.5 * xi * xi)).ToArray(); // 1.0 / (Math.Sqrt(2 * Math.PI) * Math.Exp(-0.5 * xi * xi))).ToArray(); int n = xs.Count(); alglib.hqrndstate state; alglib.hqrndseed(1234, 5678, out state); var ys_noise = ys.Select(yi => yi + alglib.hqrndnormal(state) * 0.1).ToList(); CubicSplineGCV.CubGcvReport report; var model = CubicSplineGCV.CalculateCubicSpline( xs.ToArray(), ys_noise.ToArray(), "y", new string[] { "x" }, out report); Console.WriteLine("Smoothing Parameter (= RHO/(RHO + 1) {0}", report.smoothingParameter); Console.WriteLine("Estimated DOF of RSS {0}", report.estimatedRSSDegreesOfFreedom); Console.WriteLine("GCV {0}", report.generalizedCrossValidation); Console.WriteLine("Mean squared residual {0}", report.meanSquareResiudal); Console.WriteLine("Estimate of true MSE at data points {0}", report.estimatedTrueMeanSquaredErrorAtDataPoints); Console.WriteLine("Estimate of error variance {0}", report.estimatedErrorVariance); Console.WriteLine("Mean square value of DF(I) {0}", report.meanSquareOfDf); OnlineCalculatorError error; var ys_smoothed = xs.Select(xi => model.GetEstimatedValue(xi)).ToArray(); var mse = OnlineMeanSquaredErrorCalculator.Calculate(ys, ys_smoothed, out error); Console.WriteLine("MSE(ys, ys_smooth) = {0}", mse); mse = OnlineMeanSquaredErrorCalculator.Calculate(ys, ys_noise, out error); Console.WriteLine("MSE(ys, ys_noise) = {0}", mse); Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; for (int i = 0; i < n; i++) { Console.WriteLine("{0}\t{1}\t{2}\t{3}\t{4}\t{5}", xs[i], ys[i], ys_smoothed[i], ys_noise[i], ys_smoothed[i] + 1.96 * report.se[i], ys_smoothed[i] - 1.96 * report.se[i]); } } } }