Gauss
Gauss

Reputation: 27

Financial discounting using python Vectorization approach

I want to reproduce the variable pv_exp_man :

import pandas as pd
df = pd.DataFrame({
    'time' : [0,1,2,3,4,5],
    'disc_fact' : [0.99,0.87,0.74,0.64,0.54,0.44],
    'exp_man' : [0,100,95.45,93.11,87.46,81.77],
    'pv_exp_man' : [204.211875, 206.274622, 137.097266, 89.816576, 47.228400, 0.000000]
})

The formula is :

I can do loop but what I want is to calculate this using vectorization approach. Is this possible?

Loop approach :

for k in range(len(df)-1,-1,-1):    
    if k == len(df)-1:
        df.loc[df.time == k, 'pv_exp_man'] = 0.0
    else:
        df.loc[df.time == k, 'pv_exp_man'] = df.loc[df.time == k, 'disc_fact'].values[0] * (df.loc[df.time == k, 'exp_man'].values[0]+df.loc[df.time.isin([k+1]), 'pv_exp_man'].values[0])

Upvotes: -1

Views: 61

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195573

This type of code is hard to vectorize. But you can speed-up computing to slightly refactor the code (to further speed-up you can use package)

out, df_rev = [], df[::-1]
for disc_fact, exp_man in zip(df_rev["disc_fact"], df_rev["exp_man"]):
    if not out:
        out.append(0)
    else:
        out.append((out[-1] + exp_man) * disc_fact)


df["out"] = out[::-1]
print(df)

This prints:


   time  disc_fact  exp_man  pv_exp_man         out
0     0       0.99     0.00  204.211875  204.211875
1     1       0.87   100.00  206.274622  206.274622
2     2       0.74    95.45  137.097266  137.097266
3     3       0.64    93.11   89.816576   89.816576
4     4       0.54    87.46   47.228400   47.228400
5     5       0.44    81.77    0.000000    0.000000

Upvotes: 0

Related Questions