コード例 #1
0
        public void MonotoneSplineTest()
        {
            ClString name = "MonotoneSplineTest";

            BeginTest(name);
            var test = new MonotoneSplineTest(Log, Context, name, ConsoleOutput);

            // Testing data: CallGeomBrownian; f(x) = x, F(x) = x^4 + 2*x^3 + 5*x*x - 6*x + 7,
            // f(x) = sin(x), f(x) = 1/2 (x-cos(x) sin(x)), f(x) = x+e^-x, etc.

            foreach (ClString testName in test.GetInputFileNames(GetType().FullName))
            {
                test.StartTest(testName);
                // Read input data.
                ClString  fileName = testName + ".csv";
                ClPoint[] data, reference;
                test.ReadCSVInputData(fileName, out data, out reference);
                // Initialise spline and calculate spline coefficients.
                test.Print(String.Format("Calculating spline."));
                var monotoneSpline = new ClMonotoneSpline1D();
                test.CalculationTime.Restart();
                monotoneSpline.LoadData(data);
                monotoneSpline.Calculate();
                test.CalculationTime.Stop();
                var gridData = monotoneSpline.ValueOnGrid(0.01);
                var dat      = new ClPoint[gridData.RowCount];
                for (var i = 0; i < dat.Length; i++)
                {
                    dat[i] = new ClPoint(gridData[i, 0], gridData[i, 1]);
                }
                var smoothingSpline = new ClSplineCubicSmooth1D(new Spline1DBuilder());
                smoothingSpline.LoadData(data);
                smoothingSpline.Calculate();
                if (PlotResults)
                {
                    test.StoreSpline1D(smoothingSpline, monotoneSpline, data, reference);
                }
                Log.Result(String.Format(name + " for " + testName + " completed." + "{0}" +
                                         "Number of points: " + data.Length + ";{0}" +
                                         "Processing time: " + test.CalculationTime.ElapsedMilliseconds + ";{0}" +
                                         "Deviation between spline and reference: " + ClApproximUtils.DeviationBetween(monotoneSpline, reference) + ";{0}", Environment.NewLine));
            }
            Log.Result(name + " completed");
            test.FinishTest(PrintPerformance);
        }
コード例 #2
0
        public void ConvexSplineTest()
        {
            ClString name = "ConvexSplineTest";

            BeginTest(name);
            const double convexityConstraint = 0.5;
            const double step = 0.1;
            var          test = new ConvexSplineTest(Log, Context, name, ConsoleOutput);

            // Testing data: CallGeomBrownian; FunctionExp(X), FunctionXPow2, FunctionXPow4; PayoffCallOption.

            foreach (ClString testName in test.GetInputFileNames(GetType().FullName))
            {
                test.StartTest(testName);
                // Read input data.
                ClString  fileName = testName + ".csv";
                ClPoint[] data, reference;
                test.ReadCSVInputData(fileName, out data, out reference);
                // Initialise spline and calculate spline coefficients.
                test.Print(String.Format("Calculating spline."));

                var convexSpline = new ClConvexSpline1D
                {
                    ConvexityConstraint = convexityConstraint,
                    Step = step
                };
                convexSpline.LoadData(data);
                convexSpline.Extrapolate = false;
                test.CalculationTime.Restart();
                convexSpline.Calculate();
                test.CalculationTime.Stop();
                var dat             = convexSpline.GridData(step);
                var smoothingSpline = new ClSplineCubicSmooth1D(new Spline1DBuilder());
                smoothingSpline.LoadData(data);
                smoothingSpline.Calculate();
                if (PlotResults)
                {
                    test.StoreSpline1D(smoothingSpline, convexSpline, data, reference);
                }
                Log.Result(String.Format(name + " for " + testName + " completed." + "{0}" +
                                         "Number of points: " + data.Length + ";{0}" +
                                         "Processing time: " + test.CalculationTime.ElapsedMilliseconds + ";{0}" +
                                         "Deviation between spline and reference: " + ClApproximUtils.DeviationBetween(convexSpline, reference) + ";{0}" +
                                         "Convex measure: " + convexSpline.ConvexMeasure(dat) + ";{0}", Environment.NewLine));
                test.CalculationTime.Reset();
            }
            Log.Result(name + " completed");
            test.FinishTest(PrintPerformance);
        }