CotSwapFromFwdCorrelation::CotSwapFromFwdCorrelation(
            const boost::shared_ptr<PiecewiseConstantCorrelation>& fwdCorr,
            const CurveState& curveState,
            Spread displacement)
    : fwdCorr_(fwdCorr),
      numberOfRates_(fwdCorr->numberOfRates()),
      swapCorrMatrices_(fwdCorr->correlations().size())
    {
        QL_REQUIRE(numberOfRates_==curveState.numberOfRates(),
                   "mismatch between number of rates in fwdCorr (" <<
                   numberOfRates_ << ") and curveState (" <<
                   curveState.numberOfRates() << ")");

        Matrix zed = SwapForwardMappings::coterminalSwapZedMatrix(
                                                curveState, displacement);
        Matrix zedT = transpose(zed);
        const std::vector<Matrix>& fwdCorrMatrices = fwdCorr->correlations();
        for (Size k = 0; k<fwdCorrMatrices.size(); ++k) {
            swapCorrMatrices_[k] = CovarianceDecomposition(
                zed * fwdCorrMatrices[k] * zedT).correlationMatrix();
            // zero expired rates' correlation coefficients
            const std::vector<Time>& rateTimes = curveState.rateTimes();
            const std::vector<Time>& corrTimes = fwdCorr_->times();
            for (Size i=0; i<numberOfRates_; ++i)
                for (Size j=0; j<=i; ++j)
                    if (corrTimes[k]>rateTimes[j])
                        swapCorrMatrices_[k][i][j] =
                            swapCorrMatrices_[k][j][i] = 0.0;
        }
    }
    bool MultiStepSwaption::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                               genCashFlows)
    {
        if (currentIndex_ == startIndex_)
        {
            genCashFlows[0][0].timeIndex = 0;


            Rate swapRate = currentState.cmSwapRate(startIndex_,endIndex_-startIndex_);
            Real annuity = currentState.cmSwapAnnuity(startIndex_,startIndex_,endIndex_-startIndex_);

            genCashFlows[0][0].amount =
                (*payoff_)(swapRate) * annuity;
             
            numberCashFlowsThisStep[0] =genCashFlows[0][0].amount != 0.0 ? 1 : 0 ;

            return true;
        }
        else
        {
            numberCashFlowsThisStep[0] =0;
            ++currentIndex_;
            return false;
        }
    }
    bool MultiStepSwaption::nextTimeStep(
        const CurveState &currentState,
        std::vector<Size> &numberCashFlowsThisStep,
        std::vector<std::vector<MarketModelMultiProduct::CashFlow> > &
            genCashFlows) {
        if (currentIndex_ == startIndex_) {
            genCashFlows[0][0].timeIndex = 0;
            // Rate swapRate =
            // currentState.cmSwapRate(startIndex_,endIndex_-startIndex_);
            // Real annuity =
            // currentState.cmSwapAnnuity(startIndex_,startIndex_,endIndex_-startIndex_);
            Rate swapRate =
                currentState.swapRate(startIndex_, endIndex_, step_);
            Real annuity = currentState.swapAnnuity(startIndex_, startIndex_,
                                                    endIndex_, step_);

            genCashFlows[0][0].amount = (*payoff_)(swapRate) * annuity;
            // std::max(swapRate-payoff_->strike(),0.0) * annuity; // test

            numberCashFlowsThisStep[0] =
                genCashFlows[0][0].amount != 0.0 ? 1 : 0;
        } else {
            numberCashFlowsThisStep[0] = 0;
        }

        ++currentIndex_;
        return currentIndex_ > startIndex_;
    }
