internal static DiscreteSurface ProcessMultiDimensionalPricingData(MultiDimensionalPricingData data, DateTime baseDate) { var points = data.point.Select(point => new PricingDataPoint2D(point, baseDate)).Cast <IPoint>().ToList(); var result = new DiscreteSurface(points); return(result); }
internal static DiscreteSurface ProcessMultiDimensionalPricingData(MultiDimensionalPricingData data) { var points = (from point in data.point let baseDate = data.valuationDateSpecified ? (DateTime?)data.valuationDate : null select new PricingDataPoint2D(point, baseDate)).Cast <IPoint>().ToList(); var result = new DiscreteSurface(points);//todo THERE REMAINS A PROBLEM WITH THIS CONSTRUCTOR. return(result); }
/// <summary> /// Gets the quoted asset set. /// </summary> /// <param name="logger">The logger.</param> /// <param name="cache">The cache.</param> /// <param name="nameSpace">The client namespace</param> /// <param name="fixingCalendar">The fixingCalendar.</param> /// <param name="rollCalendar">The rollCalendar.</param> /// <returns></returns> public override void Build(ILogger logger, ICoreCache cache, string nameSpace, IBusinessCalendar fixingCalendar, IBusinessCalendar rollCalendar) { var curveId = (VolatilitySurfaceIdentifier)PricingStructureIdentifier; //TODO This for backwards compatability. AnalyticalResults is the current interface. BootstrapResults = new VolCurveBootstrapResults(); PriceableOptionAssets = PriceableAssetFactory.CreatePriceableRateOptionAssets(logger, cache, nameSpace, GetVolatilityMatrix().baseDate.Value, GetVolatilityMatrix().inputs, fixingCalendar, rollCalendar); //GetVolatilityMatrix().dataPoints = null; MultiDimensionalPricingData dataPoints = GetVolatilityMatrix().dataPoints; DateTime baseDate = GetVolatilityMatrix().baseDate.Value; var interpolationMethod = InterpolationMethod; var extrapolationPermitted = ExtrapolationPermittedInput ?? ExtrapolationPermitted; BootstrapResults.Results = CapFloorBootstrapper.Bootstrap(PriceableOptionAssets, curveId.Properties, DiscountCurve, ForecastCurve, baseDate, extrapolationPermitted, interpolationMethod, Tolerance); IsBootstrapSuccessful = true; InitialiseVolatilities(curveId.BaseDate, BootstrapResults.Results); //The points use tenor strings for expiry. var termTenors = new List <String>(); foreach (var daysDifference in VolatilityOffsets) { termTenors.Add(daysDifference + "D"); } dataPoints.point = ProcessRawSurface(termTenors, Strike, VolatilityValues, curveId.StrikeQuoteUnits, curveId.UnderlyingAssetReference); SetInterpolator(dataPoints, curveId.Algorithm, curveId.PricingStructureType); }
/// <summary> /// Takes a range of volatilities, an array of tenor expiries and an /// array of strikes to create a VolatilitySurface /// </summary> /// <param name="nameSpace"></param> /// <param name="expiryTenors"></param> /// <param name="strikes"></param> /// <param name="volSurface"></param> /// <param name="surfaceId"></param> /// <param name="logger"></param> /// <param name="cache">The cache.</param> /// <param name="forwards">The array of forwards. The first element is the spot value. Conseuently, the length of this array is expiryTenors.Length + 1.</param> protected ExtendedExpiryTermStrikeVolatilitySurface(ILogger logger, ICoreCache cache, String nameSpace, String[] expiryTenors, Double[] strikes, Double[] forwards, Double[,] volSurface, VolatilitySurfaceIdentifier surfaceId) { Algorithm = surfaceId.Algorithm; PricingStructureIdentifier = surfaceId; //Build the parameteric adjustment set from th3e forwards. var parametricAdjustment = GenerateForwards(forwards); var holder = new PricingStructureAlgorithmsHolder(logger, cache, nameSpace, surfaceId.PricingStructureType, surfaceId.Algorithm); var xDimensionInterpolationMethod = InterpolationMethodHelper.Parse(holder.GetValue("xDimensionInterpolation")); var yDimensionInterpolationMethod = InterpolationMethodHelper.Parse(holder.GetValue("yDimensionInterpolation")); _matrixIndexHelper = new SortedList <ExpiryTenorStrikeKey, int>(new ExpiryTenorStrikeKey()); var points = ProcessRawSurface(expiryTenors, strikes, volSurface, surfaceId.StrikeQuoteUnits, surfaceId.UnderlyingAssetReference); PricingStructure = new VolatilityRepresentation { name = surfaceId.Name, id = surfaceId.Id, currency = surfaceId.Currency, asset = new AnyAssetReference { href = surfaceId.Instrument }, }; var datapoints = new MultiDimensionalPricingData { point = points }; PricingStructureValuation = new VolatilityMatrix { dataPoints = datapoints , adjustment = parametricAdjustment , objectReference = new AnyAssetReference { href = surfaceId.Instrument } , baseDate = new IdentifiedDate { Value = surfaceId.BaseDate } , buildDateTime = DateTime.Now , buildDateTimeSpecified = true }; var expiries = new double[expiryTenors.Length]; var index = 0; foreach (var term in expiryTenors)//TODO include business day holidays and roll conventions. { expiries[index] = PeriodHelper.Parse(term).ToYearFraction(); index++; } // Record the row/column sizes of the inputs _matrixRowCount = expiryTenors.Length; // Columns includes expiry and term (tenor) if it exists. _matrixColumnCount = strikes.Length + 1; // Generate an interpolator to use Interpolator = new ExtendedVolatilitySurfaceInterpolator(expiries, strikes, forwards, new Matrix(volSurface), xDimensionInterpolationMethod.Value, yDimensionInterpolationMethod.Value); }
/// <summary> /// Create a grid from an array of names and complex array of data /// The data will contain strings and decimals /// </summary> /// <param name="pSettings">The settings object used to generate these SABR parameters</param> /// <param name="fields">The headers used to identify the SABR parameters</param> /// <param name="data">An array of SABR parameters and expiry/tenor pairs</param> /// <param name="valueDate">The valuation date</param> /// <param name="surfaceId">The id for this matrix</param> public SwaptionVolatilityMatrix(object[][] pSettings, string[] fields, object[][] data, DateTime valueDate, string surfaceId) { // Include a QuotedAssetSet to hold the Settings object used to generate this SABR parameters Matrix Settings = AssignSettings(pSettings); // Set the id for this matrix id = surfaceId; // Set the value date for this vol matrix baseDate = new IdentifiedDate { Value = valueDate }; // Set the buildDate for this matrix buildDateTime = DateTime.Now; // Create the dataPoints structure. This will hold the matrix data dataPoints = new MultiDimensionalPricingData(); var columns = fields.Length; var rows = data.Length; dataPoints.point = new PricingStructurePoint[rows * IdentifierFieldCount]; var point = 0; // Loop through the arrays to populate the underlying VolatilityMatrix for (var gRows = 0; gRows < rows; gRows++) { // Extract the expiry/tenor information for creating var expiry = PeriodHelper.Parse(data[gRows][0].ToString()); var tenor = PeriodHelper.Parse(data[gRows][1].ToString()); for (var gCols = columns - IdentifierFieldCount; gCols < columns; gCols++) { dataPoints.point[point] = new PricingStructurePoint { coordinate = new PricingDataPointCoordinate[1] }; // Set up the co-ordinate (Expiry/Term) for each point dataPoints.point[point].coordinate[0] = new PricingDataPointCoordinate { expiration = new TimeDimension[1] }; // Set the Expiry for the co-ordinate point dataPoints.point[point].coordinate[0].expiration[0] = new TimeDimension { Items = new object[] { expiry } }; // Set the Term for the co-ordinate point dataPoints.point[point].coordinate[0].term = new TimeDimension[1]; dataPoints.point[point].coordinate[0].term[0] = new TimeDimension { Items = new object[] { tenor } }; // Add the quotation characteristics for the point // We will only record a value and the measure type dataPoints.point[point].valueSpecified = true; dataPoints.point[point].value = Convert.ToDecimal(data[gRows][gCols]); dataPoints.point[point].measureType = new AssetMeasureType { Value = fields[gCols] }; point++; } } }
private static VolatilityMatrix CreateDataPoints(PricingStructurePoint[] points, VolatilitySurfaceIdentifier surfaceId) { var datapoints = new MultiDimensionalPricingData { point = points, businessCenter = surfaceId.BusinessCenter, timing = surfaceId.QuoteTiming, currency = surfaceId.Currency, cashflowType = surfaceId.CashflowType, informationSource = surfaceId.InformationSources, measureType = surfaceId.MeasureType, quoteUnits = surfaceId.QuoteUnits }; if (surfaceId.ExpiryTime != null) { datapoints.expiryTime = (DateTime)surfaceId.ExpiryTime; datapoints.expiryTimeSpecified = true; } if (surfaceId.ValuationDate != null) { datapoints.valuationDate = (DateTime)surfaceId.ValuationDate; datapoints.valuationDateSpecified = true; } else { datapoints.valuationDate = surfaceId.BaseDate; datapoints.valuationDateSpecified = true; } if (surfaceId.Time != null) { datapoints.time = (DateTime)surfaceId.Time; datapoints.timeSpecified = true; } if (surfaceId.QuotationSide != null) { datapoints.side = (QuotationSideEnum)surfaceId.QuotationSide; datapoints.sideSpecified = true; } var pricingStructureValuation = new VolatilityMatrix { dataPoints = datapoints, objectReference = new AnyAssetReference { href = surfaceId.Instrument }, baseDate = new IdentifiedDate { Value = surfaceId.BaseDate }, buildDateTime = surfaceId.BuildDateTime, buildDateTimeSpecified = true, definitionRef = surfaceId.CapFrequency }; return(pricingStructureValuation); }
/// <summary> /// Construct a forward rates matrix from a grid /// </summary> /// <param name="strike">An array of strikes.</param> /// <param name="expiry">An array of expiry terms as strings</param> /// <param name="tenor">An array of swap tenor terms as strings</param> /// <param name="data">The forward rates for each expiry/tenor pair</param> /// <param name="matrixId">An id to associate with this array</param> public SwaptionDataMatrix(string[] tenor, decimal[] strike, string expiry, decimal[][] data, string matrixId) : this() { // A default id id = matrixId; dataPoints = new MultiDimensionalPricingData { point = new PricingStructurePoint[tenor.Length * strike.Length] }; // Set the points held by the matrix to be the total (expiry x tenor) values var point = 0; var rateExpiry = PeriodHelper.Parse(expiry); // Loop through our expiry/tenor and data arrays to build the matrix points for (var row = 0; row < tenor.Length; row++) { // Convert the expiry to an Interval var rateTenor = PeriodHelper.Parse(tenor[row]); for (var col = 0; col < strike.Length; col++) { var rateStrike = strike[col]; // Add the value to our expiry/tenor point dataPoints.point[point] = new PricingStructurePoint { valueSpecified = true, value = data[row][col], coordinate = new PricingDataPointCoordinate[1] }; // Set the value of this point // Set the expiry/tenor coordinate for this point dataPoints.point[point].coordinate[0] = new PricingDataPointCoordinate { expiration = new TimeDimension[1] }; // Expiry dataPoints.point[point].coordinate[0].expiration[0] = new TimeDimension { Items = new object[] { rateExpiry } }; // tenor dataPoints.point[point].coordinate[0].term = new TimeDimension[1]; dataPoints.point[point].coordinate[0].term[0] = new TimeDimension { Items = new object[] { rateTenor } }; // strike dataPoints.point[point].coordinate[0].strike = new[] { rateStrike }; point++; } } }
/// <summary> /// Takes a range of volatilities, an array of tenor expiries and an /// array of strikes to create a VolatilitySurface /// </summary> /// <param name="logger"></param> /// <param name="cache"></param> /// <param name="nameSpace"></param> /// <param name="expiryTenors"></param> /// <param name="strikes"></param> /// <param name="volSurface"></param> /// <param name="surfaceId"></param> public ExpiryTermTenorStrikeVolatilityCube(ILogger logger, ICoreCache cache, String nameSpace, String[] expiryTenors, Double[] strikes, Double[,] volSurface, VolatilitySurfaceIdentifier surfaceId) { Algorithm = surfaceId.Algorithm; var holder = new PricingStructureAlgorithmsHolder(logger, cache, nameSpace, surfaceId.PricingStructureType, surfaceId.Algorithm); var curveInterpolationMethod = InterpolationMethodHelper.Parse(holder.GetValue("CurveInterpolation")); var points = ProcessRawSurface(expiryTenors, strikes, volSurface); PricingStructure = new VolatilityRepresentation { name = surfaceId.Name, id = surfaceId.Id, asset = new AnyAssetReference { href = "Unknown" }, }; var datapoints = new MultiDimensionalPricingData { point = points }; PricingStructureValuation = new VolatilityMatrix { dataPoints = datapoints , objectReference = new AnyAssetReference { href = PricingStructure.id } , baseDate = new IdentifiedDate { Value = surfaceId.BaseDate } , buildDateTime = DateTime.Now , buildDateTimeSpecified = true }; var expiries = new double[expiryTenors.Length]; var index = 0; foreach (var term in expiryTenors)//TODO include business day holidays and roll conventions. { expiries[index] = PeriodHelper.Parse(term).ToYearFraction(); index++; } // Generate an interpolator to use Interpolator = new VolSurfaceInterpolator(expiries, strikes, new Matrix(volSurface), curveInterpolationMethod, true); }
/// <summary> /// Construct a VolatilityCube from points /// </summary> /// <param name="properties"></param> /// <param name="points"></param> public VolatilityCube(NamedValueSet properties, PricingStructurePoint[] points) { properties.Set(EnvironmentProp.Function, FunctionProp.Market.ToString()); PricingStructureIdentifier = new VolatilitySurfaceIdentifier(properties); var surfaceId = (VolatilitySurfaceIdentifier)PricingStructureIdentifier; PricingStructureData = new PricingStructureData(CurveType.Parent, Constants.AssetClass.Rates);//TODO Need to set for the different underlyers. SetInterpolator(); foreach (PricingStructurePoint point in points) { point.underlyingAssetReference = surfaceId.UnderlyingAssetReference; point.quoteUnits = surfaceId.StrikeQuoteUnits; } PricingStructure = new VolatilityRepresentation { name = surfaceId.Name, id = surfaceId.Id, currency = surfaceId.Currency, asset = new AnyAssetReference { href = surfaceId.Instrument }, }; var datapoints = new MultiDimensionalPricingData { point = points }; PricingStructureValuation = new VolatilityMatrix { dataPoints = datapoints, objectReference = new AnyAssetReference { href = surfaceId.Instrument }, baseDate = new IdentifiedDate { Value = surfaceId.BaseDate }, buildDateTime = DateTime.Now, buildDateTimeSpecified = true }; _matrixIndexHelper = new SortedList <ExpiryTenorStrikeKey, int>(new ExpiryTenorStrikeKey()); ProcessVolatilityRepresentation(); }
/// <summary> /// Builds a raw volatility surface from basic data types. /// </summary> /// <param name="data"></param> /// <param name="forwards"></param> /// <param name="xInterpolation"></param> /// <param name="yInterpolation"></param> public ExtendedVolatilitySurfaceInterpolator(MultiDimensionalPricingData data, ParametricAdjustmentPoint[] forwards, string xInterpolation, string yInterpolation) : base(ProcessMultiDimensionalPricingData(data), forwards, InterpolationFactory.Create(xInterpolation), InterpolationFactory.Create(yInterpolation), true) { }
/// <summary> /// Takes a range of volatilities, an array of tenor expiries and an /// array of strikes to create a VolatilitySurface /// </summary> /// <param name="logger">The logger.</param> /// <param name="cache">The cache.</param> /// <param name="expiryTenors">the expiry tenors.</param> /// <param name="tenors">The tenors.</param> /// <param name="volSurface">The vol surface.</param> /// <param name="nameSpace">The namespace</param> /// <param name="properties">The properties.</param> protected ExpiryTermTenorATMVolatilitySurface(ILogger logger, ICoreCache cache, String nameSpace, NamedValueSet properties, String[] expiryTenors, String[] tenors, Double[,] volSurface) { Algorithm = PropertyHelper.ExtractAlgorithm(properties); PricingStructureIdentifier = new VolatilitySurfaceIdentifier(properties); var surfaceId = (VolatilitySurfaceIdentifier)PricingStructureIdentifier; var holder = new PricingStructureAlgorithmsHolder(logger, cache, nameSpace, surfaceId.PricingStructureType, surfaceId.Algorithm); var curveInterpolationMethod = InterpolationMethodHelper.Parse(holder.GetValue("CurveInterpolation")); _matrixIndexHelper = new SortedList <ExpiryTenorStrikeKey, int>(new ExpiryTenorStrikeKey()); var points = ProcessRawSurface(expiryTenors, tenors, volSurface, surfaceId.UnderlyingAssetReference); PricingStructure = new VolatilityRepresentation { name = surfaceId.Name, id = surfaceId.Id, currency = surfaceId.Currency, asset = new AnyAssetReference { href = surfaceId.Instrument }, }; var datapoints = new MultiDimensionalPricingData { point = points, businessCenter = surfaceId.BusinessCenter, timing = surfaceId.QuoteTiming, currency = surfaceId.Currency, cashflowType = surfaceId.CashflowType, informationSource = surfaceId.InformationSources, measureType = surfaceId.MeasureType, quoteUnits = surfaceId.QuoteUnits }; if (surfaceId.ExpiryTime != null) { datapoints.expiryTime = (DateTime)surfaceId.ExpiryTime; datapoints.expiryTimeSpecified = true; } if (surfaceId.ValuationDate != null) { datapoints.valuationDate = (DateTime)surfaceId.ValuationDate; datapoints.valuationDateSpecified = true; } if (surfaceId.Time != null) { datapoints.time = (DateTime)surfaceId.Time; datapoints.timeSpecified = true; } if (surfaceId.QuotationSide != null) { datapoints.side = (QuotationSideEnum)surfaceId.QuotationSide; datapoints.sideSpecified = true; } PricingStructureValuation = new VolatilityMatrix { dataPoints = datapoints, objectReference = new AnyAssetReference { href = surfaceId.Instrument }, baseDate = new IdentifiedDate { Value = surfaceId.BaseDate }, buildDateTime = surfaceId.BuildDateTime, buildDateTimeSpecified = true }; var expiries = new double[expiryTenors.Length]; var index = 0; foreach (var term in expiryTenors)//TODO include business day holidays and roll conventions. { expiries[index] = PeriodHelper.Parse(term).ToYearFraction(); index++; } var tenor = new double[tenors.Length]; index = 0; foreach (var term in tenors)//TODO include business day holidays and roll conventions. { tenor[index] = PeriodHelper.Parse(term).ToYearFraction(); index++; } // Record the row/column sizes of the inputs _matrixRowCount = expiryTenors.Length; _matrixRowCount *= tenors.Length; // Columns includes expiry and term (tenor) if it exists. _matrixColumnCount = 1; // Generate an interpolator to use Interpolator = new VolSurfaceInterpolator(expiries, tenor, new Matrix(volSurface), curveInterpolationMethod, true); }
/// <summary> /// Builds a raw volatility surface from basic data types. /// </summary> /// <param name="data"></param> /// <param name="interpolation"></param> /// <param name="allowExtrapolation"></param> /// <param name="baseDate"></param> public VolSurfaceInterpolator(MultiDimensionalPricingData data, InterpolationMethod interpolation, bool allowExtrapolation, DateTime baseDate) : base(ProcessMultiDimensionalPricingData(data, baseDate), InterpolationFactory.Create(interpolation.Value), allowExtrapolation) { }
/// <summary> /// Construct an FpML MultiDimensionalPricingData from the spreadsheet values /// </summary> /// <param name="headers">The columns to write</param> /// <param name="data">The values to store</param> /// <param name="settings">The settings used by this matrix</param> /// <param name="valueDate">The valuation date</param> /// <param name="surfaceId">The id of this surface</param> public CapFloorATMMatrix(string[] headers, object[][] data, object[][] settings, DateTime valueDate, string surfaceId) : this() { // Include a QuotedAssetSet to hold the Settings object used to generate this SABR parameters Matrix if (settings != null) { Settings = AssignSettings(settings); } id = surfaceId; baseDate = new IdentifiedDate { Value = valueDate }; var rows = data.GetUpperBound(0) + 1; var points = new List <PricingStructurePoint>(); int expiry = Find("Expiry", headers); int atm = Find("ATM", headers); int type = Find("Type", headers); int ppd = Find("PPD", headers); for (int row = 0; row < rows; row++) { var point = new PricingStructurePoint(); if (data[row][0] != null) { // Populate each data point from the data array for (var column = 0; column < headers.Length; column++) { object datum = data[row][column]; if (column == expiry) { // Add the coordinate value (the expiry) var expiration = PeriodHelper.Parse(datum.ToString()); point.coordinate = new PricingDataPointCoordinate[1]; point.coordinate[0] = new PricingDataPointCoordinate { expiration = new TimeDimension[1] }; point.coordinate[0].expiration[0] = new TimeDimension { Items = new object[] { expiration } }; } else if (column == atm) { point.measureType = new AssetMeasureType { Value = "ATM" }; point.valueSpecified = true; point.value = Convert.ToDecimal(datum); } else if (column == ppd) { point.measureType = new AssetMeasureType { Value = "PPD" }; point.valueSpecified = true; point.value = Convert.ToDecimal(datum); } else if (column == type) { point.cashflowType = new CashflowType { Value = datum.ToString() }; } else { throw new ArgumentException("Unknown column name: " + column); } } } points.Add(point); } dataPoints = new MultiDimensionalPricingData { point = points.ToArray() }; }