ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 08 Nov 2020 04:39:49 +0100Issues with Cake Eating Problemhttps://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/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?Fri, 06 Nov 2020 18:35:31 +0100https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/Answer by Emmanuel Charpentier for <p>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:</p>
<pre><code>#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())
</code></pre>
<p>The results I get are as follows:</p>
<pre><code>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]
</code></pre>
<p>Initially I thought that it would be a matter of just setting <code>k</code> to some value however this breaks the loop. </p>
<p>If I wanted to obtain values for <code>k1</code> (and then plot them) how would I go about doing this?</p>
https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/?answer=54136#post-id-54136I'm not sure to understand your question, but...
`k` is a symbolic variable. You can't "set" it *stricto sensu* (setting the python variable `k`would 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...).Fri, 06 Nov 2020 19:06:42 +0100https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/?answer=54136#post-id-54136Comment by Emmanuel Charpentier for <p>I'm not sure to understand your question, but...</p>
<p><code>k</code> is a symbolic variable. You can't "set" it <em>stricto sensu</em> (setting the python variable <code>k</code>would destroy your only reference to the symbolic variable <code>k</code>) but you can substitute it with another value ; for example, you could write your "print" line as <code>print(n, klstar.subs(k1==<SomeValue>).n())</code> (if you want numeric values).</p>
<p>Collecting the successive values of <code>n</code> and <code>k1(n)</code> is left as an exercise for the reader (there are a large number of ways to do this, not all of them worth exposition...).</p>
https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/?comment=54139#post-id-54139What *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...Fri, 06 Nov 2020 22:03:11 +0100https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/?comment=54139#post-id-54139Comment by EconJohn for <p>I'm not sure to understand your question, but...</p>
<p><code>k</code> is a symbolic variable. You can't "set" it <em>stricto sensu</em> (setting the python variable <code>k</code>would destroy your only reference to the symbolic variable <code>k</code>) but you can substitute it with another value ; for example, you could write your "print" line as <code>print(n, klstar.subs(k1==<SomeValue>).n())</code> (if you want numeric values).</p>
<p>Collecting the successive values of <code>n</code> and <code>k1(n)</code> is left as an exercise for the reader (there are a large number of ways to do this, not all of them worth exposition...).</p>
https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/?comment=54137#post-id-54137Hey 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?Fri, 06 Nov 2020 19:23:54 +0100https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/?comment=54137#post-id-54137Comment by EconJohn for <p>I'm not sure to understand your question, but...</p>
<p><code>k</code> is a symbolic variable. You can't "set" it <em>stricto sensu</em> (setting the python variable <code>k</code>would destroy your only reference to the symbolic variable <code>k</code>) but you can substitute it with another value ; for example, you could write your "print" line as <code>print(n, klstar.subs(k1==<SomeValue>).n())</code> (if you want numeric values).</p>
<p>Collecting the successive values of <code>n</code> and <code>k1(n)</code> is left as an exercise for the reader (there are a large number of ways to do this, not all of them worth exposition...).</p>
https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/?comment=54151#post-id-54151My 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())`Sun, 08 Nov 2020 04:39:49 +0100https://ask.sagemath.org/question/54134/issues-with-cake-eating-problem/?comment=54151#post-id-54151