policyengine-core 0.1.3__tar.gz → 0.1.4__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/PKG-INFO +1 -1
  2. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/PolicyEngine_Core.egg-info/PKG-INFO +1 -1
  3. PolicyEngine-Core-0.1.4/PolicyEngine_Core.egg-info/SOURCES.txt +29 -0
  4. PolicyEngine-Core-0.1.4/policyengine_core/charts/__init__.py +0 -0
  5. PolicyEngine-Core-0.1.4/policyengine_core/charts/hypothetical/__init__.py +0 -0
  6. PolicyEngine-Core-0.1.4/policyengine_core/charts/hypothetical/varying.py +3 -0
  7. PolicyEngine-Core-0.1.4/policyengine_core/charts/hypothetical/waterfall.py +0 -0
  8. PolicyEngine-Core-0.1.4/policyengine_core/charts/population/__init__.py +0 -0
  9. PolicyEngine-Core-0.1.4/policyengine_core/charts/population/distributional.py +3 -0
  10. PolicyEngine-Core-0.1.4/policyengine_core/charts/population/poverty.py +3 -0
  11. PolicyEngine-Core-0.1.4/policyengine_core/charts/population/waterfall.py +3 -0
  12. PolicyEngine-Core-0.1.4/policyengine_core/metrics/__init__.py +0 -0
  13. PolicyEngine-Core-0.1.4/policyengine_core/metrics/hypothetical.py +3 -0
  14. PolicyEngine-Core-0.1.4/policyengine_core/metrics/population.py +53 -0
  15. PolicyEngine-Core-0.1.4/policyengine_core/utils/__init__.py +0 -0
  16. PolicyEngine-Core-0.1.4/policyengine_core/utils/formatting.py +3 -0
  17. PolicyEngine-Core-0.1.4/policyengine_core/utils/general.py +42 -0
  18. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/setup.py +1 -1
  19. PolicyEngine-Core-0.1.3/PolicyEngine_Core.egg-info/SOURCES.txt +0 -15
  20. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/PolicyEngine_Core.egg-info/dependency_links.txt +0 -0
  21. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/PolicyEngine_Core.egg-info/requires.txt +0 -0
  22. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/PolicyEngine_Core.egg-info/top_level.txt +0 -0
  23. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/__init__.py +0 -0
  24. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/api/__init__.py +0 -0
  25. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/api/cloud_run.py +0 -0
  26. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/api/general.py +0 -0
  27. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/api/hypothetical.py +0 -0
  28. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/api/microsimulation.py +0 -0
  29. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/reforms/__init__.py +0 -0
  30. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/reforms/utils.py +0 -0
  31. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/policyengine_core/server.py +0 -0
  32. {PolicyEngine-Core-0.1.3 → PolicyEngine-Core-0.1.4}/setup.cfg +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PolicyEngine-Core
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: UNKNOWN
5
5
  Home-page: https://github.com/policyengine/policyengine-core
6
6
  Author: PolicyEngine
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PolicyEngine-Core
3
- Version: 0.1.3
3
+ Version: 0.1.4
4
4
  Summary: UNKNOWN
5
5
  Home-page: https://github.com/policyengine/policyengine-core
6
6
  Author: PolicyEngine
