public double Price(IProcess process, IState st0, double t0, double step, int n) { var ret = new List <double>(); List <IEnumerable <IState> > pathsToStrike = Enumerable.Range(0, n).Select(_ => process.generatePathFrom(st0, t0, step, ExecutionTime)).ToList(); int n2 = (int)Math.Sqrt(n); foreach (var path in pathsToStrike) { double zbp = 1; foreach (var x in path) { zbp *= Math.Exp(-x.r * step); } double bondPriceAtStrike = UnderlyingBond.Price(process, path.Last(), ExecutionTime, step, n2); if (Type == OptionType.Call) { ret.Add(Math.Max(bondPriceAtStrike - StrikePrice, 0)); } else //Put { ret.Add(Math.Max(StrikePrice - bondPriceAtStrike, 0)); } } return(ret.Average()); }
public override AssetValuation Calculate(IInstrumentControllerData modelData) { ModelData = modelData; AnalyticModelParameters = null; // 1. First derive the analytics to be evaluated via the stream controller model // NOTE: These take precedence of the child model metrics if (AnalyticsModel == null) { AnalyticsModel = new BondTransactionAnalytic(); } var bondControllerMetrics = ResolveModelMetrics(AnalyticsModel.Metrics); AssetValuation bondValuation; var quotes = ModelData.AssetValuation.quote.ToList(); if (AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.AccrualFactor.ToString()) == null) { var quote = QuotationHelper.Create(0.0m, InstrumentMetrics.AccrualFactor.ToString(), "DecimalValue"); quotes.Add(quote); } if (AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.FloatingNPV.ToString()) == null) { var quote = QuotationHelper.Create(0.0m, InstrumentMetrics.FloatingNPV.ToString(), "DecimalValue"); quotes.Add(quote); } if (AssetValuationHelper.GetQuotationByMeasureType(ModelData.AssetValuation, InstrumentMetrics.NPV.ToString()) == null) { var quote = QuotationHelper.Create(0.0m, InstrumentMetrics.NPV.ToString(), "DecimalValue"); quotes.Add(quote); } ModelData.AssetValuation.quote = quotes.ToArray(); var marketEnvironment = modelData.MarketEnvironment; IRateCurve rateDiscountCurve = null; //2. Sets the evolution type for coupon and payment calculations. Coupons.PricingStructureEvolutionType = PricingStructureEvolutionType; Coupons.BucketedDates = BucketedDates; Coupons.Multiplier = Multiplier; if (AdditionalPayments != null) { foreach (var payment in AdditionalPayments) { payment.PricingStructureEvolutionType = PricingStructureEvolutionType; payment.BucketedDates = BucketedDates; payment.Multiplier = Multiplier; } } //3. Aggregate the child metrics. List <AssetValuation> childValuations = new List <AssetValuation> { Coupons?.Calculate(modelData) }; if (GetAdditionalPayments() != null) { var paymentControllers = new List <InstrumentControllerBase>(GetAdditionalPayments()); childValuations.AddRange(paymentControllers.Select(payment => payment.Calculate(modelData))); } var childControllerValuations = AssetValuationHelper.AggregateMetrics(childValuations, new List <string>(Metrics), PaymentCurrencies); childControllerValuations.id = Id + ".BondCouponRateStreams"; //4. Calc the asset as a little extra var metrics = ResolveModelMetrics(AnalyticsModel.Metrics); var metricsAsString = metrics.Select(metric => metric.ToString()).ToList(); var controllerData = PriceableAssetFactory.CreateAssetControllerData(metricsAsString.ToArray(), modelData.ValuationDate, modelData.MarketEnvironment); UnderlyingBond.Multiplier = Multiplier; UnderlyingBond.Calculate(controllerData); //5. Now do the bond calculations. if (bondControllerMetrics.Count > 0) { CalculationResults = new BondTransactionResults(); if (marketEnvironment.GetType() == typeof(MarketEnvironment)) { var bondCurve = (IBondCurve)modelData.MarketEnvironment.GetPricingStructure(BondCurveName); if (bondCurve != null) { var marketDataType = bondCurve.GetPricingStructureId().Properties.GetValue <string>(AssetMeasureEnum.MarketQuote.ToString(), false); if (marketDataType != null && marketDataType == BondPriceEnum.YieldToMaturity.ToString()) { IsYTMQuote = true; } //TODO handle the other cases like: AssetSwapSpread; DirtyPrice and ZSpread. var mq = (decimal)bondCurve.GetYieldToMaturity(modelData.ValuationDate, SettlementDate); Quote = BasicQuotationHelper.Create(mq, AssetMeasureEnum.MarketQuote.ToString(), PriceQuoteUnitsEnum.DecimalRate.ToString()); } rateDiscountCurve = (IRateCurve)modelData.MarketEnvironment.GetPricingStructure(DiscountCurveName); } //Generate the vectors const bool isBuyerInd = true; var analyticModelParameters = new BondTransactionParameters { IsBuyerInd = isBuyerInd, AccrualYearFractions = GetCouponAccrualFactors(), Multiplier = Multiplier, Quote = QuoteValue, CouponRate = UnderlyingBond.GetCouponRate(), NotionalAmount = UnderlyingBond.Notional, Frequency = UnderlyingBond.Frequency, IsYTMQuote = IsYTMQuote, AccruedFactor = UnderlyingBond.GetAccruedFactor(), RemainingAccruedFactor = UnderlyingBond.GetRemainingAccruedFactor(), PaymentDiscountFactors = GetDiscountFactors(rateDiscountCurve, Coupons.StreamPaymentDates.ToArray(), modelData.ValuationDate), }; //5. Get the Weightings analyticModelParameters.Weightings = CreateWeightings(CDefaultWeightingValue, analyticModelParameters.PaymentDiscountFactors.Length); //6. Set the analytic input parameters and Calculate the respective metrics AnalyticModelParameters = analyticModelParameters; CalculationResults = AnalyticsModel.Calculate <IBondTransactionResults, BondTransactionResults>(analyticModelParameters, metrics.ToArray()); // Now merge back into the overall stream valuation var bondControllerValuation = GetValue(CalculationResults, modelData.ValuationDate); bondValuation = AssetValuationHelper.UpdateValuation(bondControllerValuation, childControllerValuations, ConvertMetrics(bondControllerMetrics), new List <string>(Metrics)); } else { bondValuation = childControllerValuations; } // store inputs and results from this run CalculationPerformedIndicator = true; bondValuation.id = Id; return(bondValuation); }