|
1 |
| -import numpy as np |
| 1 | +from decimal import Decimal |
2 | 2 |
|
| 3 | +import numpy as np |
3 | 4 | import numpy_financial as npf
|
4 | 5 |
|
5 | 6 |
|
6 |
| -class Npv1DCashflow: |
| 7 | +class Npv2D: |
7 | 8 |
|
8 |
| - param_names = ["cashflow_length"] |
| 9 | + param_names = ["n_cashflows", "cashflow_lengths", "rates_lengths"] |
9 | 10 | params = [
|
10 | 11 | (1, 10, 100, 1000),
|
| 12 | + (1, 10, 100, 1000), |
| 13 | + (1, 10, 100, 1000), |
11 | 14 | ]
|
12 | 15 |
|
13 | 16 | def __init__(self):
|
| 17 | + self.rates_decimal = None |
| 18 | + self.rates = None |
| 19 | + self.cashflows_decimal = None |
14 | 20 | self.cashflows = None
|
15 | 21 |
|
16 |
| - def setup(self, cashflow_length): |
| 22 | + def setup(self, n_cashflows, cashflow_lengths, rates_lengths): |
17 | 23 | rng = np.random.default_rng(0)
|
18 |
| - self.cashflows = rng.standard_normal(cashflow_length) |
| 24 | + cf_shape = (n_cashflows, cashflow_lengths) |
| 25 | + self.cashflows = rng.standard_normal(cf_shape) |
| 26 | + self.rates = rng.standard_normal(rates_lengths) |
| 27 | + self.cashflows_decimal = rng.standard_normal(cf_shape).asdtype(Decimal) |
| 28 | + self.rates_decimal = rng.standard_normal(rates_lengths).asdtype(Decimal) |
19 | 29 |
|
20 |
| - def time_1d_cashflow(self, cashflow_length): |
21 |
| - npf.npv(0.08, self.cashflows) |
| 30 | + def time_broadcast(self, n_cashflows, cashflow_lengths, rates_lengths): |
| 31 | + npf.npv(self.rates, self.cashflows) |
22 | 32 |
|
| 33 | + def time_for_loop(self, n_cashflows, cashflow_lengths, rates_lengths): |
| 34 | + for i, rate in enumerate(self.rates): |
| 35 | + for j, cashflow in enumerate(self.cashflows): |
| 36 | + npf.npv(rate, cashflow) |
23 | 37 |
|
24 |
| -class Npv2DCashflows: |
| 38 | + def time_broadcast_decimal(self, n_cashflows, cashflow_lengths, rates_lengths): |
| 39 | + npf.npv(self.rates_decimal, self.cashflows_decimal) |
25 | 40 |
|
26 |
| - param_names = ["n_cashflows", "cashflow_lengths"] |
27 |
| - params = [ |
28 |
| - (1, 10, 100, 1000), |
29 |
| - (1, 10, 100, 1000), |
30 |
| - ] |
31 |
| - |
32 |
| - def __init__(self): |
33 |
| - self.cashflows = None |
34 |
| - |
35 |
| - def setup(self, n_cashflows, cashflow_lengths): |
36 |
| - rng = np.random.default_rng(0) |
37 |
| - self.cashflows = rng.standard_normal((n_cashflows, cashflow_lengths)) |
| 41 | + def time_for_loop_decimal(self, n_cashflows, cashflow_lengths, rates_lengths): |
| 42 | + for i, rate in enumerate(self.rates_decimal): |
| 43 | + for j, cashflow in enumerate(self.cashflows_decimal): |
| 44 | + npf.npv(rate, cashflow) |
38 | 45 |
|
39 |
| - def time_2d_cashflow(self, n_cashflows, cashflow_lengths): |
40 |
| - npf.npv(0.08, self.cashflows) |
|
0 commit comments