Skip to content

Commit

Permalink
Merge pull request #110 from QuantEcon/pep8
Browse files Browse the repository at this point in the history
STY: made all .py files pep8 compliant
  • Loading branch information
jstac committed Jan 26, 2015
2 parents 9b60437 + 72b1d17 commit 26bc87e
Show file tree
Hide file tree
Showing 79 changed files with 392 additions and 401 deletions.
5 changes: 3 additions & 2 deletions examples/3dplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import numpy as np
from matplotlib import cm


def f(x, y):
return np.cos(x**2 + y**2) / (1 + x**2 + y**2)

xgrid = np.linspace(-3, 3, 50)
ygrid = xgrid
x, y = np.meshgrid(xgrid, ygrid)
x, y = np.meshgrid(xgrid, ygrid)

fig = plt.figure(figsize=(8,6))
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
ax.plot_surface(x,
y,
Expand Down
4 changes: 1 addition & 3 deletions examples/3dvec.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,5 @@ def f(x, y):
x2, y2 = np.meshgrid(xr2, yr2)
z2 = f(x2, y2)
ax.plot_surface(x2, y2, z2, rstride=1, cstride=1, cmap=cm.jet,
linewidth=0, antialiased=True, alpha=0.2)
linewidth=0, antialiased=True, alpha=0.2)
plt.show()


1 change: 1 addition & 0 deletions examples/ar1_sd.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import numpy as np
import matplotlib.pyplot as plt


def ar1_sd(phi, omega):
return 1 / (1 - 2 * phi * np.cos(omega) + phi**2)

Expand Down
2 changes: 1 addition & 1 deletion examples/beta-binomial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import matplotlib.pyplot as plt
import numpy as np


def gen_probs(n, a, b):
probs = np.zeros(n+1)
for k in range(n+1):
Expand All @@ -22,4 +23,3 @@ def gen_probs(n, a, b):
ax.plot(list(range(0, n+1)), gen_probs(n, a, b), '-o', label=ab_label)
ax.legend()
plt.show()

4 changes: 1 addition & 3 deletions examples/binom_df.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom

fig, axes = plt.subplots(2, 2)
plt.subplots_adjust(hspace=0.4)
axes = axes.flatten()
ns = [1, 2, 4, 8]
dom = list(range(9))
dom = list(range(9))

for ax, n in zip(axes, ns):
b = binom(n, 0.5)
Expand All @@ -18,4 +17,3 @@
ax.set_title(r'$n = {}$'.format(n))

fig.show()

4 changes: 1 addition & 3 deletions examples/boxplot_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,5 @@
ax.boxplot([x, y, z])
ax.set_xticks((1, 2, 3))
ax.set_ylim(-2, 14)
ax.set_xticklabels((r'$X$', r'$Y$', r'$Z$'), fontsize=16)
ax.set_xticklabels((r'$X$', r'$Y$', r'$Z$'), fontsize=16)
plt.show()


2 changes: 1 addition & 1 deletion examples/career_vf_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
v = qe.compute_fixed_point(wp.bellman_operator, v_init)

