Ask Your Question
0

Issues with Cake Eating Problem

asked 2020-11-06 18:35:31 +0100

EconJohn gravatar image

updated 2020-11-06 21:50:29 +0100

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?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2020-11-06 19:06:42 +0100

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...).

edit flag offensive delete link more

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 ( 2020-11-06 19:23:54 +0100 )edit

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 ( 2020-11-06 22:03:11 +0100 )edit

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 ( 2020-11-08 04:39:49 +0100 )edit

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: 2020-11-06 18:35:31 +0100

Seen: 295 times

Last updated: Nov 06 '20