@@ -0,0 +1,29 @@
1
+ setup.py
2
+ PolicyEngine_Core.egg-info/PKG-INFO
3
+ PolicyEngine_Core.egg-info/SOURCES.txt
4
+ PolicyEngine_Core.egg-info/dependency_links.txt
5
+ PolicyEngine_Core.egg-info/requires.txt
6
+ PolicyEngine_Core.egg-info/top_level.txt
7
+ policyengine_core/__init__.py
8
+ policyengine_core/server.py
9
+ policyengine_core/api/__init__.py
10
+ policyengine_core/api/cloud_run.py
11
+ policyengine_core/api/general.py
12
+ policyengine_core/api/hypothetical.py
13
+ policyengine_core/api/microsimulation.py
14
+ policyengine_core/charts/__init__.py
15
+ policyengine_core/charts/hypothetical/__init__.py
16
+ policyengine_core/charts/hypothetical/varying.py
17
+ policyengine_core/charts/hypothetical/waterfall.py
18
+ policyengine_core/charts/population/__init__.py
19
+ policyengine_core/charts/population/distributional.py
20
+ policyengine_core/charts/population/poverty.py
21
+ policyengine_core/charts/population/waterfall.py
22
+ policyengine_core/metrics/__init__.py
23
+ policyengine_core/metrics/hypothetical.py
24
+ policyengine_core/metrics/population.py
25
+ policyengine_core/reforms/__init__.py
26
+ policyengine_core/reforms/utils.py
27
+ policyengine_core/utils/__init__.py
28
+ policyengine_core/utils/formatting.py
29
+ policyengine_core/utils/general.py
@@ -0,0 +1,3 @@
1
+ """
2
+ Functions to generate budget and MTR charts as earnings vary.
3
+ """
@@ -0,0 +1,3 @@
1
+ """
2
+ Distributional charts (e.g. inter-decile and intra-decile) for reforms.
3
+ """
@@ -0,0 +1,3 @@
1
+ """
2
+ Charts showing changes to poverty and inequality.
3
+ """
@@ -0,0 +1,3 @@
1
+ """
2
+ Waterfall charts, breaking down sequential impacts.
3
+ """
@@ -0,0 +1,3 @@
1
+ """
2
+ Functions extracting numerical outputs from simulations results on hypothetical situations.
3
+ """
@@ -0,0 +1,53 @@
1
+ """
2
+ Functions generating aggregates and other numerical outputs from microsimulation results.
3
+ """
4
+ from policyengine_core.api.microsimulation import Microsimulation
5
+
6
+
7
+ def gbp(x: float) -> str:
8
+ return f"£{round(x / 1e+9, 1)}bn"
9
+
10
+
11
+ def pct_change(x: float, y: float) -> float:
12
+ return (y - x) / x
13
+
14
+
15
+ def poverty_rate(sim: Microsimulation, population_var: str) -> float:
16
+ return sim.calc("in_poverty_bhc", map_to="person", period=2021)[
17
+ sim.calc(population_var) > 0
18
+ ].mean()
19
+
20
+
21
+ def headline_metrics(
22
+ baseline: Microsimulation, reformed: Microsimulation
23
+ ) -> dict:
24
+ """Compute headline society-wide metrics.
25
+
26
+ :param baseline: Baseline simulation.
27
+ :type baseline: Microsimulation
28
+ :param reformed: Reform simulation.
29
+ :type reformed: Microsimulation
30
+ :return: Dictionary with net_cost, poverty_change, winner_share,
31
+ loser_share, and gini_change.
32
+ :rtype: dict
33
+ """
34
+ new_income = reformed.calc("equiv_household_net_income", map_to="person")
35
+ old_income = baseline.calc("equiv_household_net_income", map_to="person")
36
+ gain = new_income - old_income
37
+ net_cost = (
38
+ reformed.calc("net_income").sum() - baseline.calc("net_income").sum()
39
+ )
40
+ poverty_change = pct_change(
41
+ baseline.calc("in_poverty_bhc", map_to="person").mean(),
42
+ reformed.calc("in_poverty_bhc", map_to="person").mean(),
43
+ )
44
+ winner_share = (gain > 0).mean()
45
+ loser_share = (gain < 0).mean()
46
+ gini_change = pct_change(old_income.gini(), new_income.gini())
47
+ return dict(
48
+ net_cost=gbp(net_cost),
49
+ poverty_change=float(poverty_change),
50
+ winner_share=float(winner_share),
51
+ loser_share=float(loser_share),
52
+ gini_change=float(gini_change),
53
+ )
@@ -0,0 +1,3 @@
1
+ """
2
+ Utility functions for formatting charts.
3
+ """
@@ -0,0 +1,42 @@
1
+ import json
2
+ from time import time
3
+ from flask import request, make_response
4
+
5
+
6
+ def dict_to_string(d: dict) -> str:
7
+ return "_".join(["_".join((x, y)) for x, y in d.items()])
8
+
9
+
10
+ def get_cached_result(
11
+ params: dict, endpoint: str, version: str, bucket
12
+ ) -> dict:
13
+ request_id = f"{endpoint}-{dict_to_string(params)}-{version}"
14
+ blob = bucket.blob(request_id + ".json")
15
+ if blob.exists():
16
+ return json.loads(blob.download_as_string())
17
+
18
+
19
+ def after_request_func(response):
20
+ origin = request.headers.get("Origin")
21
+ if request.method == "OPTIONS":
22
+ response = make_response()
23
+ response.headers.add("Access-Control-Allow-Credentials", "true")
24
+ response.headers.add("Access-Control-Allow-Headers", "Content-Type")
25
+ response.headers.add("Access-Control-Allow-Headers", "x-csrf-token")
26
+ response.headers.add(
27
+ "Access-Control-Allow-Methods",
28
+ "GET, POST, OPTIONS, PUT, PATCH, DELETE",
29
+ )
30
+ if origin:
31
+ response.headers.add("Access-Control-Allow-Origin", origin)
32
+ else:
33
+ response.headers.add("Access-Control-Allow-Credentials", "true")
34
+ if origin:
35
+ response.headers.add("Access-Control-Allow-Origin", origin)
36
+ response.headers[
37
+ "Cache-Control"
38
+ ] = "no-cache, no-store, must-revalidate, public, max-age=0"
39
+ response.headers["Pragma"] = "no-cache"
40
+ response.headers["Expires"] = "0"
41
+
42
+ return response
@@ -3,7 +3,7 @@ from pathlib import Path
3
3
 
4
4
  setup(
5
5
  name="PolicyEngine-Core",
6
- version="0.1.3",
6
+ version="0.1.4",
7
7
  author="PolicyEngine",
8
8
  license="http://www.fsf.org/licensing/licenses/agpl-3.0.html",
9
9
  url="https://github.com/policyengine/policyengine-core",
@@ -1,15 +0,0 @@
1
- setup.py
2
- PolicyEngine_Core.egg-info/PKG-INFO
3
- PolicyEngine_Core.egg-info/SOURCES.txt
4
- PolicyEngine_Core.egg-info/dependency_links.txt
5
- PolicyEngine_Core.egg-info/requires.txt
6
- PolicyEngine_Core.egg-info/top_level.txt
7
- policyengine_core/__init__.py
8
- policyengine_core/server.py
9
- policyengine_core/api/__init__.py
10
- policyengine_core/api/cloud_run.py
11
- policyengine_core/api/general.py
12
- policyengine_core/api/hypothetical.py
13
- policyengine_core/api/microsimulation.py
14
- policyengine_core/reforms/__init__.py
15
- policyengine_core/reforms/utils.py