Exemple #4
0
    void SwapBasisSystem::values(const CurveState& currentState,
                                 std::vector<Real>& results) const {
        Size rateIndex = rateIndex_[currentIndex_-1];

        results.reserve(3);
        results.resize(2);
        results[0] = 1.0;
        results[1] = currentState.forwardRate(rateIndex);
        if (rateIndex < rateTimes_.size()-2)
            results.push_back(currentState.coterminalSwapRate(rateIndex+1));
    }
    void BermudanSwaptionExerciseValue::nextStep(const CurveState& state) {
        const Payoff& p = (*payoffs_[currentIndex_]);
        Real value = state.coterminalSwapAnnuity(currentIndex_, currentIndex_) *
            p(state.coterminalSwapRate(currentIndex_));

     //   value /= state.discountRatios()[currentIndex_];
        value =  std::max(value, 0.0);
        cf_.timeIndex = currentIndex_;
        cf_.amount = value;
        ++currentIndex_;
    }
    bool OneStepCoterminalSwaps::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                               genCashFlows) {
        std::fill(numberCashFlowsThisStep.begin(),
                  numberCashFlowsThisStep.end(),0);

        for (Size indexOfTime=0;indexOfTime<lastIndex_;indexOfTime++) {
            Rate liborRate = currentState.forwardRate(indexOfTime);

            for (Size i=0;i<=indexOfTime;i++) {
                genCashFlows[i][(indexOfTime-i)*2].timeIndex = indexOfTime;
                genCashFlows[i][(indexOfTime-i)*2].amount =
                    -fixedRate_*fixedAccruals_[indexOfTime];

                genCashFlows[i][(indexOfTime-i)*2+1].timeIndex = indexOfTime;
                genCashFlows[i][(indexOfTime-i)*2+1].amount =
                    liborRate*floatingAccruals_[indexOfTime];

                numberCashFlowsThisStep[i] += 2;
            }
        }
        return true ;
    }
Exemple #7
0
    bool MultiStepTarn::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                                 genCashFlows)
    {
        Rate liborRate = currentState.forwardRate(currentIndex_);
        
        numberCashFlowsThisStep[0] =2;
        
        genCashFlows[0][0].amount = (liborRate+floatingSpreads_[currentIndex_])*accrualsFloating_[currentIndex_];
        genCashFlows[0][0].timeIndex = lastIndex_ + currentIndex_;

        genCashFlows[0][1].timeIndex =  currentIndex_;

        Real obviousCoupon = std::max(strikes_[currentIndex_] - multipliers_[currentIndex_]*liborRate,0.0)*accruals_[currentIndex_];

        couponPaid_+= obviousCoupon;

        ++currentIndex_;

        if (couponPaid_ < totalCoupon_ && currentIndex_ < lastIndex_ )
        {
              genCashFlows[0][1].amount = -  obviousCoupon;
              return false;
        }

        Real coupon = obviousCoupon +(totalCoupon_ -couponPaid_);
        genCashFlows[0][1].amount = - coupon;

        return true;
    }
    bool MultiStepVolSwap::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >& genCashFlows)
    {

		//std::cout << "currentIndex=" << currentIndex_ << std::endl;

		Rate referenceRate = currentState.swapRate(referenceRateIndices_.first, referenceRateIndices_.second, referenceRateStep_ );
		
		for(Size i=8; i>=1; i--) referenceRateFixings_[i] = referenceRateFixings_[i-1];
		referenceRateFixings_[0] = referenceRate;

		Size noCf=0;

		if(currentIndex_ == floatingFixingIndices_[currentFloatingIndex_]) {
	        Rate liborRate = currentState.forwardRate(currentFloatingIndex_);
			//std::cout << "generate float payment @" << floatingPaymentIndices_[currentFloatingIndex_] << std::endl;
			genCashFlows[0][noCf].timeIndex = floatingPaymentIndices_[currentFloatingIndex_];
			genCashFlows[0][noCf].amount = (payer_ ? 1.0 : -1.0)*liborRate*floatingAccruals_[currentFloatingIndex_];
			noCf++;
			currentFloatingIndex_++;
		}

		if(currentIndex_ == structuredFixingIndices_[currentStructuredIndex_]) {
			//std::cout << "generate structured payment @" << paymentTimes_[structuredPaymentIndices_[currentStructuredIndex_]] << std::endl;
			Real volIdx = (fabs(referenceRateFixings_[1]-referenceRateFixings_[5])+
				          fabs(referenceRateFixings_[2]-referenceRateFixings_[6])+
						  fabs(referenceRateFixings_[3]-referenceRateFixings_[7])+
						  fabs(referenceRateFixings_[4]-referenceRateFixings_[8])) / 4.0;
			genCashFlows[0][noCf].timeIndex = structuredPaymentIndices_[currentStructuredIndex_];
			genCashFlows[0][noCf].amount = ( (currentStructuredIndex_== (Size)filterStructuredIndex_ || filterStructuredIndex_== -1) ? 1.0 : 0.0)*(payer_ ? -1.0 : 1.0)*structuredAccruals_[currentStructuredIndex_]*std::max(floor_,fixedRate_+multiplier_*volIdx);
			//genCashFlows[0][noCf].amount = (currentIndex_==structuredFixingIndices_[0] ? 1.0 : 0.0)*std::max(referenceRate-fixedRate_,0.0)*currentState.swapAnnuity(12,12,52,2); // TEST (plain vanilla swaption payoff)
			noCf++;
			currentStructuredIndex_++;
		}

        numberCashFlowsThisStep[0] = noCf;

        ++currentIndex_;

		bool done = (floatingFixingIndices_[currentFloatingIndex_] == QL_MAX_INTEGER && structuredFixingIndices_[currentStructuredIndex_] == QL_MAX_INTEGER );

		//std::cout << "ok, finished = " << done << std::endl;

        return done; 
    }
    bool MultiStepCoterminalSwaptions::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                               genCashFlows)
    {
        genCashFlows[currentIndex_][0].timeIndex = currentIndex_;

        Rate swapRate = currentState.coterminalSwapRate(currentIndex_);
        Real annuity = currentState.coterminalSwapAnnuity(currentIndex_, currentIndex_);
        genCashFlows[currentIndex_][0].amount =
            (*payoffs_[currentIndex_])(swapRate) * annuity;
        std::fill(numberCashFlowsThisStep.begin(),
                  numberCashFlowsThisStep.end(),0);
        numberCashFlowsThisStep[currentIndex_] = 1;
        ++currentIndex_;
        return (currentIndex_ == lastIndex_);
    }
