-
Notifications
You must be signed in to change notification settings - Fork 1
/
tcapCubed.jl
182 lines (163 loc) · 4.43 KB
/
tcapCubed.jl
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
using SymPy
using Calculus
using ForwardDiff
using Optim
using Roots
using Polynomials
#using MultivariatePolynomials
using DynamicPolynomials
using PolynomialRoots
using Plots
x0 = 1#initial capital
s0 = 1 #initial price
global u = 2 #up factor
global d = 0.5 #down factor
r = 0.25#interest rate
@polyvar y p#number of shares
μ = Sym("μ")
f(x) = log.(x) #utility function
q = 1 - p
p̃ = (1 + r - d)/(u - d)
q̃ = 1 - p̃
#calculates terminal capital given of n periods when stock goes up i times
#n is number of periods
#i is number of times stock goes up
function terminalcap(n, i)
tcap = (x0 - y)*(1+r)^(n) + u^(i) * d^(n-i) * y
return tcap
end
#calculates expected value, n is number of periods
function expectedVal(n)
eval = 0
for i in 0:n
tcap = (x0 - y)*(1+r)^n + u^i * d^(n-i)*y
println(tcap)
println(typeof(tcap))
eval += p^i * q^(n - i) * binomial(BigInt(n), BigInt(i)) * f(tcap)
end
println(typeof(eval))
return eval
end
#given a desired probability, calculate the optimal y
function pOptimalY(n, prob)
eval = 0
num = 0
for i in 0:n
tcap = (x0 - y)*(1+r)^n + u^i * d^(n-i)*y
deriv = DynamicPolynomials.differentiate(tcap, y)
eval += (prob)^i * (1-prob)^(n - i) * binomial(BigInt(n), BigInt(i)) * ( deriv /(tcap*tcap*tcap*tcap))
end
numer = numerator(eval)
coeffs = [coefficient(numer, y^i) for i = 0 : maxdegree(numer)]
roo = PolynomialRoots.roots(coeffs)
badRoots = Set()
for root in roo
if imag(root) != 0
push!(badRoots, root)
else
root = real(root)
for i in 0:n
if (((x0 - s0 * root) * ( (1+r)^n) + ( (u^i) * (d^(n-i)) * s0 *root))) <= 0
push!(badRoots, root)
end
break
end
end
end
roo = Set(real.(roo))
goodRoots = setdiff(roo, badRoots)
goodRoots = collect(goodRoots)
goodRoots = sort!(goodRoots)
tpls = zeros(BigFloat, 0, 2)
for root in goodRoots
tcap = 0
for i in 0:n
tcap += (prob)^i * (1-prob)^(n - i) * binomial(BigInt(n), BigInt(i)) * -1/3 *((x0 - s0 * root)*((1+r)^n) +((u^i) * (d^(n-i)) * s0 *root))^(-3)
end
v = [root tcap]
tpls = vcat(v, tpls)
end
a = findmax(tpls)
opy = tpls[(a[2])[1]]
return opy
end
function getSwitch(n, min, max, steps)
arr = zeros(steps + 1, 4)
sw = zeros(0, 2)
for i in 1:(steps + 1)
println(i)
arr[i, 1] = min + (i-1) * (max - min)/steps
a = pOptimalY(1, arr[i, 1])
b = pOptimalY(n, arr[i, 1])
arr[i, 2] = a
arr[i, 3] = b
arr[i, 4] = b - a
end
for i in 1:(size(arr, 1))
row = arr[i, :]
println(row)
if i == 1
continue
elseif i == steps
break
elseif arr[i-1, 4] < 0 && arr[i, 4] > 0
println("decreasing to increasing: ", arr[i, 1])
elseif arr[i-1, 4] > 0 && arr[i, 4] < 0
println("increasing to decreasing: ", arr[i, 1])
end
end
return sw
end
#println(getSwitch(15, 0.58, 0.6, 100))
#println(p̃)
#plots optimaly for s curves of constant p ∈ [min, max] for periods [1, n]
function graph(n, min, max, s)
allY = zeros(0)
probs = zeros(0)
for i in 1:(s) #for a fixed probability
println(i)
global arr = zeros(s+1)
prob = min + (i-1) * (max - min)/s
for j in 1:n
arr[j] = pOptimalY(j, arr[i, 1])
end
x = 1:n
y = arr
#println("arr", arr)
allY = vcat([y], allY)
prob = string(prob)
probs = vcat(prob, probs)
println("probs", probs)
end
display(plot(allY, title="optimalY vs n", label = probs, legend=:best))
return arr
end
graph(10, p̃, 1, 10)
function varyingR(n)
switch = zeros(0)
for i in (d*100 + 1):(u*100 - 1)
global r = i/1000 - 1
println("r: ", r)
z = getSwitch(n, p̃, 1, 100)
println("z: ", z)
append!(switch, z)
println(switch)
end
return switch
end
function rnmExpectedVal(n)
eval = 0
for i in 0:n
eval += p̃^i * q̃^(n - i) * binomial(BigInt(n), BigInt(i)) * f(terminalcap(n, i))
end
return eval
end
function constantRatio(rat)
for i in 2:10
global u = i
global d = rat/i
println(u)
println(d)
println(real.(getequipoints(1)[2]))
end
end