Beispiel #1
0
        public string[] GetKeyTenors()
        {
            return(MarketInstruments != null
                                ? MarketInstruments.Select(x => x.Instrument.Tenor).Select(x => x.ToString()).ToArray()
                                : KeyTenors);

            //KeyPoints.Select(x => new Term(x.Item1 - ReferenceDate, Period.Day)).Select(x => x.ToString()).ToArray();
        }
Beispiel #2
0
        public IYieldCurve BumpKeyRate(int index, double resetRate)
        {
            if (index < 0 || index > KeyPoints.Count() - 1)
            {
                throw new IndexOutOfRangeException("YieldCurve");
            }


            if (MarketInstruments == null)
            {
                //var offset = MarketInstruments == null ? 0 : KeyPoints.Length - MarketInstruments.Length;
                //var additionalBumpIndex = (index == 0 && offset > 0) ? 0 : Int32.MaxValue;
                return(new YieldCurve(
                           Name,
                           ReferenceDate,
                           KeyPoints.Select((x, i) => i == index ? Tuple.Create(x.Item1, resetRate) : x).ToArray(),
                           Bda,
                           DayCount,
                           Calendar,
                           Currency,
                           Compound,
                           Interpolation,
                           Trait,
                           BaseMarket,
                           CalibrateMktUpdateCondition,
                           Spread
                           ));
            }
            else
            {
                return(new YieldCurve(
                           Name,
                           ReferenceDate,
                           MarketInstruments.Select((x, i) => i == index ? new MarketInstrument(x.Instrument.Bump(resetRate), resetRate, x.CalibMethod) : x as MarketInstrument).ToArray(),
                           Bda,
                           DayCount,
                           Calendar,
                           Currency,
                           Compound,
                           Interpolation,
                           Trait,
                           BaseMarket,
                           CalibrateMktUpdateCondition,
                           null
                           )
                {
                    Spread = Spread
                });
            }
        }
Beispiel #3
0
 public IYieldCurve Shift(int bp)
 {
     if (MarketInstruments == null)
     {
         return(new YieldCurve(
                    Name,
                    ReferenceDate,
                    KeyPoints.Select(x => Tuple.Create(x.Item1, x.Item2 + bp * 0.0001)).ToArray(),
                    Bda,
                    DayCount,
                    Calendar,
                    Currency,
                    Compound,
                    Interpolation,
                    Trait,
                    BaseMarket,
                    CalibrateMktUpdateCondition,
                    Spread
                    ));
     }
     else
     {
         return(new YieldCurve(
                    Name,
                    ReferenceDate,
                    MarketInstruments.Select(x => new MarketInstrument(x.Instrument.Bump(bp), x.TargetValue + bp * 0.0001, x.CalibMethod)).ToArray(),
                    Bda,
                    DayCount,
                    Calendar,
                    Currency,
                    Compound,
                    Interpolation,
                    Trait,
                    BaseMarket,
                    CalibrateMktUpdateCondition,
                    null
                    )
         {
             Spread = Spread
         });
     }
 }
Beispiel #4
0
        //swap curve building for both pricing and pnl,   bond curve building only,  not for pnl
        public YieldCurve(
            string name,
            Date referenceDate,
            MarketInstrument[] marketInstruments,
            BusinessDayConvention bda,
            IDayCount dayCount,
            ICalendar calendar,
            CurrencyCode currency,
            Compound compound,
            Interpolation interpolation,
            YieldCurveTrait trait,
            IMarketCondition baseMarket = null,
            Expression <Func <IMarketCondition, object> >[] calibrateMktUpdateCondition = null,
            ISpread spread     = null,
            Date[] knotPoints  = null,
            string[] keyTenors = null,
            InstrumentCurveDefinition rawDefinition = null
            )
        {
            Name          = name;
            ReferenceDate = referenceDate;
            Currency      = currency;
            Bda           = bda;
            DayCount      = dayCount;
            Compound      = compound;
            Calendar      = calendar;
            Interpolation = interpolation;
            Trait         = trait;
            RawDefinition = rawDefinition;
            var tempMarketInstruments     = marketInstruments.OrderBy(x => x.Instrument.UnderlyingMaturityDate).ToArray();
            var uniqueTenorMktInstruments = new List <MarketInstrument> {
                tempMarketInstruments[0]
            };

            for (var i = 1; i < tempMarketInstruments.Length; ++i)
            {
                if (tempMarketInstruments[i].Instrument.GetCalibrationDate() != tempMarketInstruments[i - 1].Instrument.GetCalibrationDate())
                {
                    uniqueTenorMktInstruments.Add(tempMarketInstruments[i]);
                }
            }
            MarketInstruments = uniqueTenorMktInstruments.ToArray();
            InputRateByTenor  = MarketInstruments.ToDictionary(p => p.Instrument.Tenor, p => p.TargetValue);

            BaseMarket = baseMarket;
            Spread     = spread ?? new ZeroSpread(0.0);
            CalibrateMktUpdateCondition = calibrateMktUpdateCondition;

            if (MarketInstruments.Any(x => x.Instrument is Bond))
            {
                var err = double.NaN;
                KeyPoints    = BondCurveCalibrator.Calibrate(Name, ReferenceDate, MarketInstruments.ToArray(), Bda, DayCount, Calendar, Compound, Interpolation, Trait, Currency, knotPoints, out err, baseMarket, CalibrateMktUpdateCondition);
                fittingError = err;
            }
            else
            {
                KeyPoints = YieldCurveCalibrator.Calibrate(name, ReferenceDate, MarketInstruments.ToArray(), Bda, DayCount, Calendar, Compound, Interpolation, Trait, Currency, baseMarket, CalibrateMktUpdateCondition);
            }


            //if (KeyPoints.Select(x => x.Item1).Any(z => z is Date))
            //{
            //	_curve = new Curve<Date>(ReferenceDate, KeyPoints, x => x.ToOADate(), interpolation);
            //}
            _curveXInYears = new Curve <double>(0.0,
                                                KeyPoints.Select(x => Tuple.Create(DayCount.CalcDayCountFraction(ReferenceDate, x.Item1), x.Item2)).ToArray(),
                                                x => x,
                                                Interpolation);
        }