ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 04 Jul 2018 09:22:49 -0500Partial differentiationhttp://ask.sagemath.org/question/42818/partial-differentiation/I am trying to compute a partial differentiation of the sum of 3 utility functions (u0 + u1 + u2) with respect to s_t0. Is this the right way to do that in SageMath?
w = var('w');
tau_t0 = var('tau_t0'); tau_t1 = var('tau_t1');
s_t0 = var('s_t0'); s_t1 = var('s_t1')
r = var('r'); n = var('n')
u0 = function ('u0')(w, tau_t0, s_t0)
u1 = function ('u1')(w, tau_t1, s_t1)
u2 = function ('u2') (n, w, tau_t0, r, s_t0, s_t1)
u0(w, tau_t0, s_t0) = w*(1-tau_t0) - s_t0
u1(w, tau_t1, s_t1) = w*(1-tau_t1) - s_t1
u2(n, w, tau_t0, r, s_t0, s_t1) = (1+n)^2 * w * tau_t0 + (1+n) * w * tau_t0 + (1+r)^2 * s_t0 + (1+r) * s_t1
a = diff(u0 (w, tau_t0, s_t0), s_t0)
b = diff(u1 (w, tau_t1, s_t1), s_t0)
c = diff(u2 (n, w, tau_t0, r, s_t0, s_t1), s_t0)
U = a + b + c
UTue, 03 Jul 2018 13:25:46 -0500http://ask.sagemath.org/question/42818/partial-differentiation/Answer by rburing for <p>I am trying to compute a partial differentiation of the sum of 3 utility functions (u0 + u1 + u2) with respect to s_t0. Is this the right way to do that in SageMath? </p>
<pre><code>w = var('w');
tau_t0 = var('tau_t0'); tau_t1 = var('tau_t1');
s_t0 = var('s_t0'); s_t1 = var('s_t1')
r = var('r'); n = var('n')
u0 = function ('u0')(w, tau_t0, s_t0)
u1 = function ('u1')(w, tau_t1, s_t1)
u2 = function ('u2') (n, w, tau_t0, r, s_t0, s_t1)
u0(w, tau_t0, s_t0) = w*(1-tau_t0) - s_t0
u1(w, tau_t1, s_t1) = w*(1-tau_t1) - s_t1
u2(n, w, tau_t0, r, s_t0, s_t1) = (1+n)^2 * w * tau_t0 + (1+n) * w * tau_t0 + (1+r)^2 * s_t0 + (1+r) * s_t1
a = diff(u0 (w, tau_t0, s_t0), s_t0)
b = diff(u1 (w, tau_t1, s_t1), s_t0)
c = diff(u2 (n, w, tau_t0, r, s_t0, s_t1), s_t0)
U = a + b + c
U
</code></pre>
http://ask.sagemath.org/question/42818/partial-differentiation/?answer=42819#post-id-42819Sure, you can do it that way. You can omit the lines
u0 = function ('u0')(w, tau_t0, s_t0)
u1 = function ('u1')(w, tau_t1, s_t1)
u2 = function ('u2') (n, w, tau_t0, r, s_t0, s_t1)
because the subsequent lines just redefine `u0`,`u1`,`u2` using Sage syntax for function definition.
You could also replace the functions `u0`,`u1`,`u2` by their values (since you only use the values):
var('w,tau_t0,tau_t1,s_t0,s_t1,r,n')
u0 = w*(1-tau_t0) - s_t0
u1 = w*(1-tau_t1) - s_t1
u2 = (1+n)^2 * w * tau_t0 + (1+n) * w * tau_t0 + (1+r)^2 * s_t0 + (1+r) * s_t1
a = diff(u0, s_t0)
b = diff(u1, s_t0)
c = diff(u2, s_t0)
U = a + b + c
U
Since partial differentiation is linear, you can also define `U` in terms of `u0`,`u1`,`u2` in one step:
U = diff(u0+u1+u2, s_t0)Tue, 03 Jul 2018 15:12:49 -0500http://ask.sagemath.org/question/42818/partial-differentiation/?answer=42819#post-id-42819Comment by sbac for <p>Sure, you can do it that way. You can omit the lines</p>
<pre><code>u0 = function ('u0')(w, tau_t0, s_t0)
u1 = function ('u1')(w, tau_t1, s_t1)
u2 = function ('u2') (n, w, tau_t0, r, s_t0, s_t1)
</code></pre>
<p>because the subsequent lines just redefine <code>u0</code>,<code>u1</code>,<code>u2</code> using Sage syntax for function definition.</p>
<p>You could also replace the functions <code>u0</code>,<code>u1</code>,<code>u2</code> by their values (since you only use the values):</p>
<pre><code>var('w,tau_t0,tau_t1,s_t0,s_t1,r,n')
u0 = w*(1-tau_t0) - s_t0
u1 = w*(1-tau_t1) - s_t1
u2 = (1+n)^2 * w * tau_t0 + (1+n) * w * tau_t0 + (1+r)^2 * s_t0 + (1+r) * s_t1
a = diff(u0, s_t0)
b = diff(u1, s_t0)
c = diff(u2, s_t0)
U = a + b + c
U
</code></pre>
<p>Since partial differentiation is linear, you can also define <code>U</code> in terms of <code>u0</code>,<code>u1</code>,<code>u2</code> in one step:</p>
<pre><code>U = diff(u0+u1+u2, s_t0)
</code></pre>
http://ask.sagemath.org/question/42818/partial-differentiation/?comment=42826#post-id-42826You're right. I didn't express it well. Now the result is what I expected. Thank you.Wed, 04 Jul 2018 09:22:49 -0500http://ask.sagemath.org/question/42818/partial-differentiation/?comment=42826#post-id-42826Comment by rburing for <p>Sure, you can do it that way. You can omit the lines</p>
<pre><code>u0 = function ('u0')(w, tau_t0, s_t0)
u1 = function ('u1')(w, tau_t1, s_t1)
u2 = function ('u2') (n, w, tau_t0, r, s_t0, s_t1)
</code></pre>
<p>because the subsequent lines just redefine <code>u0</code>,<code>u1</code>,<code>u2</code> using Sage syntax for function definition.</p>
<p>You could also replace the functions <code>u0</code>,<code>u1</code>,<code>u2</code> by their values (since you only use the values):</p>
<pre><code>var('w,tau_t0,tau_t1,s_t0,s_t1,r,n')
u0 = w*(1-tau_t0) - s_t0
u1 = w*(1-tau_t1) - s_t1
u2 = (1+n)^2 * w * tau_t0 + (1+n) * w * tau_t0 + (1+r)^2 * s_t0 + (1+r) * s_t1
a = diff(u0, s_t0)
b = diff(u1, s_t0)
c = diff(u2, s_t0)
U = a + b + c
U
</code></pre>
<p>Since partial differentiation is linear, you can also define <code>U</code> in terms of <code>u0</code>,<code>u1</code>,<code>u2</code> in one step:</p>
<pre><code>U = diff(u0+u1+u2, s_t0)
</code></pre>
http://ask.sagemath.org/question/42818/partial-differentiation/?comment=42824#post-id-42824It seems that you mean each of `u0`,`u1`,`u2` should be a (symbolic, unknown) function of *one* argument (contrary to the code you wrote). Here is how you can do that:
var('w,tau_t0,tau_t1,s_t0,s_t1,r,n')
u0 = function('u0')
u1 = function('u1')
u2 = function('u2')
a = diff(u0(w*(1-tau_t0) - s_t0), s_t0)
b = diff(u1(w*(1-tau_t1) - s_t1), s_t0)
c = diff(u2((1+n)^2 * w * tau_t0 + (1+n) * w * tau_t0 + (1+r)^2 * s_t0 + (1+r) * s_t1), s_t0)
U = a + b + c
UWed, 04 Jul 2018 06:24:53 -0500http://ask.sagemath.org/question/42818/partial-differentiation/?comment=42824#post-id-42824Comment by sbac for <p>Sure, you can do it that way. You can omit the lines</p>
<pre><code>u0 = function ('u0')(w, tau_t0, s_t0)
u1 = function ('u1')(w, tau_t1, s_t1)
u2 = function ('u2') (n, w, tau_t0, r, s_t0, s_t1)
</code></pre>
<p>because the subsequent lines just redefine <code>u0</code>,<code>u1</code>,<code>u2</code> using Sage syntax for function definition.</p>
<p>You could also replace the functions <code>u0</code>,<code>u1</code>,<code>u2</code> by their values (since you only use the values):</p>
<pre><code>var('w,tau_t0,tau_t1,s_t0,s_t1,r,n')
u0 = w*(1-tau_t0) - s_t0
u1 = w*(1-tau_t1) - s_t1
u2 = (1+n)^2 * w * tau_t0 + (1+n) * w * tau_t0 + (1+r)^2 * s_t0 + (1+r) * s_t1
a = diff(u0, s_t0)
b = diff(u1, s_t0)
c = diff(u2, s_t0)
U = a + b + c
U
</code></pre>
<p>Since partial differentiation is linear, you can also define <code>U</code> in terms of <code>u0</code>,<code>u1</code>,<code>u2</code> in one step:</p>
<pre><code>U = diff(u0+u1+u2, s_t0)
</code></pre>
http://ask.sagemath.org/question/42818/partial-differentiation/?comment=42823#post-id-42823Thank you for your answer. I think I have a problem here with the chain rule, that's why I defined u0, u1 and u2 as (utility) functions. For example, the right answer to `diff(u0, s_t0)` is not -1 but `-u0' (w*(1-tau_t0) - s_t0)`. How can I take this into account in Sage?Wed, 04 Jul 2018 05:28:05 -0500http://ask.sagemath.org/question/42818/partial-differentiation/?comment=42823#post-id-42823