MC *new_NPV(MC *var1) { MC *ans = NPV(var1); return ans; }
Disposable<Array> values(const Array &v) const { // transformations int type = type_; // start with same type as non standard // underlying (1 means payer, -1 receiver) Real nominal = fabs(v[0]); if (v[0] < 0.0) type *= -1; Real maturity = std::min(fabs(v[1]), maxMaturity_); Real fixedRate = v[2]; // allow for negative rates explicitly // (though it might not be reasonable for calibration depending // on the model to calibrate and the market instrument quotation) Size years = (Size)std::floor(maturity); maturity -= (Real)years; maturity *= 12.0; Size months = (Size)std::floor(maturity); Real alpha = 1.0 - (maturity - (Real)months); if (years == 0 && months == 0) { months = 1; // ensure a maturity of at least one month ... alpha = 1.0; // ... but in this case only look at the lower // maturity swap } // maturity -= (Real)months; maturity *= 365.25; // Size days = (Size)std::floor(maturity); // Real alpha = 1.0-(maturity-(Real)days); // generate swap Period lowerPeriod = years * Years + months * Months; //+days*Days; Period upperPeriod = lowerPeriod + 1 * Months; // 1*Days; boost::shared_ptr<SwapIndex> tmpIndexLower, tmpIndexUpper; tmpIndexLower = indexBase_->clone(lowerPeriod); tmpIndexUpper = indexBase_->clone(upperPeriod); boost::shared_ptr<VanillaSwap> swapLower = tmpIndexLower->underlyingSwap(expiry_); boost::shared_ptr<VanillaSwap> swapUpper = tmpIndexUpper->underlyingSwap(expiry_); // compute npv, delta, gamma Real npvm = alpha * NPV(swapLower, fixedRate, nominal, -h_, type) + (1.0 - alpha) * NPV(swapUpper, fixedRate, nominal, -h_, type); Real npv = alpha * NPV(swapLower, fixedRate, nominal, 0.0, type) + (1.0 - alpha) * NPV(swapUpper, fixedRate, nominal, 0.0, type); Real npvu = alpha * NPV(swapLower, fixedRate, nominal, h_, type) + (1.0 - alpha) * NPV(swapUpper, fixedRate, nominal, h_, type); Real delta = (npvu - npvm) / (2.0 * h_); Real gamma = (npvu - 2.0 * npv + npvm) / (h_ * h_); // debug output global standard underlying npv // Real xtmp = -5.0; // std::cout << "globalStandardNpv;"; // while (xtmp <= 5.0 + QL_EPSILON) { // std::cout << alpha *NPV(swapLower, fixedRate, nominal, xtmp, // type) + // (1.0 - alpha) * NPV(swapUpper, fixedRate, // nominal, xtmp, type) // << ";"; // xtmp += 0.1; // } // std::cout << std::endl; // end debug output // return target function values Array res(3); res[0] = (npv - npv_) / delta_; res[1] = (delta - delta_) / delta_; res[2] = (gamma - gamma_) / gamma_; return res; }