示例#1
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);
        }
示例#2
0
        public void SplineSlopeValueTest()
        {
            ClString name = "SplineSlopeValueTest";

            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", "Value"))
            {
                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]);
                }

                // Read end values.
                double valueLeft  = 0;
                double valueRight = 0;
                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.
                var builder = new Spline1DBuilder
                {
                    ValueLeft  = valueLeft,
                    ValueRight = valueRight,
                    SlopeLeft  = slopeLeft,
                    SlopeRight = slopeRight
                };
                var spline = new ClSplineCubicSmoothSlopeValue1D(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);
        }
示例#3
0
        public void SplineSlopeBoundTest()
        {
            ClString name = "SplineSlopeBoundTest";

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

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

                // Read slope.
                // 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]);
                }

                // Read bounds.
                ClDouble boundMin;
                ClDouble boundMax;
                try
                {
                    fileName = testName + "Bounds.csv";
                    var matrix = new ClCsvMatrix(Context.AsFileContext.LoadText(fileName));
                    boundMin = matrix[0, 0].ToDouble();
                    boundMax = matrix[0, 1].ToDouble();
                }
                catch (Exception)
                {
                    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,
                    SlopeLeft  = slopeLeft,
                    SlopeRight = slopeRight
                };
                var spline = new ClSplineCubicSmoothSlope1D(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);
        }
示例#4
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);
        }