Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 30

Sage Help Sheet

This is a Sage sheet corresponding to the commands for my 3rd year Game Theory course here.

Rearranging expressions

You will often have simple expressions like 7x18(1x)7x-18(1-x) that need simplifying (sometimes they will be more complex than that obviously). These commands show you how to carry this out:

exp = 7 * x - 18 * (1 - x) exp
25*x - 18

If your expression was more complicated you could use simplify(exp), expand(exp) and/or factor(exp).

Plotting functions

To plot a function f:x35x2f:x\to 35x-2 type:

f(x) = 35 * x - 2 plot(f, x, 0, 1)

Solving equations

Linear equations appear all the time in this course. To solve 7x2(1x)=3x+6(1x)7x-2(1-x)=3x+6(1-x):

solve(7 * x - 2 * ( 1 - x) == 3 * x + 6 * (1 - x), x)
[x == (2/3)]

Differentiation

To obtain: du1du1\frac{du_1}{du_1} where u1=(Kq1q2)q1kq1u_1=(K - q_1 - q_2)q_1 - kq_1:

K, q1, q2, k = var('K, q1, q2, k') # We need to tell Sage that these are symbolic variables u1 = (K - q1 - q2) * q1 - k * q1 diff(u1, q1)
K - k - 2*q1 - q2

If we wanted to find the point at which this equates to zero we would write the following:

du = diff(u1, q1) solve(du == 0, q1)
[q1 == 1/2*K - 1/2*k - 1/2*q2]

Infinite sums

Sage can be used to obtain expressions like i=0(13)i\sum_{i=0}^{\infty}\left(\frac{1}{3}\right)^i:

i = var('i') sum((1 / 3) ^ i, i, 0, oo)
3/2

Sage can also be used to check symbolic expressions of this nature. For example you can quickly check what the expression for i=0xi\sum_{i=0}^{\infty} x^i is:

i = var('i') assume(abs(x) < 1) sum(x ^ i, i, 0, oo)
-1/(x - 1)

Permutations

To quickly obtain all permutations of a set of the set {1,2,3}\{1,2,3\}:

p = Permutations([1, 2, 3]) list(p)
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]

Contour plots

I find 3D surface plots messy and unclear. Here is how to obtain a contour plot of the function: f:x,y3xy+2f: x,y \to 3x-y+2

y = var('y') f(x,y) = 3 * x + y - ( 2 - y) contour_plot(f, (0,1), (0,1), colorbar=True, axes_labels=['$x$', '$y$'], cmap='summer')

(There are various options that can be passed to the cmap argument: try import matplotlib.cm; matplotlib.cm.datad.keys() to list them)

Minimizing functions

You might need to minimize the following function c:α,βα3+3β/2+α2+2αβ+β22α2β+1c:\alpha, \beta\to \alpha ^ 3 + 3\beta/2 + \alpha ^ 2 + 2\alpha\beta+\beta^2-2\alpha-2\beta+1 with the following constraints: 0α10\leq \alpha \leq 1 and 0β10\leq \beta \leq 1. Here is how to do that in Sage:

f = lambda p: p[0] ^ 3 + 3 / 2 * p[1] ^ 2 + p[0] ^ 2 + 2 * p[0] * p[1] + p[1] ^ 2 - 2 * (p[0] + p[1]) + 1 c_1 = lambda p: p[0] c_2 = lambda p: 1 - p[0] c_3 = lambda p: p[1] c_4 = lambda p: 1 - p[1] a = minimize_constrained(f, [c_1, c_2, c_3, c_4], [0, 0]) a
(0.46328257302627557, 0.21476125650562683)

For more details on minimization take a look at this page.

Finding Nash equilibria of Normal Form Games

You can easily obtain the Nash Equilibria for a generic Normal Form game: