// Cap Price using VolCapletMatrix as argument public static double CapBlack(double[] T, double[] yf, double N, double K, BilinearInterpolator VolCapletMatrix, double[] df, double[] fwd) { // LINQ: getting interpolated vol double[] sigma = (from t in T select VolCapletMatrix.Solve(t, K)).ToArray(); return(CapBlack(T, yf, N, K, sigma, df, fwd)); }
public void ValueTest() { BilinearInterpolator target = new BilinearInterpolator(); IPoint point = new Point2D(4, 2, 0); List <IPoint> bounds = new List <IPoint>(); bounds.Add(new Point2D(2, 1, 9)); bounds.Add(new Point2D(6, 1, 13)); bounds.Add(new Point2D(2, 3, 7)); bounds.Add(new Point2D(6, 3, 11)); double expected = 10; double actual = target.Value(point, bounds); Assert.AreEqual(expected, actual); bounds = new List <IPoint>(); bounds.Add(new Point2D(2, 1, 3)); bounds.Add(new Point2D(6, 1, 7)); bounds.Add(new Point2D(2, 3, 5)); bounds.Add(new Point2D(6, 3, 9)); expected = 6; actual = target.Value(point, bounds); Assert.AreEqual(expected, actual); }
// 13.12.4 Bilinear Interpolation public static void BilinearInterpolation1() { Console.WriteLine("Vectors initialised: "); // Create mesh arrays int startIndex = 0; int N = 1; Vector <double> x1arr = new Vector <double>(N + 1, startIndex, 0.0); x1arr[0] = 20.0; x1arr[1] = 21.0; Vector <double> x2arr = new Vector <double>(x1arr); x2arr[0] = 14.0; x2arr[1] = 15.0; Console.WriteLine(x1arr); Console.WriteLine(x2arr); // Control values; NumericMatrix <double> Control = new NumericMatrix <double>(N + 1, N + 1, startIndex, startIndex); Control[0, 0] = 91.0; Control[1, 1] = 95.0; Control[0, 1] = 210.0; Control[1, 0] = 162.0; BilinearInterpolator myInterpolator = new BilinearInterpolator(x1arr, x2arr, Control); double x = 20.2; double y = 14.5; // 146.1 double value = myInterpolator.Solve(x, y); Console.WriteLine("Interpolated value: {0}", value); }
public void ValueTest1() { BilinearInterpolator target = new BilinearInterpolator(); // Create the point to interpolate at IPoint point = new Point2D(4, 2, 0); // Create the bounding points List <IPoint> bounds = new List <IPoint>(); bounds.Add(new Point2D(3, 1, 3)); bounds.Add(new Point2D(5, 1, 9)); bounds.Add(new Point2D(3, 3, 7)); bounds.Add(new Point2D(5, 3, 13)); // Set the expected value double expected = 8; // Interpolate at the point and test result double actual = target.Value(point, bounds); Assert.AreEqual(expected, actual); // Create the point to interpolate at point = new Point2D(4, 3, 0); // Create the bounding points bounds = new List <IPoint>(); bounds.Add(new Point2D(2, 2, 2)); bounds.Add(new Point2D(5, 2, 11)); bounds.Add(new Point2D(2, 4, 6)); bounds.Add(new Point2D(5, 4, 15)); // Set the expected value expected = 10; // Interpolate at the point and test result actual = target.Value(point, bounds); Assert.AreEqual(expected, actual); // Test the boundary condition xy > x1y1 // Create the point to interpolate at point = new Point2D(5, 5, 0); // Create the bounding points bounds = new List <IPoint>(); bounds.Add(new Point2D(4, 3, 10)); bounds.Add(new Point2D(5, 3, 13)); bounds.Add(new Point2D(4, 4, 12)); bounds.Add(new Point2D(5, 4, 15)); // Set the expected value expected = 15.0d; // Interpolate at the point and test result actual = target.Value(point, bounds); Assert.AreEqual(expected, actual); }
private void Ini(double[] T, double[] df, double[] fwd, double[] yf, double[] Tquoted, List <double[]> VolSilos, double[] strike) { // data member Exp = T; Strike = strike; // Caplet are telescopic, max number of caplets. Fill data in CapletVolMatrix. CapletVolMatrix = new List <double[]>(); for (int i = 0; i < strike.Length; i++) { monoStrikeCapletVolBuilder b = new monoStrikeCapletVolBuilder(); b.LateIni(T, df, fwd, yf, Tquoted, VolSilos[i], strike[i]); CapletVolMatrix.Add(b.GetCapletVol()); } // Inizialize the Bilinear Interpolator BI = new BilinearInterpolator(T, strike, CapletVolMatrix); }
protected void drawQuad(DrawContext dc, Matrix geoToCartesian, int slices, int stacks) { Vec4 ll = this.transformToQuadCoordinates(geoToCartesian, this.corners.get(0)); Vec4 lr = this.transformToQuadCoordinates(geoToCartesian, this.corners.get(1)); Vec4 ur = this.transformToQuadCoordinates(geoToCartesian, this.corners.get(2)); Vec4 ul = this.transformToQuadCoordinates(geoToCartesian, this.corners.get(3)); BilinearInterpolator interp = new BilinearInterpolator(ll, lr, ur, ul); GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility. gl.glBegin(GL2.GL_TRIANGLE_STRIP); try { this.drawQuad(dc, interp, slices, stacks); } finally { gl.glEnd(); } }
// used in constructor private void Ini(string tenor, IRateCurve curve, BilinearInterpolator capletVolMatrix, double nominal) { stringTenor = tenor; rateCurve = curve; volMatrix = capletVolMatrix; N = nominal; // yf of longer cap SwapStyle y = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(curve.RefDate(), 0, tenor, curve.GetSwapStyle().buildingBlockType); yf = y.scheduleLeg2.GetYFVect(Dc._Act_360); capSchedule = y.scheduleLeg2; int toRemove = yf.Length - 1; yf = yf.Where((val, inx) => inx != toRemove).ToArray(); // T all tenor needed. They are more than input Date refDate = curve.RefDate(); List <Date> ToDate = y.scheduleLeg2.toDates.ToList(); ToDate.RemoveAt(ToDate.Count - 1); // remove last element T = (from c in ToDate select refDate.YF_365(c)).ToArray(); // df- getting relevant dates Date[] dfDate = y.scheduleLeg2.payDates; int Ncaplet = yf.Length; // number of caplets df = new double[Ncaplet]; // fwd rate fwd = new double[Ncaplet]; Date[] fwdDate = y.scheduleLeg2.fromDates; for (int i = 0; i < Ncaplet; i++) // Note the loop start from 1 { // first discount factor is on first payment date of caplet (first caplet skipped) df[i] = curve.Df(dfDate[i + 1]); fwd[i] = curve.Fwd(fwdDate[i + 1]); } }
protected void drawQuad(DrawContext dc, BilinearInterpolator interp, int slices, int stacks) { double[] compArray = new double[4]; double du = 1.0f / (float)slices; double dv = 1.0f / (float)stacks; GL2 gl = dc.getGL().getGL2(); // GL initialization checks for GL2 compatibility. for (int vi = 0; vi < stacks; vi++) { double v = vi * dv; double vn = (vi + 1) * dv; if (vi != 0) { interp.interpolate(slices * du, v, compArray); gl.glTexCoord2d(slices * du, v); gl.glVertex3dv(compArray, 0); interp.interpolate(0, v, compArray); gl.glTexCoord2d(0, v); gl.glVertex3dv(compArray, 0); } for (int ui = 0; ui <= slices; ui++) { double u = ui * du; interp.interpolate(u, v, compArray); gl.glTexCoord2d(u, v); gl.glVertex3dv(compArray, 0); interp.interpolate(u, vn, compArray); gl.glTexCoord2d(u, vn); gl.glVertex3dv(compArray, 0); } } }
public override double Price(BilinearInterpolator CapletVolMatrix, double strike) { return(Formula.CapBlack(T, yf, N, strike, CapletVolMatrix, df, fwd)); }
public void BilinearInterpolatorConstructorTest() { BilinearInterpolator target = new BilinearInterpolator(); Assert.IsNotNull(target); }
void TestEdges(string dataSetName, double lat, double lon , string rasterSouthWestName, string rasterSouthEastName , string rasterNorthWestName, string rasterNorthEastName) { DEMDataSet dataSet = DEMDataSet.RegisteredDatasets.FirstOrDefault(d => d.Name == dataSetName); Assert.NotNull(dataSet); DEMFileType fileType = dataSet.FileFormat.Type; int rasterSize = dataSet.PointsPerDegree; double amountx = (1d / rasterSize) / 4d; double amounty = (1d / rasterSize) / 4d; // Regenerates all metadata //_rasterService.GenerateDirectoryMetadata(dataSet // , force: true // , deleteOnError: false // , maxDegreeOfParallelism: 1); _elevationService.DownloadMissingFiles(dataSet, lat, lon); var tiles = _rasterService.GenerateReportForLocation(dataSet, lat, lon); Assert.True(tiles.Count == 4); Assert.Single(tiles, t => string.Equals(Path.GetFileName(t.LocalName), rasterSouthWestName, StringComparison.OrdinalIgnoreCase)); Assert.Single(tiles, t => string.Equals(Path.GetFileName(t.LocalName), rasterSouthEastName, StringComparison.OrdinalIgnoreCase)); Assert.Single(tiles, t => string.Equals(Path.GetFileName(t.LocalName), rasterNorthWestName, StringComparison.OrdinalIgnoreCase)); Assert.Single(tiles, t => string.Equals(Path.GetFileName(t.LocalName), rasterNorthEastName, StringComparison.OrdinalIgnoreCase)); if (dataSet.FileFormat.Registration == DEMFileRegistrationMode.Cell) { using (var rasterNW = _rasterService.OpenFile(tiles.First(t => string.Equals(rasterNorthWestName, Path.GetFileName(t.LocalName))).LocalName, fileType)) using (var rasterNE = _rasterService.OpenFile(tiles.First(t => string.Equals(rasterNorthEastName, Path.GetFileName(t.LocalName))).LocalName, fileType)) using (var rasterSW = _rasterService.OpenFile(tiles.First(t => string.Equals(rasterSouthWestName, Path.GetFileName(t.LocalName))).LocalName, fileType)) using (var rasterSE = _rasterService.OpenFile(tiles.First(t => string.Equals(rasterSouthEastName, Path.GetFileName(t.LocalName))).LocalName, fileType)) { var elevNW = rasterNW.GetElevationAtPoint(rasterNW.ParseMetaData(dataSet.FileFormat), rasterSize - 1, rasterSize - 1); var elevNE = rasterNE.GetElevationAtPoint(rasterNE.ParseMetaData(dataSet.FileFormat), 0, rasterSize - 1); var elevSW = rasterSW.GetElevationAtPoint(rasterSW.ParseMetaData(dataSet.FileFormat), rasterSize - 1, 0); var elevSE = rasterSE.GetElevationAtPoint(rasterSE.ParseMetaData(dataSet.FileFormat), 0, 0); BilinearInterpolator interpolator = new BilinearInterpolator(); var elev0 = interpolator.Interpolate(elevSW, elevSE, elevNW, elevNE, 0.25, 0.25); var apiElev0 = _elevationService.GetPointElevation(lat + amounty, lon - amountx, dataSet); Assert.True((elev0 - apiElev0.Elevation.Value) < double.Epsilon); var elev1 = interpolator.Interpolate(elevSW, elevSE, elevNW, elevNE, 0.75, 0.25); var apiElev1 = _elevationService.GetPointElevation(lat + amounty, lon + amountx, dataSet); Assert.True((elev1 - apiElev1.Elevation.Value) < double.Epsilon); var elev2 = interpolator.Interpolate(elevSW, elevSE, elevNW, elevNE, 0.25, 0.75); var apiElev2 = _elevationService.GetPointElevation(lat - amounty, lon - amountx, dataSet); Assert.True((elev2 - apiElev2.Elevation.Value) < double.Epsilon); var elev3 = interpolator.Interpolate(elevSW, elevSE, elevNW, elevNE, 0.75, 0.75); var apiElev3 = _elevationService.GetPointElevation(lat - amounty, lon + amountx, dataSet); Assert.True((elev3 - apiElev3.Elevation.Value) < double.Epsilon); } } else { using (var rasterNW = _rasterService.OpenFile(tiles.First(t => string.Equals(rasterNorthWestName, Path.GetFileName(t.LocalName))).LocalName, fileType)) using (var rasterNE = _rasterService.OpenFile(tiles.First(t => string.Equals(rasterNorthEastName, Path.GetFileName(t.LocalName))).LocalName, fileType)) using (var rasterSW = _rasterService.OpenFile(tiles.First(t => string.Equals(rasterSouthWestName, Path.GetFileName(t.LocalName))).LocalName, fileType)) using (var rasterSE = _rasterService.OpenFile(tiles.First(t => string.Equals(rasterSouthEastName, Path.GetFileName(t.LocalName))).LocalName, fileType)) { // Northen row, west to east var elevN0 = rasterNW.GetElevationAtPoint(rasterNW.ParseMetaData(dataSet.FileFormat), rasterSize - 1, rasterSize - 1); var elevN1 = rasterNW.GetElevationAtPoint(rasterNW.ParseMetaData(dataSet.FileFormat), rasterSize, rasterSize - 1); var elevN2 = rasterNE.GetElevationAtPoint(rasterNE.ParseMetaData(dataSet.FileFormat), 1, rasterSize - 1); // middle row, west to east var elevM0 = rasterNW.GetElevationAtPoint(rasterNW.ParseMetaData(dataSet.FileFormat), rasterSize - 1, rasterSize); var elevM1 = rasterNW.GetElevationAtPoint(rasterNW.ParseMetaData(dataSet.FileFormat), rasterSize, rasterSize); var elevM2 = rasterNE.GetElevationAtPoint(rasterNE.ParseMetaData(dataSet.FileFormat), 1, rasterSize); // Sourthen row, west to east var elevS0 = rasterSW.GetElevationAtPoint(rasterSW.ParseMetaData(dataSet.FileFormat), rasterSize - 1, 1); var elevS1 = rasterSW.GetElevationAtPoint(rasterSW.ParseMetaData(dataSet.FileFormat), rasterSize, 1); var elevS2 = rasterSE.GetElevationAtPoint(rasterSE.ParseMetaData(dataSet.FileFormat), 1, 1); BilinearInterpolator interpolator = new BilinearInterpolator(); var elev0 = interpolator.Interpolate(elevM0, elevM1, elevN0, elevN1, 0.75, 0.75); var apiElev0 = _elevationService.GetPointElevation(lat + amounty, lon - amountx, dataSet); Assert.True((elev0 - apiElev0.Elevation.Value) < double.Epsilon); var elev1 = interpolator.Interpolate(elevM1, elevM2, elevN1, elevN2, 0.25, 0.75); var apiElev1 = _elevationService.GetPointElevation(lat + amounty, lon + amountx, dataSet); Assert.True((elev1 - apiElev1.Elevation.Value) < double.Epsilon); var elev2 = interpolator.Interpolate(elevS0, elevS1, elevM0, elevM1, 0.75, 0.25); var apiElev2 = _elevationService.GetPointElevation(lat - amounty, lon - amountx, dataSet); Assert.True((elev2 - apiElev2.Elevation.Value) < double.Epsilon); var elev3 = interpolator.Interpolate(elevS1, elevS2, elevM1, elevM2, 0.25, 0.25); var apiElev3 = _elevationService.GetPointElevation(lat - amounty, lon + amountx, dataSet); Assert.True((elev3 - apiElev3.Elevation.Value) < double.Epsilon); } } }
public VanillaCapFloor(string tenor, IRateCurve curve, BilinearInterpolator capletVolMatrix, double nominal) { Ini(tenor, curve, capletVolMatrix, nominal); }
public static void BilinearInterpolation2() { // Create mesh arrays int startIndex = 0; // Number of subdivisions N,M in the x and y directions int N = 4; int M = 3; Vector <double> x1arr = new Vector <double>(N + 1, startIndex, 0.0); double a = 0.0; double b = 1.0; double h1 = (b - a) / (double)N; x1arr[x1arr.MinIndex] = a; for (int j = x1arr.MinIndex + 1; j <= x1arr.MaxIndex; j++) { x1arr[j] = x1arr[j - 1] + h1; } Vector <double> x2arr = new Vector <double>(M + 1, startIndex, 0.0); double h2 = (b - a) / (double)M; x1arr[x1arr.MinIndex] = a; for (int j = x2arr.MinIndex + 1; j <= x2arr.MaxIndex; j++) { x2arr[j] = x2arr[j - 1] + h2; } Console.WriteLine(x1arr); Console.WriteLine(x2arr); // Control values; NumericMatrix <double> Control = new NumericMatrix <double>(N + 1, M + 1, startIndex, startIndex); Func <double, double, double> func = (double x1, double x2) => x1 + x2; for (int i = Control.MinRowIndex; i <= Control.MaxRowIndex; i++) { for (int j = Control.MinColumnIndex; j <= Control.MaxColumnIndex; j++) { Control[i, j] = func(x1arr[i], x2arr[j]); } } BilinearInterpolator myInterpolator = new BilinearInterpolator(x1arr, x2arr, Control); double x = 0.1; double y = 0.7; double value = myInterpolator.Solve(x, y); Console.WriteLine("Interpolated value: {0}", value); // Take center point (xm, ym) of each element and interpolate on it NumericMatrix <double> InterpolatedMatrix = new NumericMatrix <double>(N, M, startIndex, startIndex); // Abscissa points of new interpolated matrix Vector <double> Xarr = new Vector <double>(InterpolatedMatrix.Rows, startIndex, 0.0); Vector <double> Yarr = new Vector <double>(InterpolatedMatrix.Columns, startIndex, 0.0); for (int i = InterpolatedMatrix.MinRowIndex; i <= InterpolatedMatrix.MaxRowIndex; i++) { for (int j = InterpolatedMatrix.MinColumnIndex; j <= InterpolatedMatrix.MaxColumnIndex; j++) { Xarr[i] = 0.5 * (x1arr[i] + x1arr[i + 1]); // xm Yarr[j] = 0.5 * (x2arr[j] + x2arr[j + 1]); // ym InterpolatedMatrix[i, j] = myInterpolator.Solve(Xarr[i], Yarr[j]); } } // Present the interpolated matrix in Excel ExcelMechanisms driver = new ExcelMechanisms(); string title = "Interpolated matrix"; driver.printMatrixInExcel <double>(InterpolatedMatrix, Xarr, Yarr, title); }
public static double Swaption(double N, double K, string Start, string SwapTenor, bool isPayer, BilinearInterpolator VolMatrix, IRateCurve Curve) { Date refDate = Curve.RefDate(); // curve ref date // false/true with fwd swap matrix Date startDate = refDate.add_period(Start, false); // swap start 2 business days after the expiry Date expDate = startDate.add_workdays(-2); // expiry of swaption Date today = refDate.add_workdays(-2); double T = today.YF_365(expDate); Period p = new Period(SwapTenor); // should be in year 1Y, 2Y (not 3m,...) double sigma = VolMatrix.Solve(T, p.tenor); return(Swaption(N, K, Start, SwapTenor, isPayer, sigma, Curve)); }
public static double CapBlack(string Tenor, double strike, double N, IRateCurve curve, BilinearInterpolator VolCapletMatrix) { Date refDate = curve.RefDate(); SwapStyle y = (SwapStyle) new BuildingBlockFactory().CreateBuildingBlock(refDate, 0, Tenor, curve.GetSwapStyle().buildingBlockType); double[] yf = y.scheduleLeg2.GetYFVect(Dc._Act_360); int toRemove = yf.Length - 1; yf = yf.Where((val, inx) => inx != toRemove).ToArray(); List <Date> ToDate = y.scheduleLeg2.toDates.ToList(); ToDate.RemoveAt(ToDate.Count - 1); // remove last element double[] T = (from c in ToDate select refDate.YF_365(c)).ToArray(); // df- getting relevant dates Date[] dfDate = y.scheduleLeg2.payDates; int Ncaplet = yf.Length; // number of caplets double[] df = new double[Ncaplet]; // fwd rate double[] fwd = new double[Ncaplet]; Date[] fwdDate = y.scheduleLeg2.fromDates; for (int i = 0; i < Ncaplet; i++) // Note the loop start from 1 { // first discount factor is on first payment date of caplet (first caplet skipped) df[i] = curve.Df(dfDate[i + 1]); fwd[i] = curve.Fwd(fwdDate[i + 1]); } double[] sigma = (from t in T select VolCapletMatrix.Solve(t, strike)).ToArray(); return(CapBlack(T, yf, N, strike, sigma, df, fwd)); }
// Update VolMatrix public virtual void SetNewVolMatrix(BilinearInterpolator newCapletVolMatrix) { Ini(stringTenor, rateCurve, newCapletVolMatrix, N); }
// Constructor public VanillaCap(string tenor, IRateCurve curve, BilinearInterpolator capletVolMatrix, double nominal) : base(tenor, curve, capletVolMatrix, nominal) { IsCap = true; // if true is a cap }
abstract public double Price(double[] sigma, double strike); // array of sigma abstract public double Price(BilinearInterpolator CapletVolMatrix, double strike); // you can customize it