Exemple #1
0
        public void SplineSlopeConditionsTest()
        {
            ClString name = "SplineSlopeConditionsTest";

            BeginTest(name);

            // Testing data: CallGeomBrownian; Linear, Parabolic, Exponential Data; PayoffButterfly, PayoffCallOption.

            ApproximationTestsHelper test = new ApproximationTestsHelper(Log, Context, name, ConsoleOutput);

            foreach (ClString testName in test.GetInputFileNames(GetType().FullName, "Slope"))
            {
                test.StartTest(testName);

                // Read input data.
                ClString  fileName = testName + ".csv";
                ClPoint[] data, reference;
                test.ReadCSVInputData(fileName, out data, out reference);

                // Read slope.
                double slopeLeft  = 0;
                double slopeRight = 0;
                var    n          = reference.Length;
                try
                {
                    fileName = testName + "Slope.csv";
                    var matrix = new ClCsvMatrix(Context.AsFileContext.LoadText(fileName));
                    slopeLeft  = matrix[0, 0].ToDouble();
                    slopeRight = matrix[0, 1].ToDouble();
                }
                catch (Exception)
                {
                    // if slopes are not set, calculates slopes by first and last two points of reference points.
                    slopeLeft  = (reference[1].Value - reference[0].Value) / (reference[1].X[0] - reference[0].X[0]);
                    slopeRight = (reference[n - 1].Value - reference[n - 2].Value) / (reference[n - 1].X[0] - reference[n - 2].X[0]);
                }

                // Initialise spline and calculate spline coefficients.
                var spline = new ClSplineCubicSmoothSlope1D(new Spline1DBuilder())
                {
                    Params =
                    {
                        SlopeLeft  = slopeLeft,
                        SlopeRight = slopeRight
                    }
                };
                spline.LoadData(data);
                test.CalculationTime.Restart();
                spline.Calculate();
                test.CalculationTime.Stop();
                // Store results and complete test.
                test.Print(String.Format("Optimal lambda: {0}", spline.Params.SmoothParam));
                if (PlotResults)
                {
                    test.StoreSpline1D(spline, reference);
                }
                test.Print("Completed " + name + " for " + testName);
            }
            test.FinishTest(PrintPerformance);
        }
Exemple #2
0
        public void SplineEndValueTest()
        {
            ClString name = "SplineEndValueTest";

            BeginTest(name);

            // Testing data: Call, Put, Risk Reversal, Straddle, Strangle, Bull, Bear, Butterfly and Barrier (Down-and-in call, Down-and-out call, Up-and-in call, Up-and-out call ) Options.

            ApproximationTestsHelper test = new ApproximationTestsHelper(Log, Context, name, ConsoleOutput);

            foreach (ClString testName in test.GetInputFileNames(GetType().FullName, "Value"))
            {
                test.StartTest(testName);

                // Read input data.
                ClString  fileName = testName + ".csv";
                ClPoint[] data, reference;
                test.ReadCSVInputData(fileName, out data, out reference);

                // Read end values.
                double valueLeft  = 0;
                double valueRight = 0;
                var    n          = reference.Length;
                try
                {
                    fileName = testName + "Value.csv";
                    var matrix = new ClCsvMatrix(Context.AsFileContext.LoadText(fileName));
                    valueLeft  = matrix[0, 0].ToDouble();
                    valueRight = matrix[0, 1].ToDouble();
                }
                catch (Exception)
                {
                    // set reference values at end points if end values are not set.
                    valueLeft  = reference[0].Value;
                    valueRight = reference[n - 1].Value;
                }

                // Initialise spline and calculate spline coefficients.
                Spline1DBuilder builder = new Spline1DBuilder();
                builder.ValueLeft  = valueLeft;
                builder.ValueRight = valueRight;
                var spline = new ClSplineCubicSmoothEndValue1D(builder);

                spline.LoadData(data);
                test.CalculationTime.Restart();
                spline.Calculate();
                test.CalculationTime.Stop();

                // Store results and complete test.
                test.Print(String.Format("Optimal lambda: {0}", spline.Params.SmoothParam));
                if (PlotResults)
                {
                    test.StoreSpline1D(spline, reference);
                }
                test.Print("Completed " + name + " for " + testName);
            }
            test.FinishTest(PrintPerformance);
        }
Exemple #3
0
        /// <summary>Returns spline values on the grid with a specified step.</summary>
        public ClCsvMatrix ValueOnGrid(double nodeStep)
        {
            ClInt       n      = Convert.ToInt32(Math.Ceiling((Points[PointsNumber - 1] - Points[0]) / nodeStep));
            ClCsvMatrix matrix = new ClCsvMatrix(n, 2);

            for (ClInt i = 0; i < n; i++)
            {
                ClDouble x = Points[0] + i * nodeStep;
                matrix[i, 0] = x.ToVariant();
                matrix[i, 1] = ValueAt(x);
            }
            return(matrix);
        }
