public void TestConstructor() { // Check that a mismatch in the sizes of the array data is detected. _xArray = new double[2]; _xArray[0] = 274d; _xArray[1] = 365d; _yArray = new double[3]; _yArray[0] = 9.29d / 100.0d; _yArray[1] = 9.46d / 100.0d; _yArray[2] = 10.34d / 100.0d; try { _interpObj = new CubicHermiteSplineInterpolation(); _interpObj.Initialize(_xArray, _yArray); } catch (System.Exception e) { const string errorMessage = "Cubic Hermite Spline interpolation needs equal length arrays"; Assert.AreEqual(errorMessage, e.Message); } // Check that the minimum array sizes is detected. _xArray = new double[1]; _yArray = new double[1]; try { _interpObj = new CubicHermiteSplineInterpolation(); _interpObj.Initialize(_xArray, _yArray); } catch (System.Exception e) { const string errorMessage = "Cubic Hermite Spline interpolation needs at least two (x,y)"; Assert.AreEqual(errorMessage, e.Message); } // Check that unsorted x array is detected. _xArray = new double[2]; _yArray = new double[2]; try { _interpObj = new CubicHermiteSplineInterpolation(); _interpObj.Initialize(_xArray, _yArray); } catch (System.Exception e) { const string errorMessage = "Cubic Hermite Spline interpolation needs sorted x values"; Assert.AreEqual(errorMessage, e.Message); } }
/// <summary> /// Helper function used to initialise the private fields. /// </summary> /// <param name="volatilityCurve">The Bootstrap engine /// that contains the results of the bootstrap.</param> /// <param name="expiryInterpolationType">Type of the expiry /// interpolation. /// Example: Linear interpolation.</param> private void InitialisePrivateFields (VolatilityCurve volatilityCurve, ExpiryInterpolationType expiryInterpolationType) { // Initialise the Calculation Date. _calculationDate = volatilityCurve.GetBaseDate(); // Set the x and y arrays for the one dimensional interpolation. var results = volatilityCurve.BootstrapResults.Results; IDayCounter dayCountObj = Actual365.Instance; var tempXArray = new List <double>(); var tempYArray = new List <double>(); var count = 1; foreach (var expiry in results.Keys) { var timeToExpiry = dayCountObj.YearFraction (_calculationDate, expiry); tempXArray.Add(timeToExpiry); tempYArray.Add(decimal.ToDouble(results[expiry])); // Record the first and last time to expiry and available // bootstrap Caplet volatility. if (count == 1) { _firstExpiry = (decimal)timeToExpiry; _firstVolatility = results[expiry]; } _lastVolatility = results[expiry]; _lastExpiry = (decimal)timeToExpiry; ++count; } double[] xArray = tempXArray.ToArray(); double[] yArray = tempYArray.ToArray(); // Initialise the one dimensional interpolation object. switch (expiryInterpolationType) { case ExpiryInterpolationType.CubicHermiteSpline: _expiryInterpolationObj = new CubicHermiteSplineInterpolation(); _expiryInterpolationObj.Initialize(xArray, yArray); break; default: // Linear interpolation _expiryInterpolationObj = new LinearInterpolation(); _expiryInterpolationObj.Initialize(xArray, yArray); break; } }
/// <summary> /// Compute an interpolated value from a set of known (x,y) values by Cubic Hermite Spline interpolation. /// Extrapolation is NOT available. /// </summary> /// <param name="xArray">Array (row/column) of x-values.</param> /// <param name="yArray">Array (row/column) of y-values.</param> /// <param name="target">Value at which to compute the interpolation.</param> /// <returns></returns> public double CubicHermiteSplineInterpolate(double[] xArray, double[] yArray, double target) { if (xArray == null) { return(0); } if (yArray == null) { return(0); } var chsi = new CubicHermiteSplineInterpolation(); chsi.Initialize(xArray, yArray);//CubicSplineInterpolation.InterpolateAkima(xArray, yArray); return(chsi.ValueAt(target, true)); }
public void Initialisation() { _xArray = new[] { 274d, 365d, 732d, 1097d, 1462d, 1828d, 2556d, 3654d, 5480d }; _yArray = new[] { 9.28622644581371d / 100d, 9.45686973002003d / 100d, 10.3375d / 100d, 10.7525d / 100d, 10.8400d / 100d, 10.9325d / 100d, 10.9450d / 100d, 10.9900d / 100d, 11.0000d / 100d }; _x2Array = new decimal[] { 274, 365, 732, 1097, 1462, 1828, 2556, 3654, 5480 }; _y2Array = new[] { 9.28622644581371m / 100m, 9.45686973002003m / 100m, 10.3375m / 100m, 10.7525m / 100m, 10.8400m / 100m, 10.9325m / 100m, 10.9450m / 100m, 10.9900m / 100m, 11.0000m / 100m }; // Instantiate an instance of the class. _interpObj = new CubicHermiteSplineInterpolation(); _interpObj.Initialize(_x2Array, _y2Array); Assert.IsNotNull(_interpObj); _target = 400d; _tolerance = 1.0E-5d; }