OptionletVolatilityStructure makeCapVolCurve(Date todaysDate) { double[] vols = {14.40, 17.15, 16.81, 16.64, 16.17, 15.78, 15.40, 15.21, 14.86}; List<Date> dates=new List<Date>() ; List<double> capletVols=new List<double>(); LiborForwardModelProcess process= new LiborForwardModelProcess(10, makeIndex()); for (int i=0; i < 9; ++i) { capletVols.Add(vols[i]/100); dates.Add(process.fixingDates()[i+1]); } return new CapletVarianceCurve(todaysDate, dates, capletVols,new Actual360()); }
// calculating swaption volatility matrix using // Rebonatos approx. formula. Be aware that this // matrix is valid only for regular fixings and // assumes that the fix and floating leg have the // same frequency public SwaptionVolatilityMatrix getSwaptionVolatilityMatrix() { if (swaptionVola != null) { return(swaptionVola); } IborIndex index = process_.index(); Date today = process_.fixingDates()[0]; int size = process_.size() / 2; Matrix volatilities = new Matrix(size, size); List <Date> exercises = new InitializedList <Date>(size); for (int i = 0; i < size; ++i) { exercises[i] = process_.fixingDates()[i + 1]; } List <Period> lengths = new InitializedList <Period>(size); for (int i = 0; i < size; ++i) { lengths[i] = (i + 1) * index.tenor(); } Vector f = process_.initialValues(); for (int k = 0; k < size; ++k) { int alpha = k; double t_alpha = process_.fixingTimes()[alpha + 1]; Matrix var = new Matrix(size, size); for (int i = alpha + 1; i <= k + size; ++i) { for (int j = i; j <= k + size; ++j) { var[i - alpha - 1, j - alpha - 1] = var[j - alpha - 1, i - alpha - 1] = covarProxy_.integratedCovariance(i, j, t_alpha, null); } } for (int l = 1; l <= size; ++l) { int beta = l + k; Vector w = w_0(alpha, beta); double sum = 0.0; for (int i = alpha + 1; i <= beta; ++i) { for (int j = alpha + 1; j <= beta; ++j) { sum += w[i] * w[j] * f[i] * f[j] * var[i - alpha - 1, j - alpha - 1]; } } volatilities[k, l - 1] = Math.Sqrt(sum / t_alpha) / S_0(alpha, beta); } } return(swaptionVola = new SwaptionVolatilityMatrix(today, exercises, lengths, volatilities, index.dayCounter())); }
CapletVarianceCurve makeCapVolCurve(Date todaysDate) { double[] vols = {14.40, 17.15, 16.81, 16.64, 16.17, 15.78, 15.40, 15.21, 14.86, 14.54}; List<Date> dates = new List<Date>(); List<double> capletVols = new List<double>(); LiborForwardModelProcess process= new LiborForwardModelProcess(len+1, makeIndex(),null); for (int i=0; i < len; ++i) { capletVols.Add(vols[i]/100); dates.Add(process.fixingDates()[i+1]); } return new CapletVarianceCurve( todaysDate, dates, capletVols, new ActualActual()); }
public void testSwaptionPricing() { //"Testing forward swap and swaption pricing..."); //SavedSettings backup; const int size = 10; const int steps = 8*size; #if QL_USE_INDEXED_COUPON const double tolerance = 1e-6; #else const double tolerance = 1e-12; #endif List<Date> dates = new List<Date>(); List<double> rates = new List<double>(); dates.Add(new Date(4,9,2005)); dates.Add(new Date(4,9,2011)); rates.Add(0.04); rates.Add(0.08); IborIndex index = makeIndex(dates, rates); LiborForwardModelProcess process = new LiborForwardModelProcess(size, index); LmCorrelationModel corrModel = new LmExponentialCorrelationModel(size, 0.5); LmVolatilityModel volaModel = new LmLinearExponentialVolatilityModel(process.fixingTimes(), 0.291, 1.483, 0.116, 0.00001); // set-up pricing engine process.setCovarParam((LfmCovarianceParameterization) new LfmCovarianceProxy(volaModel, corrModel)); // set-up a small Monte-Carlo simulation to price swations List<double> tmp = process.fixingTimes(); TimeGrid grid=new TimeGrid(tmp ,steps); List<int> location=new List<int>(); for (int i=0; i < tmp.Count; ++i) { location.Add(grid.index(tmp[i])) ; } ulong seed=42; const int nrTrails = 5000; LowDiscrepancy.icInstance = new InverseCumulativeNormal(); IRNG rsg = (InverseCumulativeRsg<RandomSequenceGenerator<MersenneTwisterUniformRng> ,InverseCumulativeNormal>) new PseudoRandom().make_sequence_generator(process.factors()*(grid.size()-1),seed); MultiPathGenerator<IRNG> generator=new MultiPathGenerator<IRNG>(process, grid, rsg, false); LiborForwardModel liborModel = new LiborForwardModel(process, volaModel, corrModel); Calendar calendar = index.fixingCalendar(); DayCounter dayCounter = index.forwardingTermStructure().link.dayCounter(); BusinessDayConvention convention = index.businessDayConvention(); Date settlement = index.forwardingTermStructure().link.referenceDate(); SwaptionVolatilityMatrix m = liborModel.getSwaptionVolatilityMatrix(); for (int i=1; i < size; ++i) { for (int j=1; j <= size-i; ++j) { Date fwdStart = settlement + new Period(6*i, TimeUnit.Months); Date fwdMaturity = fwdStart + new Period(6*j, TimeUnit.Months); Schedule schedule =new Schedule(fwdStart, fwdMaturity, index.tenor(), calendar, convention, convention, DateGeneration.Rule.Forward, false); double swapRate = 0.0404; VanillaSwap forwardSwap = new VanillaSwap(VanillaSwap.Type.Receiver, 1.0, schedule, swapRate, dayCounter, schedule, index, 0.0, index.dayCounter()); forwardSwap.setPricingEngine(new DiscountingSwapEngine(index.forwardingTermStructure())); // check forward pricing first double expected = forwardSwap.fairRate(); double calculated = liborModel.S_0(i-1,i+j-1); if (Math.Abs(expected - calculated) > tolerance) Assert.Fail("Failed to reproduce fair forward swap rate" + "\n calculated: " + calculated + "\n expected: " + expected); swapRate = forwardSwap.fairRate(); forwardSwap = new VanillaSwap(VanillaSwap.Type.Receiver, 1.0, schedule, swapRate, dayCounter, schedule, index, 0.0, index.dayCounter()); forwardSwap.setPricingEngine(new DiscountingSwapEngine(index.forwardingTermStructure())); if (i == j && i<=size/2) { IPricingEngine engine = new LfmSwaptionEngine(liborModel, index.forwardingTermStructure()); Exercise exercise = new EuropeanExercise(process.fixingDates()[i]); Swaption swaption = new Swaption(forwardSwap, exercise); swaption.setPricingEngine(engine); GeneralStatistics stat = new GeneralStatistics(); for (int n=0; n<nrTrails; ++n) { Sample<MultiPath> path = (n%2!=0) ? generator.antithetic() : generator.next(); //Sample<MultiPath> path = generator.next(); List<double> rates_ = new InitializedList<double>(size); for (int k=0; k<process.size(); ++k) { rates_[k] = path.value[k][location[i]]; } List<double> dis = process.discountBond(rates_); double npv=0.0; for (int k=i; k < i+j; ++k) { npv += (swapRate - rates_[k]) * ( process.accrualEndTimes()[k] - process.accrualStartTimes()[k])*dis[k]; } stat.add(Math.Max(npv, 0.0)); } if (Math.Abs(swaption.NPV() - stat.mean()) > stat.errorEstimate()*2.35) Assert.Fail("Failed to reproduce swaption npv" + "\n calculated: " + stat.mean() + "\n expected: " + swaption.NPV()); } } } }
public LfmHullWhiteParameterization( LiborForwardModelProcess process, OptionletVolatilityStructure capletVol, Matrix correlation, int factors) : base(process.size(), factors) { diffusion_ = new Matrix(size_ - 1, factors_); fixingTimes_ = process.fixingTimes(); Matrix sqrtCorr = new Matrix(size_ - 1, factors_, 1.0); if (correlation.empty()) { if (!(factors_ == 1)) { throw new ApplicationException("correlation matrix must be given for " + "multi factor models"); } } else { if (!(correlation.rows() == size_ - 1 && correlation.rows() == correlation.columns())) { throw new ApplicationException("wrong dimesion of the correlation matrix"); } if (!(factors_ <= size_ - 1)) { throw new ApplicationException("too many factors for given LFM process"); } Matrix tmpSqrtCorr = MatrixUtilitites.pseudoSqrt(correlation, MatrixUtilitites.SalvagingAlgorithm.Spectral); // reduce to n factor model // "Reconstructing a valid correlation matrix from invalid data" // (<http://www.quarchome.org/correlationmatrix.pdf>) for (int i = 0; i < size_ - 1; ++i) { double d = 0; tmpSqrtCorr.row(i).GetRange(0, factors_).ForEach((ii, vv) => d += vv * tmpSqrtCorr.row(i)[ii]); //sqrtCorr.row(i).GetRange(0, factors_).ForEach((ii, vv) => sqrtCorr.row(i)[ii] = tmpSqrtCorr.row(i).GetRange(0, factors_)[ii] / Math.Sqrt(d)); for (int k = 0; k < factors_; ++k) { sqrtCorr[i, k] = tmpSqrtCorr.row(i).GetRange(0, factors_)[k] / Math.Sqrt(d); } } } List <double> lambda = new List <double>(); DayCounter dayCounter = process.index().dayCounter(); List <double> fixingTimes = process.fixingTimes(); List <Date> fixingDates = process.fixingDates(); for (int i = 1; i < size_; ++i) { double cumVar = 0.0; for (int j = 1; j < i; ++j) { cumVar += lambda[i - j - 1] * lambda[i - j - 1] * (fixingTimes[j + 1] - fixingTimes[j]); } double vol = capletVol.volatility(fixingDates[i], 0.0, false); double var = vol * vol * capletVol.dayCounter().yearFraction(fixingDates[0], fixingDates[i]); lambda.Add(Math.Sqrt((var - cumVar) / (fixingTimes[1] - fixingTimes[0]))); for (int q = 0; q < factors_; ++q) { diffusion_[i - 1, q] = sqrtCorr[i - 1, q] * lambda.Last(); } } covariance_ = diffusion_ * Matrix.transpose(diffusion_); }
public LfmHullWhiteParameterization( LiborForwardModelProcess process, OptionletVolatilityStructure capletVol, Matrix correlation, int factors) : base(process.size(), factors) { diffusion_ = new Matrix(size_-1, factors_); fixingTimes_= process.fixingTimes(); Matrix sqrtCorr = new Matrix(size_ - 1, factors_, 1.0); if (correlation.empty()) { if(!(factors_ == 1)) throw new ApplicationException("correlation matrix must be given for "+ "multi factor models"); } else { if(!(correlation.rows() == size_-1 && correlation.rows() == correlation.columns())) throw new ApplicationException("wrong dimesion of the correlation matrix"); if(!(factors_ <= size_-1)) throw new ApplicationException("too many factors for given LFM process"); Matrix tmpSqrtCorr =MatrixUtilitites.pseudoSqrt(correlation, MatrixUtilitites.SalvagingAlgorithm.Spectral); // reduce to n factor model // "Reconstructing a valid correlation matrix from invalid data" // (<http://www.quarchome.org/correlationmatrix.pdf>) for (int i=0; i < size_-1; ++i) { double d = 0; tmpSqrtCorr.row(i).GetRange(0, factors_).ForEach((ii, vv) => d += vv*tmpSqrtCorr.row(i)[ii]); //sqrtCorr.row(i).GetRange(0, factors_).ForEach((ii, vv) => sqrtCorr.row(i)[ii] = tmpSqrtCorr.row(i).GetRange(0, factors_)[ii] / Math.Sqrt(d)); for (int k = 0; k < factors_; ++k){ sqrtCorr[i, k] = tmpSqrtCorr.row(i).GetRange(0, factors_)[k] / Math.Sqrt(d); } } } List<double> lambda=new List<double>(); DayCounter dayCounter = process.index().dayCounter(); List<double> fixingTimes = process.fixingTimes(); List<Date> fixingDates = process.fixingDates(); for (int i = 1; i < size_; ++i) { double cumVar = 0.0; for (int j = 1; j < i; ++j) { cumVar += lambda[i-j-1] * lambda[i-j-1] * (fixingTimes[j+1] - fixingTimes[j]); } double vol = capletVol.volatility(fixingDates[i], 0.0,false); double var = vol * vol * capletVol.dayCounter().yearFraction(fixingDates[0], fixingDates[i]); lambda.Add(Math.Sqrt( (var - cumVar) / (fixingTimes[1] - fixingTimes[0])) ); for (int q=0; q<factors_; ++q) { diffusion_[i - 1, q]=sqrtCorr[i - 1, q] * lambda.Last() ; } } covariance_ = diffusion_ * Matrix.transpose(diffusion_); }