# === plot value function === #
fig = plt.figure(figsize=(8,6))
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(111, projection='3d')
tg, eg = np.meshgrid(wp.theta, wp.epsilon)
ax.plot_surface(tg,
Expand Down
6 changes: 2 additions & 4 deletions examples/cauchy_samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,9 @@
sample_mean[i] = np.mean(data[:i])

# == Plot == #
ax.plot(list(range(n)), sample_mean, 'r-', lw=3, alpha=0.6, label=r'$\bar X_n$')
ax.plot(list(range(n)), sample_mean, 'r-', lw=3, alpha=0.6,
label=r'$\bar X_n$')
ax.plot(list(range(n)), [0] * n, 'k--', lw=0.5)
ax.legend()

fig.show()



27 changes: 13 additions & 14 deletions examples/clt3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,25 @@
"""

import numpy as np
from scipy.stats import beta, bernoulli, gaussian_kde
from scipy.stats import beta, gaussian_kde
from mpl_toolkits.mplot3d import Axes3D
from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt

beta_dist = beta(2, 2)
beta_dist = beta(2, 2)


def gen_x_draws(k):
"""
Returns a flat array containing k independent draws from the
Returns a flat array containing k independent draws from the
distribution of X, the underlying random variable. This distribution is
itself a convex combination of three beta distributions.
"""
bdraws = beta_dist.rvs((3, k))
# == Transform rows, so each represents a different distribution == #
bdraws[0,:] -= 0.5
bdraws[1,:] += 0.6
bdraws[2,:] -= 1.1
bdraws[0, :] -= 0.5
bdraws[1, :] += 0.6
bdraws[2, :] -= 1.1
# == Set X[i] = bdraws[j, i], where j is a random draw from {0, 1, 2} == #
js = np.random.random_integers(0, 2, size=k)
X = bdraws[js, np.arange(k)]
Expand All @@ -40,7 +41,7 @@ def gen_x_draws(k):
# == Form a matrix Z such that each column is reps independent draws of X == #
Z = np.empty((reps, nmax))
for i in range(nmax):
Z[:,i] = gen_x_draws(reps)
Z[:, i] = gen_x_draws(reps)
# == Take cumulative sum across columns
S = Z.cumsum(axis=1)
# == Multiply j-th column by sqrt j == #
Expand All @@ -52,23 +53,23 @@ def gen_x_draws(k):
ax = fig.gca(projection='3d')

a, b = -3, 3
gs = 100
gs = 100
xs = np.linspace(a, b, gs)

# == Build verts == #
greys = np.linspace(0.3, 0.7, nmax)
verts = []
for n in ns:
density = gaussian_kde(Y[:,n-1])
density = gaussian_kde(Y[:, n-1])
ys = density(xs)
verts.append(list(zip(xs, ys)))

poly = PolyCollection(verts, facecolors = [str(g) for g in greys])
poly = PolyCollection(verts, facecolors=[str(g) for g in greys])
poly.set_alpha(0.85)
ax.add_collection3d(poly, zs=ns, zdir='x')

#ax.text(np.mean(rhos), a-1.4, -0.02, r'$\beta$', fontsize=16)
#ax.text(np.max(rhos)+0.016, (a+b)/2, -0.02, r'$\log(y)$', fontsize=16)
# ax.text(np.mean(rhos), a-1.4, -0.02, r'$\beta$', fontsize=16)
# ax.text(np.max(rhos)+0.016, (a+b)/2, -0.02, r'$\log(y)$', fontsize=16)
ax.set_xlim3d(1, nmax)
ax.set_xticks(ns)
ax.set_xlabel("n")
Expand All @@ -77,5 +78,3 @@ def gen_x_draws(k):
ax.set_zlim3d(0, 0.4)
ax.set_zticks((0.2, 0.4))
plt.show()


4 changes: 2 additions & 2 deletions examples/dice.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@

import random


class Dice:

faces = (1, 2, 3, 4, 5, 6)

def __init__(self):
self.current_face = 1
self.current_face = 1

def roll(self):
self.current_face = random.choice(Dice.faces)

15 changes: 5 additions & 10 deletions examples/duopoly_lqnash.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@
"""
from __future__ import division
import sys
import numpy as np
import scipy.linalg as la
import matplotlib.pyplot as plt
from numpy import sqrt, max, eye, dot, zeros, cumsum, array
from numpy.random import randn
from numpy import array, eye
from quantecon.lqnash import nnash


#---------------------------------------------------------------------#
# ---------------------------------------------------------------------#
# Set up parameter values and LQ matrices
# Remember state is x_t = [1, y_{1, t}, y_{2, t}] and
# control is u_{i, t} = [y_{i, t+1} - y_{i, t}]
#---------------------------------------------------------------------#
# ---------------------------------------------------------------------#
a0 = 10.
a1 = 1.
beta = 1.
Expand All @@ -45,9 +40,9 @@
q2 = array([[-.5*d]])


#---------------------------------------------------------------------#
# ---------------------------------------------------------------------#
# Solve using QE's nnash function
#---------------------------------------------------------------------#
# ---------------------------------------------------------------------#

f1, f2, p1, p2 = nnash(a, b1, b2, r1, r2, q1, q2, 0., 0., 0., 0., 0., 0.,
tol=1e-8, max_iter=1000)
3 changes: 1 addition & 2 deletions examples/duopoly_mpe.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@

# == Solve using QE's nnash function == #
F1, F2, P1, P2 = qe.nnash(A, B1, B2, R1, R2, Q1, Q2, S1, S2, W1, W2, M1, M2,
beta=beta)
beta=beta)

# == Display policies == #
print("Computed policies for firm 1 and firm 2:\n")
print("F1 = {}".format(F1))
print("F2 = {}".format(F2))
print("\n")

29 changes: 14 additions & 15 deletions examples/eigenvec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
(2, 1))
A = np.array(A)
evals, evecs = eig(A)
evecs = evecs[:,0], evecs[:,1]
evecs = evecs[:, 0], evecs[:, 1]

fig, ax = plt.subplots()
# Set the axes through the origin
Expand All @@ -22,30 +22,30 @@
for spine in ['right', 'top']:
ax.spines[spine].set_color('none')
ax.grid(alpha=0.4)

xmin, xmax = -3, 3
ymin, ymax = -3, 3
ax.set_xlim(xmin, xmax)
ax.set_ylim(ymin, ymax)
#ax.set_xticks(())
#ax.set_yticks(())
# ax.set_xticks(())
# ax.set_yticks(())

