Ejemplo n.º 1
0
def run_projection(model_iters, metrics, params):
    dfs = []
    for model_iter, metric, param in zip(model_iters, metrics, params):
        crv_aggregator = create_curve_aggregator(metric)
        proj_mgr = ProjectionManager(crv_aggregator, model_iter, **param)
        proj_mgr.run()
        dfs.append( crv_aggregator.to_dataframe())

    return pd.concat(dfs, axis=1)
Ejemplo n.º 2
0
def main():
    from lib.utils import dcf_act_act
    from Managers.MarketDataManager import MARKET_DATA_MANAGER

    MARKET_DATA_MANAGER.reset()
    MARKET_DATA_MANAGER.setup(dt.date(2014, 4, 15))
    # Setup a bond
    asset1 = AssBondFixRate(
        face=100,
        coupon_rate=0.05,
        frequency=4,
        issue_date=dt.date(2014, 4, 15),
        expiration_date=dt.date(2015, 4, 15),
        dcf=dcf_act_act,
        pricing_model=None,
        name='Sample Bond 1',
    )

    # or a "float rate bond" but provided with a scenario generator gives you flat rate
    # a LIBOR 3M +50 bps floater
    asset2 = AssBondFloater(rate_index_name='LIBOR_3M', spread=0.005)

    pricing_date = dt.date(2014, 4, 15)

    # create a portfolio
    port = AssPortfolioModelBase([asset1, asset2], [1, 1])
    port.create_iterator(dt.date(2014, 4, 15))
    print(port)

    metrics = ['Date', 'CF_In:Interest', 'CF_In:Principal']

    crv_aggregator = create_curve_aggregator(metrics)

    params = {'pricing date': pricing_date, 'periods': 60, 'frequency': 'MS'}
    proj_mgr = ProjectionManager(crv_aggregator, port, **params)
    proj_mgr.run()

    df = crv_aggregator.to_dataframe()
    print(df)
Ejemplo n.º 3
0
    def run_bond(self, asset):
        from Managers.MarketDataManager import MARKET_DATA_MANAGER
        MARKET_DATA_MANAGER.reset()
        MARKET_DATA_MANAGER.setup(dt.date(2014,4,15))

        credit_model = NoDefaultCreditModel()

        # Setup Asset Model
        pricing_date = dt.date(2014, 4, 15)
        model = AssModelBase(asset, credit_model)
        model_iter = model.create_iterator(pricing_date)

        metrics = ['Date', 'CF_In:Interest', 'CF_In:Principal']

        crv_aggregator = create_curve_aggregator(metrics)

        params = {'pricing date': pricing_date, 'periods': 60, 'frequency': 'MS'}
        proj_mgr = ProjectionManager(crv_aggregator, model_iter, **params)
        proj_mgr.run()

        df = crv_aggregator.to_dataframe()
        #df.to_csv('test_1')
        #print(df)
        return df