Exemple #4
0
        /// <summary>  Return ClCsvMatrix with all spline coefficients.  </summary>
        public ClCsvMatrix GetCoefficients()
        {
            ClCsvMatrix matrix = new ClCsvMatrix(splineSections_.Length - 1, 6);

            for (int i = 0; i < splineSections_.Length - 1; i++)
            {
                matrix[i, 0] = new ClDouble(splineSections_[i].X).ToVariant();
                matrix[i, 1] = new ClDouble(splineSections_[i + 1].X).ToVariant();
                matrix[i, 2] = new ClDouble(splineSections_[i].A).ToVariant();
                matrix[i, 3] = new ClDouble(splineSections_[i].B).ToVariant();
                matrix[i, 4] = new ClDouble(splineSections_[i].C).ToVariant();
                matrix[i, 5] = new ClDouble(splineSections_[i].D).ToVariant();
            }
            return(matrix);
        }
Exemple #5
0
        /// <summary>Return ClCsvMatrix with input data.</summary>
        public ClCsvMatrix GetInputData()
        {
            if (!IsInitialized)
            {
                throw new ClEx("Spline is not initialised.");
            }

            // Build matrix with input data.
            var matrix = new ClCsvMatrix(PointsNumber, 2);

            for (ClInt i = 0; i < PointsNumber; i++)
            {
                matrix[i, 0] = new ClDouble(Points[i]).ToVariant();
                matrix[i, 1] = new ClDouble(Values[i]).ToVariant();
            }
            return(matrix);
        }
Exemple #6
0
        /// <summary>Return ClCsvMatrix with fitted data.</summary>
        public ClCsvMatrix GetFittedData()
        {
            if (!IsCalculated)
            {
                throw new ClEx("Spline is not calculated.");
            }

            // Build matrix with fitted data.
            var matrix = new ClCsvMatrix(splineMath_.DataNumber, 2);

            for (ClInt i = 0; i < splineMath_.DataNumber; i++)
            {
                var x = Points[i];
                matrix[i, 0] = new ClDouble(x).ToVariant();
                matrix[i, 1] = new ClDouble(ValueAt(x)).ToVariant();
            }
            return(matrix);
        }
Exemple #7
0
        public void KernelRegressionTest()
        {
            ClString name = "KernelRegressionTest";

            BeginTest(name);

            // Testing data: CallGeomBrownian; Linear, Parabolic, Exponential Data; PayoffButterfly, PayoffCallOption.

            NadarayaWatsonKernelRegressionTest test = new NadarayaWatsonKernelRegressionTest(Log, Context, name, ConsoleOutput);

            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);
                test.TotalPointsNumber += data.Length;

                // Initialise spline and calculate spline coefficients.
                test.CalculationTime.Restart();
                ClNadarayaWatsonKernelRegression spline = new ClNadarayaWatsonKernelRegression(new ClKernelRegressionParams());
                //spline.OptimSteer.StartSeed = startSeed;
                //// TODO Switch to CvGoldenSection, CvScan, NcpScan or NcpGoldenSection optimization.
                //spline.OptimSteer.Method = ClSplineCubicOptimalSmooth1D.OptimizationSteering.UseMethod.CvGoldenSection;
                spline.LoadData(data);
                //spline.Calculate();
                test.CalculationTime.Stop();
                Array.Sort(data, new ClPointComparerX());
                ClCsvMatrix gridData = test.ValueOnGrid(spline, data[0].X[0], data[data.Length - 1].X[0], 0.01);
                // Store results and complete test.
                if (PlotResults)
                {
                    test.StoreNadarayaWatson1D(spline, data, reference, gridData);
                }
                test.Print("Completed " + name + " for " + testName);
            }
            test.FinishTest(PrintPerformance);
        }
Exemple #8
0
        public void SplineBoundTest()
        {
            ClString name = "SplineBoundTest";

            BeginTest(name);

            // Testing data: CallGeomBrownian, PayoffButterfly, PayoffCallOption.

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

            foreach (ClString testName in test.GetInputFileNames(GetType().FullName, "Bounds"))
            {
                test.StartTest(testName);

                // Read input data.
                ClString  fileName = testName + ".csv";
                ClPoint[] data, reference;
                test.ReadCSVInputData(fileName, out data, out reference);

                // Read bounds.
                ClDouble   boundMin;
                ClDouble   boundMax;
                const bool fromFile = false;
                // TODO Switch to true to load bounds from file
                if (fromFile)
                {
                    fileName = testName + "Bounds.csv";
                    ClCsvMatrix matrix = new ClCsvMatrix(Context.AsFileContext.LoadText(fileName));
                    boundMin = matrix[0, 0].ToDouble();
                    boundMax = matrix[0, 1].ToDouble();
                }
                else
                {
                    var refer = new double[reference.Length];
                    for (var i = 0; i < refer.Length; i++)
                    {
                        refer[i] = reference[i].Value;
                    }
                    boundMin = refer.Min();
                    boundMax = refer.Max();
                }

                // Initialise spline and calculate spline coefficients.
                var splineParams = new Spline1DBuilder {
                    Min = boundMin, Max = boundMax
                };

                var spline = new ClSplineCubicSmooth1D(splineParams);
                spline.LoadData(data);
                test.CalculationTime.Restart();
                spline.Calculate();
                test.CalculationTime.Stop();

                // Store results and complete test.
                if (PlotResults)
                {
                    test.StoreSpline1D(spline, reference);
                }
                test.Print("Completed " + name + " for " + testName);
            }
            test.FinishTest(PrintPerformance);
        }