Multi-period Example
In [2]:
Copied!
import numpy as np
import pandas as pd
import cvxpy as cp
import numpy as np
import pandas as pd
import cvxpy as cp
Suppose $W_0=\$ 500$. Determine the optimal amount of money $x_0$ (in $\$$ ) to invest in the risky asset at time 0 to maximize the expected logarithmic utility of your wealth $W_1$ at time 1 (end of the investment period).
In [3]:
Copied!
p = np.array([0.5,0.5])
r = np.array([0.25, -0.2])
W0 = 500
L1 = 0
x0 = cp.Variable(1)
obj = cp.Maximize(p.T@cp.log(W0 + r*x0 - L1))
constraints = [x0 >= 0, x0 <= W0]
prob = cp.Problem(obj,constraints)
# prob.solve(solver=cp.GUROBI)
prob.solve()
print("x0 = ", x0.value[0])
print("Ratio = ", x0.value[0]/(W0 - L1))
p = np.array([0.5,0.5])
r = np.array([0.25, -0.2])
W0 = 500
L1 = 0
x0 = cp.Variable(1)
obj = cp.Maximize(p.T@cp.log(W0 + r*x0 - L1))
constraints = [x0 >= 0, x0 <= W0]
prob = cp.Problem(obj,constraints)
# prob.solve(solver=cp.GUROBI)
prob.solve()
print("x0 = ", x0.value[0])
print("Ratio = ", x0.value[0]/(W0 - L1))
x0 = 249.9449220288116 Ratio = 0.49988984405762316
Suppose $W_0=\$ 500$ and $L_2=\$ 100$. Determine the optimal amount of money $x_0$ (in $\$$ ) to invest in the risky asset at time 0 to maximize the expected logarithmic utility of your wealth $W_2$ at time $t=2$.
In [55]:
Copied!
p = np.array([0.25,0.25,0.25,0.25])
r = np.array([[0.25, 0.25],
[0.25, -0.2],
[-0.2, 0.25],
[-0.2, -0.2]])
W0 = 500
L2 = 100
x = cp.Variable(2)
obj = cp.Maximize(p.T@cp.log(W0 + r@x - L2))
constraints = [x >= 0]
prob = cp.Problem(obj,constraints)
prob.solve()
print(x.value)
p = np.array([0.25,0.25,0.25,0.25])
r = np.array([[0.25, 0.25],
[0.25, -0.2],
[-0.2, 0.25],
[-0.2, -0.2]])
W0 = 500
L2 = 100
x = cp.Variable(2)
obj = cp.Maximize(p.T@cp.log(W0 + r@x - L2))
constraints = [x >= 0]
prob = cp.Problem(obj,constraints)
prob.solve()
print(x.value)
[197.555372 197.555372]