-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_system.py
More file actions
executable file
·79 lines (61 loc) · 1.95 KB
/
Copy pathcomplex_system.py
File metadata and controls
executable file
·79 lines (61 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from dask import delayed
import makedata
import dask.array as da
import numpy as np
@delayed
def increment(x):
return x + 1
@delayed
def halve(y):
return y / 2
@delayed
def default(hist, income):
return hist**2 + income
@delayed
def agg(x, y):
return x + y
def merge(seq):
if len(seq) < 2:
return seq
middle = len(seq)//2
left = merge(seq[:middle])
right = merge(seq[middle:])
if not right:
return left
return [agg(left[0], right[0])]
class Default():
inputs = ['inc_hist', 'halved_income']
outputs = ['defaults']
@delayed
def equation(self, inc_hist, halved_income, **kwargs):
return inc_hist**2 + halved_income
@delayed
def agerisk(age):
return (100 - age)/2
def main():
hist_yrs = range(10)
incomes = range(10)
inc_hist = [increment(n) for n in hist_yrs]
halved_income = [halve(n) for n in incomes]
estimated_defaults = [default(hist, income) for hist, income in zip(inc_hist, halved_income)]
total_defaults = sum(estimated_defaults)
print(total_defaults)
total_defaults.compute
print(total_defaults.compute())
total_defaults.visualize() # requires graphviz and python-graphviz to be install
def using_default_class():
""" Exercise to do: implement on larger dataset using dask rather than theoretical example
"""
data = makedata.data()
#data = data.compute()
print(data.income)
print(data.dtypes)
data["halve_income"] = data.income.map(halve.compute())
data["inc_hist"] = data.age.map(agerisk.compute())
data["default"] = data.apply(lambda data : default(data.inc_hist,data.halve_income).compute() , axis=1,)
default_total = data.default.sum()
default_total = default_total.compute()/1000000
print("defaults of portfolio are: ",default_total, " million")
if __name__ == "__main__":
#using_default_class()
main()