public void ConstVol() { var constVolA = 0.32; var constVolB = 0.16; var originDate = new DateTime(2017, 02, 21); var impliedSurfaceA = new ConstantVolSurface(originDate, constVolA) { AssetId = "A" }; var impliedSurfaceB = new ConstantVolSurface(originDate, constVolB) { AssetId = "B" }; var model = new AssetFxModel(originDate, null); model.AddVolSurface("A", impliedSurfaceA); model.AddVolSurface("B", impliedSurfaceB); var timesteps = Enumerable.Range(1, 4).Select(x => x / 4.0).ToArray(); var termCorrels = new[] { 0.9, 0.85, 0.8, 0.75 }; var correlVector = new CorrelationTimeVector("A", "B", termCorrels, timesteps); model.CorrelationMatrix = correlVector; var lc = model.LocalCorrelationRaw(timesteps); var lcVec = lc.Select(x => x[0][0]).ToArray(); for (var i = 0; i < termCorrels.Length; i++) { var expected = lcVec.Take(i + 1).Average(); Assert.Equal(expected, termCorrels[i], 8); } }
public void CorrelTimeVectorFacts() { var z = new CorrelationTimeVector(); z = new CorrelationTimeVector("x", "y", new[] { 0.999, 0.998 }, new[] { 1.0, 2.0 }); Assert.Throws <Exception>(() => z.GetCorrelation("x", "z")); Assert.False(z.TryGetCorrelation("x", "z", out var c)); var zz = z.Clone(); var bumped = zz.Bump(0.5); Assert.True(bumped.GetCorrelation("x", "y") < 1.0); }
public static object CreateCorrelationTimeVecvtor( [ExcelArgument(Description = "Object name")] string ObjectName, [ExcelArgument(Description = "Label A")] string LabelA, [ExcelArgument(Description = "Labels B")] object[] LabelsB, [ExcelArgument(Description = "Times")] double[] Times, [ExcelArgument(Description = "Correlations")] double[,] Correlations, [ExcelArgument(Description = "Interpolation type, default Linear")] object InterpType) { return(ExcelHelper.Execute(_logger, () => { if (!Enum.TryParse <Interpolator1DType>(InterpType.OptionalExcel("Linear"), out var iType)) { throw new Exception($"Could not parse interpolator type {InterpType}"); } var matrix = new CorrelationTimeVector(LabelA, LabelsB.ObjectRangeToVector <string>(), Correlations.SquareToJagged(), Times, iType); return ExcelHelper.PushToCache <ICorrelationMatrix>(matrix, ObjectName); })); }
private AssetFxMCModel GetSut() { var buildDate = DateTime.Parse("2018-10-04"); var usd = TestProviderHelper.CurrencyProvider["USD"]; var zar = TestProviderHelper.CurrencyProvider["ZAR"]; TestProviderHelper.CalendarProvider.Collection.TryGetCalendar("NYC", out var usdCal); var pair = new FxPair() { Domestic = zar, Foreign = usd, PrimaryCalendar = usdCal, SpotLag = 2.Bd() }; var dfCurve = new IrCurve(new[] { buildDate, buildDate.AddDays(1000) }, new[] { 0.0, 0.0 }, buildDate, "disco", Interpolator1DType.Linear, usd, "DISCO"); var dates = new[] { buildDate, buildDate.AddDays(32), buildDate.AddDays(60), buildDate.AddDays(90) }; var times = dates.Select(d => buildDate.CalculateYearFraction(d, DayCountBasis.Act365F)).ToArray(); var vols = new[] { 0.32, 0.30, 0.29, 0.28 }; var comCurve = new PriceCurve(buildDate, dates, new[] { 100.0, 100.0, 100.0, 100.0 }, PriceCurveType.NYMEX, TestProviderHelper.CurrencyProvider) { Name = "CL", AssetId = "CL" }; var comSurface = new GridVolSurface(buildDate, new[] { 0.5 }, dates, vols.Select(x => new double[] { x }).ToArray(), StrikeType.ForwardDelta, Interpolator1DType.Linear, Interpolator1DType.LinearInVariance, DayCountBasis.Act365F) { AssetId = "CL" }; var fxSurface = new ConstantVolSurface(buildDate, 0.16) { AssetId = "USD/ZAR" }; var correlVector = new CorrelationTimeVector("CL", "USD/ZAR", _correls, times); var fModel = new FundingModel(buildDate, new Dictionary <string, IrCurve> { { "DISCO", dfCurve } }, TestProviderHelper.CurrencyProvider, TestProviderHelper.CalendarProvider); var fxM = new FxMatrix(TestProviderHelper.CurrencyProvider); fxM.Init(usd, buildDate, new Dictionary <Currency, double>() { { zar, 14.0 } }, new List <FxPair>() { pair }, new Dictionary <Currency, string> { { usd, "DISCO" }, { zar, "DISCO" } }); fModel.SetupFx(fxM); fModel.VolSurfaces.Add("ZAR/USD", fxSurface); fModel.VolSurfaces.Add("USD/ZAR", fxSurface); var aModel = new AssetFxModel(buildDate, fModel); aModel.AddVolSurface("CL", comSurface); aModel.AddPriceCurve("CL", comCurve); aModel.CorrelationMatrix = correlVector; var product1 = AssetProductFactory.CreateAsianOption(dates[1], dates[1], 1400, "CL", OptionType.Call, usdCal, dates[1], zar); product1.TradeId = "P1"; product1.DiscountCurve = "DISCO"; product1.FxConversionType = FxConversionType.AverageThenConvert; var product2 = AssetProductFactory.CreateAsianOption(dates[2], dates[2], 1400, "CL", OptionType.Call, usdCal, dates[2], zar); product2.TradeId = "P2"; product2.DiscountCurve = "DISCO"; product2.FxConversionType = FxConversionType.AverageThenConvert; var product3 = AssetProductFactory.CreateAsianOption(dates[3], dates[3], 1400, "CL", OptionType.Call, usdCal, dates[3], zar); product3.TradeId = "P3"; product3.DiscountCurve = "DISCO"; product3.FxConversionType = FxConversionType.AverageThenConvert; var pfolio = new Portfolio { Instruments = new List <IInstrument> { product1, product2, product3 } }; var settings = new McSettings { Generator = RandomGeneratorType.MersenneTwister, NumberOfPaths = (int)2.0.IntPow(15), NumberOfTimesteps = 1, ReportingCurrency = zar, Parallelize = false, LocalCorrelation = true, }; var sut = new AssetFxMCModel(buildDate, pfolio, aModel, settings, TestProviderHelper.CurrencyProvider, TestProviderHelper.FutureSettingsProvider, TestProviderHelper.CalendarProvider); return(sut); }