Ejemplo n.º 4
0
    def run_va_model(self):
        raw_input = {"Acct Value": 1344581.6,
                     "Attained Age": 52.8,
                     "ID": "000001",
                     "Issue Age": 45.1,
                     "Issue Date": dt.date(2005, 6, 22),
                     "Initial Date": dt.date(2013, 2, 1),
                     "Maturity Age": 90,
                     "Population": 1,
                     "Riders": dict({}),
                     "ROP Amount": 1038872.0,
                     "Gender": "F",
                     "RPB": 1038872.0,
                     "Free Withdrawal Rate": 0.1,
                     "Asset Names": ["Fund A", "Fund B"],
                     "Asset Values": [1344581.6/2, 1344581.6/2]}

        # For now, we assume the init_date is month begin
        step_per_year = 12
        periods = 360
        init_date = dt.date(2013, 2, 1)
        pricing_date = init_date
        # Set up the investment index
        #credit_rider = isr.InsCreditRateFixed(credit_rate)

        # set up the mutual fund return index
        init_df = [ pd.TimeSeries(data=[100], index=[init_date], name='stock A'),
                    pd.TimeSeries(data=[100], index=[init_date], name='stock B')
                    ]
        eq_index = [ip.IndexProvider(init_df[0], 'stock A'), ip.IndexProvider(init_df[1], 'stock B')]

        # no vol, otherwise randomness will break my test
        sim_engine = EqBSEngine(np.array([0.02, 0.02]), np.array([0.0, 0.0]), corr=np.array([[1., 0.3], [0.3, 1.]]))
        simulator = ScenarioGenerator(eq_index, sim_engine, **{'max_time_step': 5. / BDAYS_PER_YEAR})

        MARKET_DATA_MANAGER.reset()
        MARKET_DATA_MANAGER.setup(init_date)
        MARKET_DATA_MANAGER.index_table[ 'stock A'] = eq_index[0]
        MARKET_DATA_MANAGER.index_table[ 'stock B'] = eq_index[1]
        MARKET_DATA_MANAGER.scen_gen_table['stock A']=simulator
        MARKET_DATA_MANAGER.scen_gen_table['stock B']=simulator

        fund_info = {'Fund A':
                         {
                             'Allocations': {
                                 'stock A': 1,
                                 'stock B': 0,
                             },
                             'Management Fee': 0.01,
                             'Description': 'blah blah',
                         },
                     'Fund B':
                         {
                             'Allocations': {
                                 'stock A': 0,
                                 'stock B': 1,
                             },
                             'Management Fee': 0.01,
                             'Description': 'blah blah',
                         },
        }

        credit_rider = isr.InsCreditRateMutualFunds(fund_info=fund_info)

        # Set up non-rider fees
        annual_fee_rate = 0.01
        annual_booking_fee = 100
        mgmt_fee = mif.InsFeeProp(annual_fee_rate, fee_name="Mgmt Fee")
        booking_fee = mif.InsFeeConst(annual_booking_fee, fee_name="Booking Fee")
        fees = [mgmt_fee, booking_fee]

        # Set up rider
        db_rider_fee_rate = 0.005
        db_rider = mir.InsRiderDB(extract_strict(raw_input, "ROP Amount"), db_rider_fee_rate, rider_name="UWL")
        riders = [db_rider]

        # Setup investment index
        inv_index = credit_rider.inv_index(init_date, periods, step_per_year)

        # Setup iteration
        product = InsProduct(riders, fees, inv_index)
        acct = InsAcct(raw_input, product)
        acct_iter = acct.acct_iterator()

        # Setup lapse function and lapse model
        xs = [0]
        ys = [0.0, 0.1]
        shock_func = linear_comp_bounded(1, 0, floor=0.5, cap=1.5)
        lapse_model = LapseDynamic(InsStepFunc(xs, ys), shock_func, rider_name='UWL')

        # Setup surrender charge
        xs = [0]
        ys = [100, 100]
        fixed_charge_func = InsStepFunc(xs, ys)
        xs = [0, 1, 2]
        ys = [0.0, 0.3, 0.2, 0.0]
        pct_charge_func = InsStepFunc(xs, ys)
        surrender_charge = SurrenderCharge(fixed_charge_func, pct_charge_func)

        # Setup mortality function and mortality model
        xs = [x for x in range(0, 100)]
        ys = [0.01] * 100
        ys.append(float('inf'))
        mort_model = InsMortModel(InsStepFunc(xs, ys))

        # Setup VA Model
        model = InsModelVA(acct, lapse_model, mort_model, surrender_charge)
        model_iter = model.create_iterator(pricing_date)

        # model iterator to evolve the model_iter to move forward
        metrics = ['Account Value',
                   'Active Population',
                   'Benefit Base.UWL',
                   'Rider Fee.UWL',
                   'Benefit.UWL',
                   'Fee.Mgmt Fee',
                   'Fee.Booking Fee',
                   'Date',
                   'Attained Age',
                   'Anniv Flag',
                   'Death',
                   'Lapse',
                   'Paid Benefit.UWL',
                   'Surrender Charge',
                   ]
        crv_aggregator = create_curve_aggregator(metrics)

        params = {'pricing date': init_date, 'periods': 60, 'frequency': 'MS'}
        proj_mgr = ProjectionManager(crv_aggregator, model_iter, **params)
        proj_mgr.run()

        df = crv_aggregator.to_dataframe()
        # df[['Rider Fee.UWL', 'Fee.Mgmt Fee', 'Fee.Booking Fee', 'Surrender Charge']].plot(kind='bar', stacked=True)
        return df
