|
3 | 3 | from policyengine_uk.data.dataset_schema import UKDataset |
4 | 4 |
|
5 | 5 | START_YEAR = 2020 |
6 | | -END_YEAR = 2034 |
| 6 | +END_YEAR = 2029 |
7 | 7 |
|
8 | 8 |
|
9 | 9 | def create_policyengine_uprating_factors_table(): |
@@ -45,27 +45,45 @@ def create_policyengine_uprating_factors_table(): |
45 | 45 |
|
46 | 46 | file_path = Path(__file__).parent / "uprating_growth_factors.csv" |
47 | 47 | df_growth["Parameter"] = df.index.map(parameter_by_variable) |
| 48 | + df_growth = df_growth[ |
| 49 | + ["Parameter"] + [year for year in range(START_YEAR, END_YEAR + 1)] |
| 50 | + ] |
48 | 51 | df_growth.to_csv(file_path) |
49 | 52 | return pd.read_csv(file_path) |
50 | 53 |
|
51 | 54 |
|
| 55 | +def convert_yoy_growth_to_index( |
| 56 | + growth_factors: pd.DataFrame, |
| 57 | +): |
| 58 | + """ |
| 59 | + Convert year-on-year growth factors to an index. |
| 60 | + """ |
| 61 | + growth_factors = growth_factors.copy() |
| 62 | + index = growth_factors[growth_factors.columns[2]] * 0 + 1 |
| 63 | + for year in growth_factors.columns[2:]: |
| 64 | + index *= 1 + growth_factors[year] |
| 65 | + growth_factors[year] = index |
| 66 | + return growth_factors |
| 67 | + |
| 68 | + |
52 | 69 | def apply_growth_factors( |
53 | 70 | dataset: UKDataset, |
54 | 71 | growth_factors: pd.DataFrame, |
55 | 72 | start_year: int, |
56 | 73 | end_year: int, |
57 | 74 | ): |
| 75 | + start_year = str(start_year) |
| 76 | + end_year = str(end_year) |
58 | 77 | dataset = dataset.copy() |
| 78 | + growth_factors_indices = convert_yoy_growth_to_index(growth_factors) |
59 | 79 | for i in range(len(growth_factors)): |
60 | | - index = 1 |
61 | 80 | variable = growth_factors["Variable"].values[i] |
62 | | - for year in range(start_year, end_year + 1): |
63 | | - growth_factor = growth_factors[str(year)].values[i] |
64 | | - index *= 1 + growth_factor |
| 81 | + start_index = growth_factors_indices[start_year].values[i] |
| 82 | + end_index = growth_factors_indices[end_year].values[i] |
65 | 83 |
|
66 | 84 | for table in dataset.tables: |
67 | 85 | if variable in table.columns: |
68 | | - table[variable] *= index |
| 86 | + table[variable] *= end_index / start_index |
69 | 87 |
|
70 | 88 | return dataset |
71 | 89 |
|
|
0 commit comments