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); }
public void Playground() { //Implementation of this example: https://cassowary.readthedocs.org/en/latest/topics/examples.html#quadrilaterals var _0 = new ClPoint(10, 10); var _1 = new ClPoint(10, 200); var _2 = new ClPoint(200, 200); var _3 = new ClPoint(200, 10); var m0 = new ClPoint(0, 0); var m1 = new ClPoint(0, 0); var m2 = new ClPoint(0, 0); var m3 = new ClPoint(0, 0); //We don't want the points to move unless necessary _solver.AddPointStays(new[] { _0, _1, _2, _3 }); //Define the midpoints // ReSharper disable CompareOfFloatsByEqualityOperator _solver.AddConstraint(m0.X, _0.X, _1.X, (m, a, b) => m == a * 0.5 + b * 0.5); _solver.AddConstraint(m0.Y, _0.Y, _1.Y, (m, a, b) => m == a * 0.5 + b * 0.5); _solver.AddConstraint(m1.X, _1.X, _2.X, (m, a, b) => m == a * 0.5 + b * 0.5); _solver.AddConstraint(m1.Y, _1.Y, _2.Y, (m, a, b) => m == a * 0.5 + b * 0.5); _solver.AddConstraint(m2.X, _2.X, _3.X, (m, a, b) => m == a * 0.5 + b * 0.5); _solver.AddConstraint(m2.Y, _2.Y, _3.Y, (m, a, b) => m == a * 0.5 + b * 0.5); _solver.AddConstraint(m3.X, _3.X, _0.X, (m, a, b) => m == a * 0.5 + b * 0.5); _solver.AddConstraint(m3.Y, _3.Y, _0.Y, (m, a, b) => m == a * 0.5 + b * 0.5); // ReSharper restore CompareOfFloatsByEqualityOperator //Make sure left stays left and top stays top _solver.AddConstraint(_0.X, _2.X, (a, b) => a + 20 <= b); _solver.AddConstraint(_0.X, _3.X, (a, b) => a + 20 <= b); _solver.AddConstraint(_1.X, _2.X, (a, b) => a + 20 <= b); _solver.AddConstraint(_1.X, _3.X, (a, b) => a + 20 <= b); _solver.AddConstraint(_0.Y, _1.Y, (a, b) => a + 20 <= b); _solver.AddConstraint(_0.Y, _2.Y, (a, b) => a + 20 <= b); _solver.AddConstraint(_3.Y, _1.Y, (a, b) => a + 20 <= b); _solver.AddConstraint(_3.Y, _2.Y, (a, b) => a + 20 <= b); //Make sure all points stay in 500x500 convas _solver.AddConstraint(_0.X, a => a >= 0); _solver.AddConstraint(_0.Y, a => a >= 0); _solver.AddConstraint(_0.X, a => a <= 500); _solver.AddConstraint(_0.Y, a => a <= 500); Console.WriteLine(m0.X.Value + " " + m0.Y.Value); Console.WriteLine(m1.X.Value + " " + m1.Y.Value); Console.WriteLine(m2.X.Value + " " + m2.Y.Value); Console.WriteLine(m3.X.Value + " " + m3.Y.Value); _solver.BeginEdit(_2.X, _2.Y) .SuggestValue(_2.X, 300) .SuggestValue(_2.Y, 400) .EndEdit(); Console.WriteLine(m0.X.Value + " " + m0.Y.Value); Console.WriteLine(m1.X.Value + " " + m1.Y.Value); Console.WriteLine(m2.X.Value + " " + m2.Y.Value); Console.WriteLine(m3.X.Value + " " + m3.Y.Value); }
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); }