public override RrFunction Add(RrFunction other) { var cst = other as ConstantRrFunction; if (cst != null) { return(RrFunctions.Constant(Value + cst.Value)); } var step = other as StepFunction; if (step != null) { return(StepFunction.Add(step, this)); } var spline = other as SplineInterpoler; if (spline != null) { return(SplineInterpoler.Add(spline, this)); } return(base.Add(other)); }
public void Mult() { var cubic1 = SplineInterpoler.BuildCubicSpline(new[] { 0.0, 1.0, 2.0, 3.0 }, new[] { 1.0, 0.22, -0.1, 0.1 }); var cubic2 = SplineInterpoler.BuildCubicSpline(new[] { -0.5, 1.1, 2.0, 5.0 }, new[] { 1.0, 0.22, -0.1, 0.1 }); var mult = cubic1 * cubic2; Assert.IsTrue(mult is SplineInterpoler); var testVals = new[] { -0.5, 0.0, 1.0, 1.1, 2.0, 3.0, 5.0 }; foreach (var x in testVals) { var err = Math.Abs(mult.Eval(x) - (cubic1.Eval(x) * cubic2.Eval(x))); Assert.AreEqual(err, 0.0); } var rand = new Random(251); for (int i = 0; i < 100; i++) { double x = -5.0 + 12.0 * rand.NextDouble(); var err = Math.Abs(mult.Eval(x) - (cubic1.Eval(x) * cubic2.Eval(x))); Assert.LessOrEqual(err, 18.0 * DoubleUtils.MachineEpsilon); } }
public void TestEval() { var abs = new[] { 0.0, 0.5, 0.99, 2.5 }; var vals = new[] { 5.0, 5.4, 3.1, 1.0 }; var stepFunc = SplineInterpoler.BuildCubicSpline(abs, vals); foreach (var i in Enumerable.Range(0, abs.Length - 1)) { Assert.AreEqual(stepFunc.Eval(abs[i]), vals[i]); } }
public static object CubicSpline(double[] abscissae, double[] values, double[] points) { return(FunctionRunnerUtils.Run("CubicSpline", () => { var interpoler = SplineInterpoler.BuildCubicSpline(abscissae, values); var result = new double[points.Length, 1]; for (int i = 0; i < points.Length; i++) { result[i, 0] = interpoler.Eval(points[i]); } return result; })); }
public static VolatilitySurface BuildInterpol(VolatilityMatrix volMatrix, MoneynessProvider moneyness) { double[] timePillars = volMatrix.Time[volMatrix.Pillars]; var varianceSlices = EnumerableUtils.For(0, timePillars.Length, i => { double t = timePillars[i]; double[] moneynessPillars = volMatrix.Strikes.Map(k => moneyness.Moneyness(t, k)); var varianceSlice = volMatrix.Vols.Row(i).Map(v => t * v * v); return((RrFunction)SplineInterpoler.BuildCubicSpline(moneynessPillars, varianceSlice)); }); var varianceInterpol = new VarianceInterpoler(timePillars, varianceSlices); return(new VolatilitySurface(volMatrix.Time, moneyness, varianceInterpol, varianceInterpol.BuildLocalVariance())); }
public override RrFunction Mult(RrFunction other) { var cst = other as ConstantRrFunction; if (cst != null) { return(ProductRrFunctions.Mult(this, cst)); } var exp = other as ExpRrFunction; if (exp != null) { return(ProductRrFunctions.Mult(exp, this)); } var step = other as StepFunction; if (step != null) { return(StepFunction.Mult(step, this)); } var spline = other as SplineInterpoler; if (spline != null) { return(SplineInterpoler.Mult(spline, this)); } var lc = other as LinearCombinationRrFunction; if (lc != null) { return(ProductRrFunctions.Mult(lc, this)); } return(base.Mult(other)); }
public static RrFunction LinearInterpolation(double[] abscissae, double[] values, double leftExtrapolationSlope = 0.0, double rightExtrapolationSlope = 0.0) { return(SplineInterpoler.BuildLinearSpline(abscissae, values, leftExtrapolationSlope, rightExtrapolationSlope)); }