Beispiel #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);
        }
Beispiel #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);
        }
Beispiel #3
0
        public void SplineOptimalSmoothTest()
        {
            ClString name = "SplineOptimalSmoothTest";

            BeginTest(name);

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

            var test = new ApproximationTestsHelper(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);

                // Initialise spline and calculate spline coefficients.
                var spline = new ClSplineCubicOptimalSmooth1D(new Spline1DBuilder())
                {
                    OptimSteer =
                    {
                        StartSeed = StartSeed,
                        Method    = ClSplineCubicOptimalSmooth1D.OptimizationSteering.UseMethod.CvGoldenSection
                    }
                };
                // TODO Switch to CvGoldenSection, CvScan, NcpScan or NcpGoldenSection optimization.
                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);
        }
Beispiel #4
0
        public void SplineTest()
        {
            ClString name = "SplineTest";

            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))
            {
                test.StartTest(testName);

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

                ClSplineCubicSmooth1D spline = new ClSplineCubicSmooth1D(new Spline1DBuilder());
                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);
        }
Beispiel #5
0
        public void AMCRegressionTest()
        {
            ClString name = "AMCRegressionTest";

            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))
            {
                test.StartTest(testName);

                // Read input data.
                ClString  fileName = testName + ".csv";
                ClPoint[] data, reference;
                test.ReadCSVInputData(fileName, out data, out reference);
                // initial data for polynomial regression
                double[] dataX = new double[data.Length];
                for (int pos = 0; pos < data.Length; pos++)
                {
                    dataX[pos] = data[pos].X[0];
                }
                ClDoubleArray explanatoryVariables = new ClDoubleArray(dataX);
                double[]      dataV = new double[data.Length];
                for (int pos = 0; pos < data.Length; pos++)
                {
                    dataV[pos] = data[pos].Value;
                }
                ClDoubleArray exposureDiscounted = new ClDoubleArray(dataV);
                // Initialise polynomial regression and calculate it
                var amcBuilder = new ClPolynomialLsqAmcBuilder
                {
                    Sdf = new ClDoubleArray(data.Length, 1.0),
                    ExplanatoryVariables = explanatoryVariables,
                    PolynomialPower      = 3
                };
                IClAmc amc = amcBuilder.Build <IClAmc>();

                test.CalculationTime.Restart();
                ClDoubleArray result = new ClDoubleArray(amc.Evaluate(exposureDiscounted));
                test.CalculationTime.Stop();

                ClPoint[] valuesAt = new ClPoint[data.Length];
                for (int pos = 0; pos < data.Length; pos++)
                {
                    valuesAt[pos] = new ClPoint(data[pos].X, result[pos]);
                }

                // Initialise spline and calculate spline coefficients.
                var spline = new ClSplineCubicSmooth1D(new Spline1DBuilder());
                spline.LoadData(data);
                spline.Calculate();

                // Store results and complete test.
                if (PlotResults)
                {
                    test.StoreSpline1D(spline, valuesAt, data, reference);
                }
                test.Print("Completed " + name + " for " + testName);
            }
            test.FinishTest(PrintPerformance);
        }
Beispiel #6
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);
        }