Esempio n. 1
0
    def apply_yield_forward_calcs(self):
        ''' Calculates spot, yield and forwards based on provides zeros
        '''
        for indx, vals in self.results.iterrows():
            if vals.type == 2. or vals.type == 4.:
                date_diff = intdate.convert_period_to_year(self.instruments[indx].frequency,
                                                           self.options)
                if abs(date_diff - vals['date_diff']) > 0.025:
                    strt = intdate.convert_date_bdte(self.options['start_date'], self.options)
                    maturity = strt.get_day_count(self.instruments[indx].reset,
                                                  self.options['control']['convention'])
                    ref_indx = self.zeros.determine_closest_maturity(maturity)
                    if ref_indx:
                        if self.dbg:
                            print("Warning Updating forward rate %s %f" % (
                                indx, abs(date_diff-vals['date_diff'])))
                        frwrd = intbase.calc_forward_rate_1d(
                            self.zeros.matrix.loc[ref_indx, 'maturity'],
                            self.results.loc[indx, 'maturity'],
                            self.zeros.matrix.loc[ref_indx, 'zero'],
                            self.results.loc[indx, 'zero'], 100.)

                        self.results.loc[indx, 'forward'] = frwrd
                        ref_indx = self.zeros.determine_closest_maturity(
                            self.results.loc[indx, 'maturity'])

                        if ref_indx:  # Updates zeros forward
                            self.zeros.matrix.loc[ref_indx, 'forward'] = frwrd
            self.results.loc[indx, 'spot'] = intbase.calc_spot_simple_1d(
                vals['zero'], vals['maturity'], 100.)
Esempio n. 2
0
def test_forward_d1_calc(tolerance, dbg):
    ''' forward test calculation '''
    t1 = 0.5
    t2 = 1.0
    zero_t1 = 0.970874
    zero_t2 = 0.933532
    x1 = intbase.calc_forward_rate_1d(t1, t2, zero_t1, zero_t2)
    if bool(dbg):
        print(x1, dbg)

    assert abs(x1 - 0.08) < tolerance
def calc_vega_black_caplet(strike, t1, t2, zero_t1, zero_t2, sigma, dbg=False):
    ''' calculates black caplet vega '''

    forward = intbase.calc_forward_rate_1d(t1, t2, zero_t1, zero_t2)
    d1, _ = calc_di_black(strike, t1, forward, sigma, dbg=dbg)
    cpl_vega = (t2 - t1)*zero_t2*forward*np.sqrt(t1)*norm_dist.pdf(d1)

    if dbg:
        print("vega (black): strike %.7f forward %.7f t1 %f t2 %f " % (strike, forward, t1, t2))
        print("vega (black): p(0, t1) %.7f d1 %.7f caplet %.7f " % (zero_t1, d1, cpl_vega))

    return cpl_vega
def calc_vega_bachelier_caplet(strike, t1, t2, zero_t1, zero_t2, sigma, dbg=False):
    ''' calculates bachelier vega for caplet '''
    delta = (t2 -t1)
    numerator = intbase.calc_forward_rate_1d(t1, t2, zero_t1, zero_t2)
    d1 = calc_di_bachelier(strike, t1, numerator, sigma, dbg=dbg)

    p1 = delta*zero_t2*np.sqrt(t1)
    p2 = norm_dist.pdf(d1)

    if dbg:
        print("Vega (bachelier): zero %f t1 %f p1 %f p2 %f result %f" % (
            zero_t2, t1, p1, p2, p1*p2))

    return p1*p2
def calc_price_bachelier_caplet(strike, t1, t2, zero_t1, zero_t2, sigma, forward_t=0.0, dbg=False):
    ''' calculates caplet price based on bachelier (normal) method '''
    delta = (t2 -t1)
    numerator = intbase.calc_forward_rate_1d(t1, t2, zero_t1, zero_t2)

    d1 = calc_di_bachelier(strike, t1, numerator, sigma, forward_t=forward_t, dbg=dbg)
    mult = delta*zero_t2*sigma*np.sqrt(t1 - forward_t)
    p1 = mult*d1*norm_dist.cdf(d1)
    p2 = mult*norm_dist.pdf(d1)

    if dbg:
        print(" caplet (bachelier) delta %f zero %f mult %f" % (delta, zero_t2, mult))
        print(" caplet (bachelier) p1 %f p2 %f caplet %f" % (p1, p2, (p1+p2)))
    cpl = p1 + p2
    return cpl
def calc_price_black_caplet(strike, t1, t2, zero_t1, zero_t2, sigma, dbg=False):
    ''' calculates black caplet zero '''
    delta = (t2 - t1)
    forward = intbase.calc_forward_rate_1d(t1, t2, zero_t1, zero_t2)
    mult = delta*zero_t2

    d1, d2 = calc_di_black(strike, t1, forward, sigma, dbg=dbg)
    p1 = mult*forward*norm_dist.cdf(d1)
    p2 = mult*strike*norm_dist.cdf(d2)
    if dbg:
        print(" caplet (black) delta %f forward %f mult %f" % (delta, forward, mult))
        print(" caplet (black) p1 %f p2 %f caplet %f" % (p1, p2, (p1-p2)))

    cpl = p1 - p2

    return cpl
Esempio n. 7
0
    def init_zero(self, rates):
        ''' initializes structure in the case of zeros '''
        mult = 100.0  # calculate results in percents

        for i in np.arange(0, self.matrix.shape[0]):
            if i == 0:
                self.matrix.loc[i, 'zero'] = 1.0
            else:
                self.matrix.loc[i, 'zero'] = rates[i - 1]
                self.matrix.loc[i, 'maturity'] = intdate.calc_bdte_diff(
                    self.schedule[i], self.date_spec, self.schedule[0])

                self.matrix.loc[i, 'date_diff'] = intdate.calc_bdte_diff(
                    self.schedule[i], self.date_spec, self.schedule[i - 1])
                self.matrix.loc[i, 'forward'] = intbase.calc_forward_rate_1d(
                    self.matrix.loc[(i - 1), 'maturity'],
                    self.matrix.loc[i, 'maturity'], self.matrix.loc[(i - 1),
                                                                    'zero'],
                    self.matrix.loc[i, 'zero'], mult)

                self.matrix.loc[i, 'yield'] = mult*(-1.)*np.log(self.matrix.loc[i, 'zero'])/\
                    self.matrix.loc[i, 'maturity']