the answer should 117.4, i'm getting 9982.3... not sure problem here's code:
def util(c,p,alpha): mu = 0 in range(0,len(c)): m = p[i]*(c[i]**(1-alpha)) mu += m return mu**(1/(1-alpha)) omega = 0.495 c = np.array([100,200,1000]) p = np.array([omega, omega, 1-2*omega]) alpha = 5 edit: i'm not sure if there error math or function wrote, asking if math fits code wrote.
i'm solving equation mu: u(mu) = e[u(c)] payoffs c, , probability distribution p, above. u(c) has form c^(1-alpha)/(1-alpha).
u(mu) = mu^(1-alpha)/(1-alpha) = e[u(c)] = (omega*c1^(1-alpha)+omega*c2^(1-alpha)+(1-2*omega)*c3^(1-alpha))/(1-alpha) => mu = (omega*c1^(1-alpha)+omega*c2^(1-alpha)+(1-2*omega)*c3^(1-alpha))^(1/(1-alpha))
your main problem python doing integer division. python 2 integer division /, unless from __future__ import division (see pep 238). need change @ least 1 of operands floating-point value. can setting alpha = 5.0 instead of alpha = 5. or can write 1.0 - alpha instead of 1 - alpha.
also, can make code bit more compact using numpy's vectorized operations. util function can shortened to:
def util(c, p, alpha): mu = np.sum(p * (c ** (1.0 - alpha))) return mu ** (1 / (1.0 - alpha))
Comments
Post a Comment