-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbinaryTreeRentModel.py
More file actions
185 lines (128 loc) · 4.39 KB
/
binaryTreeRentModel.py
File metadata and controls
185 lines (128 loc) · 4.39 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
'''
author: darren**2
date: 2019
rent model.
there exists a situation where a tenant renting a property can either pay their rent, u, with a probability P
or can only pay part of the rent, d, with a probability (1-p).
We work out the fair value, v of the rent as follows:
v = [u * p] + [d * (1-p)]
given one can assume the fair value v and a probability of getting paid for the period
construct a binomial tree that computed the child legs.
the function would be
f(v, p) returns u, d
step 2:
assuming we know the outcomes
compute (and store) the next values on the tree
'''
class NodeOld:
"""Class to represent a node."""
goodprob = 0.9
badprob = 1 - goodprob
rent = 1000
shitrate = 250
def __init__(self, val=None, l=None, r=None):
self.val = val
self.l = l
self.r = r
def __repr__(self):
# """Define output of print function."""
if self.l or self.r:
return f"{self.val}[{self.l},{self.r}]"
return str(self.val)
def getfairvalue(self):
return self.l.val + self.r.val
def createleft(self):
self.l= NodeOld(NodeOld.rent * NodeOld.goodprob)
def createright(self):
self.r= NodeOld(NodeOld.shitrate * NodeOld.badprob)
root = NodeOld()
root.createleft()
root.createright()
root.val = root.getfairvalue()
# print(root)
class Monthly:
''' class for a node of the tree '''
def __init__(self, val, goodTenant, contractedRent):
'''
val: [int] value of the node
goodTenant: [bool] true or false for good or bad tenant
prob: [float] the probability of getting paid
up: [float] value of child node if paid
dn: [float] value of child node if not paid
recover: [float] the recovery %, 100% full paid, 0% not paid, 50% half paid
'''
self.goodTenant = goodTenant
self.contractedRent = contractedRent
self.val = val
# set the probs and recoveries based on tenant type
if self.goodTenant == True:
self.prob = 0.99
self.recoverUp = 1
self.recoverDn = 0.25
# these are the children nodes for good tenant
self.up = self.contractedRent * self.prob * self.recoverUp
self.dn = self.contractedRent * (1-self.prob) * self.recoverDn
elif self.goodTenant == False:
self.prob = 0.25
self.recoverUp = 1.5
self.recoverDn = 0.125
# these are the children nodes for bad tenant
self.up = self.contractedRent * self.prob * self.recoverUp
self.dn = self.contractedRent * (1-self.prob) * self.recoverDn
else:
print('error, should not get here')
def getPrev(self):
pass
return 100
def calcUpMove(self):
''' calculate an upmove based on parent params '''
self.up = self.contractedRent * self.prob * self.recoverUp
return self.up
def calcDnMove(self):
pass
return
# set the root
tenantStart = True # assume tenant is good
contractedRent = 1000
startValue = contractedRent
root = Monthly(startValue, tenantStart, contractedRent)
print(f'good tenant: {root.goodTenant}, {round(root.up,4)}, {round(root.dn,4)}')
tenantStart = False # assume tenant is bad
contractedRent = 1000
startValue = contractedRent
root = Monthly(startValue, tenantStart, contractedRent)
print(f'bad tenant: {root.goodTenant}, {round(root.up,4)}, {round(root.dn,4)}')
'''
refenence by level and branch
level 0 - branch 0
level 1 - branch 0, 1
level 2 - branch 0, 1, 2, 3 (4 of these)
level 3 - branch 0, 1, 2, 3, 4, 5, 6, 7 (8 of these)
...
level n - branch 0, ... , 2**n -1 (2**n of these)
'''
# a list of lists for the nodes (levels contain 2**n branches)
level = []
branch = []
# start with [0][0]
# append the branch
branch.append(root)
# insert branch into level
level.append(branch)
print('level0, branch0 val ---> ', level[0][0].val)
print('level0, branch0 dn ---> ', level[0][0].dn)
print('level0, branch0 up ---> ', level[0][0].up)
# braches in level 1
#branch.append(Monthly(level[0][0]))
level.append(['hello', 'b'])
# print('level0 ---> ',level[0])
# print('n --->', branch[0])
# print('level0, n0 ---> ', level[0][0])
# print('level1 ---> ', level[1])
# print('level1, n0 ---> ', level[1][0])
# print('level1, n1 ---> ', level[1][1])
# do the next node
"""
output is:
925.0[900.0,24.999999999999993]
"""