<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Utility Functions
Date
Step1:  Start by looking at a fair lottery random.  A Bernoulli distribution can be used to represent a fair lottery
with proability $p$ for payoff $k_1$, probability $1-p$ for payoff of $k_2$.  If the lottery is fair it has 
expected value of zero, which can be used to solve for $p$.
Step2:  Playing around with random variables
Step3:  Worked example and definition of Utility
Below we follow the example from p14-17, of a bet on a fair coin flip, $p=1/2$,
that costs \$10 to enter, and pays off \$16 for a win, \$4 for a loss.
Step4:  Now consider a utility function of the form $U(W) = \sqrt{W}$, where $W$ is the wealth of the player.
Step5:  We see that the expected utility of the bet is less than the utility of the original \$10
Step6:  Calculate the risk premium
Step7:  We can go back to the general form for the lottery and calculate the risk premium $\pi$ as a function of the other
parameters, assuming that the cost to enter the bet is given by the fair value of the lottery.
(Sidenote
Step8:  Relate risk premium to utility function curvature.
Assume general RV for (payoff - cost), assume this has $E[x] == 0$ i.e. cost is expected payoff.
This also means var(x) = $E[x^2]$.
Step9:  We get to the form for risk premium given in the text, using SymPy to do the required manipulations.
Risk Aversion Coefficients
We can extract the term containing $U(W)$ into the coefficient for absolute risk aversion $R_A$ and from this the 
coefficient for relative risk aversion $R_R$
Step10:  Then we can apply these definitions to some utility functions of interest
Step11:  Constant Relative Risk Aversion
Step12:  Constant Absolute Risk Aversion
Step13:  Quadratic Utility
<ASSISTANT_TASK:>
Python Code:
import sympy as sp
from sympy.interactive import printing
printing.init_printing(use_latex=True)
from sympy.stats import Bernoulli, LogNormal, density, sample, P as Prob, E as Expected, variance
Explanation: Utility Functions
Date: 2022-02-21
Author: Matt McDonnell @mattmcd
Looking at 'Quantitative Financial Economics' by Cuthbertson and Nitzsche to understand utility functions and
indifference curves.
End of explanation
k1, k2 = sp.symbols('k1 k2', real=True)
p = sp.symbols('p', nonnegative=True)
Xs = sp.symbols('X')
X = Bernoulli('X', p=p, succ=k1, fail=k2)
Expected(X)
sp.Eq(p, sp.solve(Expected(X), p)[0])
Explanation: Start by looking at a fair lottery random.  A Bernoulli distribution can be used to represent a fair lottery
with proability $p$ for payoff $k_1$, probability $1-p$ for payoff of $k_2$.  If the lottery is fair it has 
expected value of zero, which can be used to solve for $p$.
End of explanation
# Doesn't work - same RV?
# FairCoin = X.subs({p: sp.S.Half, k1: 1, k2: -1})
# FairCoin2 = X.subs({p: sp.S.Half, k1: -1, k2: 1})
# Works
FairCoin = Bernoulli('X1', p=sp.S.Half, succ=1, fail=-1)
FairCoin2 = Bernoulli('X2', p=sp.S.Half, succ=1, fail=-1)
sample(FairCoin + FairCoin2, size=(10,))
Prob(X.subs({p: 1/2, k1:1, k2:-1}) > 0)
Explanation: Playing around with random variables
End of explanation
# Expected value of FairCoin toss with payoff $16 and $4. 
# No risk aversion -> would pay up to this amount for the bet
Payoff = X.subs({p: sp.S.Half, k1: 16, k2: 4})
Expected(Payoff)
Explanation: Worked example and definition of Utility
Below we follow the example from p14-17, of a bet on a fair coin flip, $p=1/2$,
that costs \$10 to enter, and pays off \$16 for a win, \$4 for a loss.
End of explanation
# Expected utility of the FairCoin toss with payoff $16 and $4 for sqrt utility
# i.e. U(W) = sqrt(W)
d = sp.Dummy()
U = sp.Lambda(d, sp.sqrt(d))
Expected(U(Payoff))
Explanation: Now consider a utility function of the form $U(W) = \sqrt{W}$, where $W$ is the wealth of the player.
End of explanation
# Utility of keeping $10 rather than paying $10 for bet
U(10).evalf()
Explanation: We see that the expected utility of the bet is less than the utility of the original \$10:
End of explanation
# Calculate the risk premium: what would you pay not to have to take the bet?
initial_wealth = 10
bet_cost = 10
sp.solve(U(initial_wealth-d) - Expected(U(initial_wealth + Payoff - bet_cost)), d)[0].evalf()
Explanation: Calculate the risk premium: what would you pay not to have to take the bet?
End of explanation
# General form
W, c, pis = sp.symbols('W, c, pi', real=True)
# Solve for cost
sp.Eq(c, sp.solve(Expected(X - c), c)[0].collect([k1, k2]))
sp.Eq(pis, sp.solve(U(W-pis) - Expected(U(W + X - Expected(X))), pis)[0].simplify().collect(p))
Explanation: We can go back to the general form for the lottery and calculate the risk premium $\pi$ as a function of the other
parameters, assuming that the cost to enter the bet is given by the fair value of the lottery.
(Sidenote: I'm not a fan of using $\pi$ to represent risk premium, seems like it's asking for trouble if $\pi$
the number crops up in expressions.)
End of explanation
x = sp.stats.rv.RandomSymbol('x')   
Us = sp.symbols('U', cls=sp.Function)
sp.Eq(Us(W - pis), Expected(Us(W + x)))
lhs = sp.series(Us(W - pis), pis, n=2).removeO().simplify()
lhs
sigma_sq_x = sp.symbols('sigma_x^2', positive=True)
rhs = Expected(
    sp.series(Us(W + x), x, n=3).removeO()
).collect(Us(W)).subs(Expected(x), 0).subs({Expected(x**2): sigma_sq_x})
rhs
pi = sp.solve(lhs - rhs, pis)[0]
sp.Eq(pis, pi)
Explanation: Relate risk premium to utility function curvature.
Assume general RV for (payoff - cost), assume this has $E[x] == 0$ i.e. cost is expected payoff.
This also means var(x) = $E[x^2]$.
End of explanation
Ras, Rrs = sp.symbols('R_A R_R', positive=True)
Ra = pi/(sigma_sq_x/2)
Rr = W*Ras
Rau = lambda U: Ra.subs(Us(W), U).simplify().powsimp()
sp.Eq(Ras, Ra), sp.Eq(Rrs, Rr)
Explanation: We get to the form for risk premium given in the text, using SymPy to do the required manipulations.
Risk Aversion Coefficients
We can extract the term containing $U(W)$ into the coefficient for absolute risk aversion $R_A$ and from this the 
coefficient for relative risk aversion $R_R$
End of explanation
g, a, b, c = sp.symbols('gamma a b c', positive=True)
U_crras, U_caras, U_qs = sp.symbols('U_{CRRA} U_{CARA} U_Q', cls=sp.Function)
U_crra = W**(1-g)/(1-g)
U_cara = a - b*sp.exp(-c*W)
U_q = W - b/2*W**2
Explanation: Then we can apply these definitions to some utility functions of interest: constant relative risk aversion, 
constant absolute risk aversion, quadratic
End of explanation
sp.Eq(U_crras(W), U_crra)
sp.Eq(Ras, Rau(U_crra)), sp.Eq(Rrs, Rr.subs(Ras, Rau(U_crra)))
Explanation: Constant Relative Risk Aversion
End of explanation
sp.Eq(U_caras(W), U_cara)
sp.Eq(Ras, Rau(U_cara)), sp.Eq(Rrs, Rr.subs(Ras, Rau(U_cara)))
Explanation: Constant Absolute Risk Aversion
End of explanation
sp.Eq(U_qs(W), U_q)
sp.Eq(Ras, Rau(U_q)), sp.Eq(Rrs, Rr.subs(Ras, Rau(U_q)))
Explanation: Quadratic Utility
End of explanation
<END_TASK>