Loading [MathJax]/jax/output/HTML-CSS/jax.js
Ask Your Question
0

Issues with Cake Eating Problem

asked 4 years ago

EconJohn gravatar image

updated 4 years ago

A while back I asked about simulating the cake eating problem in sage and the algorithm seems like an excellent way to work with things. So far the code looks as follows for obtaining a soloution to the cake eating problem:

#Step1: Name variables
k,k1,v0,beta,vk,vk1=var('k,k1,v0,beta,vk,vk1')

#Step2: Initialize your values
beta=0.6
v0=log(k)

#Step 3: The Loop to Obtain Policy function
for n in range(5):
    vk = log(k-k1) + beta*v0(k=k1)
    FOC = vk.diff(k1)
    k1star = solve(FOC==0, k1)
    print(n, k1star)
    v0 = (vk).subs(k1=k1star[0].rhs())

The results I get are as follows:

0 [k1 == 3/8*k]
1 [k1 == 24/49*k]
2 [k1 == 147/272*k]
3 [k1 == 816/1441*k]
4 [k1 == 4323/7448*k]

Initially I thought that it would be a matter of just setting k to some value however this breaks the loop.

If I wanted to obtain values for k1 (and then plot them) how would I go about doing this?

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
0

answered 4 years ago

Emmanuel Charpentier gravatar image

I'm not sure to understand your question, but...

k is a symbolic variable. You can't "set" it stricto sensu (setting the python variable kwould destroy your only reference to the symbolic variable k) but you can substitute it with another value ; for example, you could write your "print" line as print(n, klstar.subs(k1==<SomeValue>).n()) (if you want numeric values).

Collecting the successive values of n and k1(n) is left as an exercise for the reader (there are a large number of ways to do this, not all of them worth exposition...).

Preview: (hide)
link

Comments

Hey I tried your print command and I got nothing. Could you direct me to a resource where I could learn how to collect successive values?

EconJohn gravatar imageEconJohn ( 4 years ago )

What precisely" did you try to "get nothing" ? What was on the screen ? Did you account for the fact that you are working on a *list of solutions (I forgopt that in my quick answer...).

See any Python tutorial on lists and iterations...

Emmanuel Charpentier gravatar imageEmmanuel Charpentier ( 4 years ago )

My code is as follows:

#Step 3: The Loop to Obtain Policy function for n in range(5): vk = log(k-k1) + beta*v0(k=k1) FOC = vk.diff(k1) k1star = solve(FOC==0, k1) print(n, klstar.subs(k1==100).n()) v0 = (vk).subs(k1=k1star[0].rhs())

EconJohn gravatar imageEconJohn ( 4 years ago )

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 4 years ago

Seen: 341 times

Last updated: Nov 06 '20