# Plot each eigenvector
for v in evecs:
ax.annotate('', xy=v, xytext=(0, 0),
arrowprops=dict(facecolor='blue',
shrink=0,
alpha=0.6,
width=0.5))
ax.annotate('', xy=v, xytext=(0, 0),
arrowprops=dict(facecolor='blue',
shrink=0,
alpha=0.6,
width=0.5))

# Plot the image of each eigenvector
for v in evecs:
v = np.dot(A, v)
ax.annotate('', xy=v, xytext=(0, 0),
arrowprops=dict(facecolor='red',
shrink=0,
alpha=0.6,
width=0.5))
ax.annotate('', xy=v, xytext=(0, 0),
arrowprops=dict(facecolor='red',
shrink=0,
alpha=0.6,
width=0.5))

# Plot the lines they run through
x = np.linspace(xmin, xmax, 3)
Expand All @@ -55,4 +55,3 @@


plt.show()

3 changes: 2 additions & 1 deletion examples/evans_sargent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from quantecon.matrix_eqn import solve_discrete_lyapunov
from scipy.optimize import root


def computeG(A0, A1, d, Q0, tau0, beta, mu):
"""
Compute government income given mu and return tax revenues and
Expand Down Expand Up @@ -107,6 +108,7 @@ def computeG(A0, A1, d, Q0, tau0, beta, mu):
Q0 = 1000.0
tau0 = 0.0


def gg(mu):
"""
Computes the tax revenues for the government given Lagrangian
Expand Down Expand Up @@ -175,4 +177,3 @@ def gg(mu):
print(y)
print("-F")
print(-F)

7 changes: 3 additions & 4 deletions examples/evans_sargent_plot1.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""
import numpy as np
import matplotlib.pyplot as plt
from evans_sargent import T, y
from evans_sargent import T, y

tt = np.arange(T) # tt is used to make the plot time index correct.

Expand All @@ -20,8 +20,8 @@
ax.set_xlim(0, 15)

bbox = (0., 1.02, 1., .102)
legend_args = {'bbox_to_anchor' : bbox, 'loc' : 3, 'mode' : 'expand'}
p_args = {'lw' : 2, 'alpha' : 0.7}
legend_args = {'bbox_to_anchor': bbox, 'loc': 3, 'mode': 'expand'}
p_args = {'lw': 2, 'alpha': 0.7}

ax = axes[0]
ax.plot(tt, y[1, :], 'b-', label="output", **p_args)
Expand All @@ -42,4 +42,3 @@
ax.set_xlabel(r'time', fontsize=16)

plt.show()

14 changes: 8 additions & 6 deletions examples/evans_sargent_plot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"""
import numpy as np
import matplotlib.pyplot as plt
from evans_sargent import T, y, uhatdif, tauhatdif, mu, G, GPay
from evans_sargent import T, uhatdif, tauhatdif, mu, G

tt = np.arange(T) # tt is used to make the plot time index correct.
tt2 = np.arange(T-1)
Expand All @@ -21,18 +21,20 @@
ax.set_xlim(-0.5, 15)

bbox = (0., 1.02, 1., .102)
legend_args = {'bbox_to_anchor' : bbox, 'loc' : 3, 'mode' : 'expand'}
p_args = {'lw' : 2, 'alpha' : 0.7}
legend_args = {'bbox_to_anchor': bbox, 'loc': 3, 'mode': 'expand'}
p_args = {'lw': 2, 'alpha': 0.7}

ax = axes[0]
ax.plot(tt2, tauhatdif, label=r'time inconsistency differential for tax rate', **p_args)
ax.plot(tt2, tauhatdif, label=r'time inconsistency differential for tax rate',
**p_args)
ax.set_ylabel(r"$\Delta\tau$", fontsize=16)
ax.set_ylim(-0.1, 1.4)
ax.set_yticks((0.0, 0.4, 0.8, 1.2))
ax.legend(ncol=1, **legend_args)

ax = axes[1]
ax.plot(tt, uhatdif, label=r'time inconsistency differential for $u$', **p_args)
ax.plot(tt, uhatdif, label=r'time inconsistency differential for $u$',
**p_args)
ax.set_ylabel(r"$\Delta u$", fontsize=16)
ax.set_ylim(-3, .1)
ax.set_yticks((-3.0, -2.0, -1.0, 0.0))
Expand All @@ -55,4 +57,4 @@
ax.set_xlabel(r'time', fontsize=16)

plt.show()
#lines = plt.plot(tt, GPay, "o")
# lines = plt.plot(tt, GPay, "o")

0 comments on commit 26bc87e

Please sign in to comment.