Exemple #10
0
 bool MultiStepForwards::nextTimeStep(
         const CurveState& currentState,
         std::vector<Size>& numberCashFlowsThisStep,
         std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                            genCashFlows) {
     Rate liborRate = currentState.forwardRate(currentIndex_);
     genCashFlows[currentIndex_][0].timeIndex = currentIndex_;
     genCashFlows[currentIndex_][0].amount =
         (liborRate-strikes_[currentIndex_])*accruals_[currentIndex_];
     std::fill(numberCashFlowsThisStep.begin(),
               numberCashFlowsThisStep.end(),0);
     numberCashFlowsThisStep[currentIndex_] = 1;
     ++currentIndex_;
     return (currentIndex_ == strikes_.size());
 }
 void CustomBasisSystem::values(const CurveState& currentState,
                              std::vector<Real>& results) const {
     results.reserve(startByCurrentIndex_.size()+1);
     results.resize(1);
     results[0] = 1.0;
     //        std::cout << "Custom basis system:" << std::endl;
     for(Size i=0;i<startByCurrentIndex_.size();i++) {
         results.push_back(currentState.swapRate(startByCurrentIndex_[i](currentIndex_),
                                                 endByCurrentIndex_[i](currentIndex_),
                                                 step_));
         //            std::cout << "rate (" << startByCurrentIndex_[i](currentIndex_) << ";" <<
         //    endByCurrentIndex_[i](currentIndex_) << ";" << step_ << ") " << results.back() << std::endl;
     }
     
 }
Exemple #12
0
    bool OneStepOptionlets::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                               genCashFlows) {
        std::fill(numberCashFlowsThisStep.begin(),
                  numberCashFlowsThisStep.end(), 0);
        for (Size i=0; i<payoffs_.size(); ++i) {
            Rate liborRate = currentState.forwardRate(i);
            Real payoff = (*payoffs_[i])(liborRate);
            if (payoff>0.0) {
                numberCashFlowsThisStep[i] = 1;
                genCashFlows[i][0].timeIndex = i;
                genCashFlows[i][0].amount = payoff*accruals_[i];
            }
        }

        return true;
    }
