#include "CapletOptionPricer.h"

CapletOptionPricer::CapletOptionPricer(const std::string& ub_error, double t1_df, double time, double df, double caplet_volatility, double accrual_factor, double forward_libor)
	:
	OptionPricer(1e-200, t1_df, ub_error),
	m_forward_libor(forward_libor),
	m_accrual_factor(accrual_factor)
{
	double intermediate = caplet_volatility * std::sqrt(time);
	m_dcp_di_right = 0.5 * intermediate;
	m_dcp_di_left_factor = 1.0 / intermediate;
	m_dcp_all_factor = df / (1.0 + m_accrual_factor * m_forward_libor);
}

double CapletOptionPricer::digital_option_in_arrears_price(double k) const
{
	double lhs = m_dcp_di_left_factor * std::log(m_forward_libor / k);
	double d1 = lhs + m_dcp_di_right;
	double d2 = lhs - m_dcp_di_right;
	return m_dcp_all_factor * (Algorithm::normal_CDF(d2) + m_accrual_factor * m_forward_libor * Algorithm::normal_CDF(d1));
}
