public LiborForwardModel(LiborForwardModelProcess process, LmVolatilityModel volaModel, LmCorrelationModel corrModel) : base(volaModel.parameters().Count + corrModel.parameters().Count) { f_ = new InitializedList <double>(process.size()); accrualPeriod_ = new InitializedList <double>(process.size()); covarProxy_ = new LfmCovarianceProxy(volaModel, corrModel); process_ = process; int k = volaModel.parameters().Count; for (int j = 0; j < k; j++) { arguments_[j] = volaModel.parameters()[j]; } for (int j = 0; j < corrModel.parameters().Count; j++) { arguments_[j + k] = corrModel.parameters()[j]; } for (int i = 0; i < process.size(); ++i) { accrualPeriod_[i] = process.accrualEndTimes()[i] - process.accrualStartTimes()[i]; f_[i] = 1.0 / (1.0 + accrualPeriod_[i] * process_.initialValues()[i]); } }
public double discountBondOption(Option.Type type, double strike, double maturity, double bondMaturity) { List <double> accrualStartTimes = process_.accrualStartTimes(); List <double> accrualEndTimes = process_.accrualEndTimes(); Utils.QL_REQUIRE(accrualStartTimes.First() <= maturity && accrualStartTimes.Last() >= maturity, () => "capet maturity does not fit to the process"); int i = accrualStartTimes.BinarySearch(maturity); if (i < 0) { // The lower_bound() algorithm finds the first position in a sequence that value can occupy // without violating the sequence's ordering // if BinarySearch does not find value the value, the index of the prev minor item is returned i = ~i + 1; } // impose limits. we need the one before last at max or the first at min i = Math.Max(Math.Min(i, accrualStartTimes.Count - 1), 0); Utils.QL_REQUIRE(i < process_.size() && Math.Abs(maturity - accrualStartTimes[i]) < 100 * Const.QL_EPSILON && Math.Abs(bondMaturity - accrualEndTimes[i]) < 100 * Const.QL_EPSILON, () => "irregular fixings are not (yet) supported"); double tenor = accrualEndTimes[i] - accrualStartTimes[i]; double forward = process_.initialValues()[i]; double capRate = (1.0 / strike - 1.0) / tenor; double var = covarProxy_.integratedCovariance(i, i, process_.fixingTimes()[i]); double dis = process_.index().forwardingTermStructure().link.discount(bondMaturity); double black = Utils.blackFormula( (type == Option.Type.Put ? Option.Type.Call : Option.Type.Put), capRate, forward, Math.Sqrt(var)); double npv = dis * tenor * black; return(npv / (1.0 + capRate * tenor)); }
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()) { Utils.QL_REQUIRE(factors_ == 1, () => "correlation matrix must be given for multi factor models"); } else { Utils.QL_REQUIRE(correlation.rows() == size_ - 1 && correlation.rows() == correlation.columns(), () => "wrong dimesion of the correlation matrix"); Utils.QL_REQUIRE(factors_ <= size_ - 1, () => "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]); 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_); }