Exemple #13
0
void LongstaffSchwartzExerciseStrategy::nextStep(
    const CurveState& currentState) {
    principalInNumerairePortfolio_ = newPrincipal_;

    if (isRebateTime_[currentIndex_])
        exercise_->nextStep(currentState);
    if (isControlTime_[currentIndex_])
        control_->nextStep(currentState);
    if (isBasisTime_[currentIndex_])
        basisSystem_->nextStep(currentState);

    if (currentIndex_ < numeraires_.size()-1) {
        Size numeraire = numeraires_[currentIndex_];
        Size nextNumeraire = numeraires_[currentIndex_+1];
        newPrincipal_ *=
            currentState.discountRatio(numeraire, nextNumeraire);
    }

    ++currentIndex_;
}
    bool MarketModelPathwiseSwap::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelPathwiseMultiProduct::CashFlow> >& cashFlowsGenerated) 
    {
        Rate liborRate = currentState.forwardRate(currentIndex_);
        cashFlowsGenerated[0][0].timeIndex = currentIndex_+1;

        cashFlowsGenerated[0][0].amount[0] =
                     (liborRate-strikes_[currentIndex_])*accruals_[currentIndex_]*multiplier_;

        numberCashFlowsThisStep[0] = 1;
        
        for (Size i=1; i <= numberRates_; ++i)
                cashFlowsGenerated[0][0].amount[i] =0;

        cashFlowsGenerated[0][0].amount[currentIndex_+1] = accruals_[currentIndex_]*multiplier_;
            
        ++currentIndex_;
        return (currentIndex_ == strikes_.size());
    }
    bool MultiStepRatchet::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                                 genCashFlows)
    {
        Rate liborRate = currentState.forwardRate(currentIndex_);
        Real currentCoupon = std::max(gearingOfFloor_* floor_ + spreadOfFloor_,
                                      gearingOfFixing_* liborRate + spreadOfFixing_);

        genCashFlows[0][0].timeIndex = currentIndex_;
        genCashFlows[0][0].amount =
            multiplier_* accruals_[currentIndex_]*currentCoupon;

        //floor_ = liborRate;                           //StepRatchet
        floor_ = currentCoupon;                         //FullRatchet
        numberCashFlowsThisStep[0] = 1;

        ++currentIndex_;

        return (currentIndex_ == lastIndex_);
    }