Ejemplo n.º 5
0
        'Account Value',
        'Active Population',
        'Benefit Base.UWL',
        'Rider Fee.UWL',
        'Benefit.UWL',
        'Fee.Mgmt Fee',
        'Fee.Booking Fee',
        'Date',
        'Attained Age',
        'Anniv Flag',
        'Death',
        'Lapse',
        'Paid Benefit.UWL',
        'Surrender Charge',
    ]
    crv_aggregator = create_curve_aggregator(metrics)

    params = {'pricing date': init_date, 'periods': 60, 'frequency': 'MS'}
    proj_mgr = ProjectionManager(crv_aggregator, model_iter, **params)
    proj_mgr.run()

    df = crv_aggregator.to_dataframe()
    df[[
        'Rider Fee.UWL', 'Fee.Mgmt Fee', 'Fee.Booking Fee', 'Surrender Charge'
    ]].plot(kind='bar', stacked=True)

    import os
    if not os.path.isdir(OUTPUT_PATH):
        os.makedirs(OUTPUT_PATH)
    df.to_csv(OUTPUT_PATH + '/rider_rs.csv')
    print('output saved to %s' % OUTPUT_PATH + '/rider_rs.csv')
Ejemplo n.º 6
0
    def run_fa_model(self):
        raw_input = {
            "Acct Value": 1344581.6,
            "Attained Age": 52.8,
            "ID": "000001",
            "Issue Age": 45.1,
            "Issue Date": dt.date(2005, 6, 22),
            "Initial Date": dt.date(2013, 2, 1),
            "Maturity Age": 90,
            "Population": 1,
            "Riders": dict({}),
            "ROP Amount": 1038872.0,
            "Gender": "F",
            "RPB": 1038872.0,
            "Free Withdrawal Rate": 0.1,
            "Asset Names": ["Credit Account"],
            "Asset Values": [1344581.6]
        }

        # For now, we assume the init_date is month begin
        step_per_year = 12
        credit_rate = 0.03
        periods = 360
        init_date = dt.date(2013, 2, 1)
        pricing_date = init_date
        # Set up the investment index
        credit_rider = isr.InsCreditRateFixed(credit_rate)

        # Set up non-rider fees
        annual_fee_rate = 0.01
        annual_booking_fee = 100
        mgmt_fee = mif.InsFeeProp(annual_fee_rate, fee_name="Mgmt Fee")
        booking_fee = mif.InsFeeConst(annual_booking_fee,
                                      fee_name="Booking Fee")
        fees = [mgmt_fee, booking_fee]

        # Set up rider
        db_rider_fee_rate = 0.005
        db_rider = mir.InsRiderDB(extract_strict(raw_input, "ROP Amount"),
                                  db_rider_fee_rate,
                                  rider_name="UWL")
        riders = [db_rider]

        # Setup investment index
        inv_index = credit_rider.inv_index(init_date, periods, step_per_year)

        # Setup iteration
        product = InsProduct(riders, fees, inv_index)
        acct = InsAcct(raw_input, product)

        # Setup lapse function and lapse model
        xs = [0]
        ys = [0.0, 0.1]
        lapse_model = LapseStatic(InsStepFunc(xs, ys))

        # Setup surrender charge
        xs = [0]
        ys = [100, 100]
        fixed_charge_func = InsStepFunc(xs, ys)
        xs = [0, 1, 2]
        ys = [0.0, 0.3, 0.2, 0.0]
        pct_charge_func = InsStepFunc(xs, ys)
        surrender_charge = SurrenderCharge(fixed_charge_func, pct_charge_func)

        # Setup mortality function and mortality model
        xs = [x for x in range(0, 100)]
        ys = [0.01] * 100
        ys.append(float('inf'))
        mort_model = InsMortModel(InsStepFunc(xs, ys))

        # Setup FA Model
        model = InsModelFA(acct, lapse_model, mort_model, surrender_charge)
        model_iter = model.create_iterator(pricing_date)

        # model iterator to evolve the model_iter to move forward
        metrics = [
            'Account Value',
            'Active Population',
            'Benefit Base.UWL',
            'Rider Fee.UWL',
            'Benefit.UWL',
            'Fee.Mgmt Fee',
            'Fee.Booking Fee',
            'Date',
            'Attained Age',
            'Anniv Flag',
            'Death',
            'Lapse',
            'Paid Benefit.UWL',
            'Surrender Charge',
        ]
        crv_aggregator = create_curve_aggregator(metrics)

        time_line = pd.date_range(init_date, periods=60, freq='MS').date
        prev_date = init_date
        for d in time_line:
            if d != init_date:
                year_frac = (d - prev_date).days / DAYS_PER_YEAR
                model_iter.next(d, year_frac)
            crv_aggregator.collect_element(model_iter)
            prev_date = d
        df = crv_aggregator.to_dataframe()
        return df