Exemple #16
0
    bool MultiStepSwap::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                                 genCashFlows)
    {
        Rate liborRate = currentState.forwardRate(currentIndex_);

        genCashFlows[0][0].timeIndex = currentIndex_;
        genCashFlows[0][0].amount =
            -multiplier_*fixedRate_*fixedAccruals_[currentIndex_];

        genCashFlows[0][1].timeIndex = currentIndex_;
        genCashFlows[0][1].amount =
            multiplier_*liborRate*floatingAccruals_[currentIndex_];

        numberCashFlowsThisStep[0] = 2;

        ++currentIndex_;

        return (currentIndex_ == lastIndex_);
    }
    bool MarketModelPathwiseInverseFloater::nextTimeStep(
        const CurveState& currentState,
        std::vector<Size>& numberCashFlowsThisStep,
        std::vector<std::vector<MarketModelPathwiseMultiProduct::CashFlow> >& cashFlowsGenerated) 
    {
        numberCashFlowsThisStep[0] =1 ;
        for (Size i=1; i <= lastIndex_; ++i)
            cashFlowsGenerated[0][0].amount[i] =0;

        Rate liborRate = currentState.forwardRate(currentIndex_);
        Real inverseFloatingCoupon = std::max((fixedStrikes_[currentIndex_] - fixedMultipliers_[currentIndex_]*liborRate),0.0)*fixedAccruals_[currentIndex_] ;
        Real floatingCoupon = (liborRate+floatingSpreads_[currentIndex_])*floatingAccruals_[currentIndex_];
        cashFlowsGenerated[0][0].timeIndex = currentIndex_;
        cashFlowsGenerated[0][0].amount[0] =multiplier_*(inverseFloatingCoupon - floatingCoupon);
 
 

        if (inverseFloatingCoupon > 0.0)
        {

            cashFlowsGenerated[0][0].amount[currentIndex_+1] =multiplier_*( - fixedMultipliers_[currentIndex_]*fixedAccruals_[currentIndex_]  - floatingAccruals_[currentIndex_]);


        }
        else
        {

            cashFlowsGenerated[0][0].amount[currentIndex_+1] =-multiplier_*floatingAccruals_[currentIndex_];

        }

        ++currentIndex_;

        return (currentIndex_ == lastIndex_);


    }
    bool MultiStepCoinitialSwaps::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                               genCashFlows) {
        Rate liborRate = currentState.forwardRate(currentIndex_);
        std::fill(numberCashFlowsThisStep.begin(),
                  numberCashFlowsThisStep.end(),0);

        for (Size i=currentIndex_;i<lastIndex_;i++) {
            genCashFlows[i][0].timeIndex = currentIndex_;
            genCashFlows[i][0].amount =
                -fixedRate_*fixedAccruals_[currentIndex_];

            genCashFlows[i][1].timeIndex = currentIndex_;
            genCashFlows[i][1].amount =
                liborRate*floatingAccruals_[currentIndex_];

            numberCashFlowsThisStep[i] = 2;
        }
        ++currentIndex_;

        return (currentIndex_ == lastIndex_);
    }
 void LogNormalFwdRateIpc::setInitialState(const CurveState& cs) {
     setForwards(cs.forwardRates());
 }
    bool MultiStepPeriodCapletSwaptions::nextTimeStep(
            const CurveState& currentState,
            std::vector<Size>& numberCashFlowsThisStep,
            std::vector<std::vector<MarketModelMultiProduct::CashFlow> >&
                                                               genCashFlows)
    {

     for (Size i=0; i < numberCashFlowsThisStep.size(); ++i)
        numberCashFlowsThisStep[i]=0UL;

    if (currentIndex_ >=offset_ && (currentIndex_ - offset_) % period_ ==0)
    {
        // caplet first
        Real df = currentState.discountRatio(currentIndex_ + period_, currentIndex_);
        Time tau = rateTimes_[currentIndex_+period_]- rateTimes_[currentIndex_];
        Real forward = (1.0/df-1.0)/tau;
        Real value = (*forwardPayOffs_[productIndex_])(forward);
        value *= tau*currentState.discountRatio(currentIndex_+period_,currentIndex_);

        if (value >0)
        {
            numberCashFlowsThisStep[productIndex_]=1UL;
            genCashFlows[productIndex_][0].amount = value;
            genCashFlows[productIndex_][0].timeIndex= productIndex_;
        }

        // now swaption

        unsigned long numberPeriods = numberBigFRAs_ - productIndex_;
        Real B = 0.0;
        double P0 = 1.0; // i.e currentState.discountRatio(currentIndex_,currentIndex_);
        Real Pn = currentState.discountRatio(currentIndex_ + numberPeriods*period_, currentIndex_);
        for (unsigned long i=0; i < numberPeriods; ++i)
        {
            Time tau = rateTimes_[currentIndex_+(i+1)*period_]- rateTimes_[currentIndex_+i*period_];
            B+= tau*currentState.discountRatio(currentIndex_+(i+1)*period_,currentIndex_);
        }


        Real swapRate = (P0-Pn)/B;

        Real swaptionValue=  (*swapPayOffs_[productIndex_])(swapRate);
        swaptionValue *=B;

        if (swaptionValue >0)
        {
            numberCashFlowsThisStep[productIndex_+numberBigFRAs_]=1UL;
            genCashFlows[productIndex_+numberBigFRAs_][0].amount = swaptionValue;
            genCashFlows[productIndex_+numberBigFRAs_][0].timeIndex=productIndex_+numberBigFRAs_;
       }

        ++productIndex_;

    }

    ++currentIndex_;

    bool terminate =(productIndex_ >= numberBigFRAs_);


    return terminate;
    }
Exemple #21
0
 void SVDDFwdRatePc::setInitialState(const CurveState& cs) 
 {
     setForwards(cs.forwardRates());
 }
 void TriggeredSwapExercise::values(const CurveState& state,
                                    std::vector<Real>& results) const {
     Size swapIndex = rateIndex_[currentStep_-1];
     results.resize(1);
     results[0] = state.coterminalSwapRate(swapIndex);
 }