Skip to content

Commit

Permalink
Fix math rendering in docstrings (#315)
Browse files Browse the repository at this point in the history
* add math rendering and clean up lss.py

* add math rendering and clean up kalman.py

* fix error and math rendering in approximation.py

* move class docstring outside init in ivp.py

* fix math rendering and some documentation errors in lqcontrol.py

* fix math rendering and clean up matrix.eqn.py

* fix math rendering and clean up robustlq.py

* fix math rendering in quadsums.py

* fix math rendering and clean up code in estspec.py

* fix math rendering in arma.py

* fix math rendering in lae.py

* fix previous commit

* fix brackets in lss.py

* fix to approximation.py

* Fix to matrix_eqn.py docstring

* Fixes to lss.py

* Update links in docs
  • Loading branch information
natashawatkins authored and mmcky committed Jul 4, 2017
1 parent c60251d commit a82ba69
Show file tree
Hide file tree
Showing 11 changed files with 296 additions and 194 deletions.
10 changes: 5 additions & 5 deletions quantecon/arma.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class ARMA(object):
X_t = \phi X_{t-1} + \epsilon_t + \theta \epsilon_{t-1}
where :math:`epsilon_t` is a white noise process with standard
deviation :math:`sigma`. If phi and theta are arrays or sequences,
where :math:`\epsilon_t` is a white noise process with standard
deviation :math:`\sigma`. If phi and theta are arrays or sequences,
then the interpretation is the ARMA(p, q) model
.. math::
Expand Down Expand Up @@ -182,16 +182,16 @@ def spectral_density(self, two_pi=True, res=1200):
.. math::
f(w) = \sum_k \gamma(k) exp(-ikw)
f(w) = \sum_k \gamma(k) \exp(-ikw)
where gamma is the autocovariance function and the sum is over
the set of all integers.
Parameters
----------
two_pi : Boolean, optional
Compute the spectral density function over [0, pi] if
two_pi is False and [0, 2 pi] otherwise. Default value is
Compute the spectral density function over :math:`[0, \pi]` if
two_pi is False and :math:`[0, 2 \pi]` otherwise. Default value is
True
res : scalar or array_like(int), optional(default=1200)
If res is a scalar then the spectral density is computed at
Expand Down
30 changes: 15 additions & 15 deletions quantecon/estspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,18 @@ def smooth(x, window_len=7, window='hanning'):


def periodogram(x, window=None, window_len=7):
"""
r"""
Computes the periodogram
.. math::
I(w) = (1 / n) | sum_{t=0}^{n-1} x_t e^{itw} |^2
I(w) = \frac{1}{n} \Big[ \sum_{t=0}^{n-1} x_t e^{itw} \Big] ^2
at the Fourier frequences w_j := 2 pi j / n, j = 0, ..., n - 1,
using the fast Fourier transform. Only the frequences w_j in [0,
pi] and corresponding values I(w_j) are returned. If a window type
is given then smoothing is performed.
at the Fourier frequences :math:`w_j := \frac{2 \pi j}{n}`,
:math:`j = 0, \dots, n - 1`, using the fast Fourier transform. Only the
frequences :math:`w_j` in :math:`[0, \pi]` and corresponding values
:math:`I(w_j)` are returned. If a window type is given then smoothing
is performed.
Parameters
----------
Expand Down Expand Up @@ -139,19 +140,18 @@ def ar_periodogram(x, window='hanning', window_len=7):
"""
# === run regression === #
x_lag = x[:-1] # lagged x
X = np.array([np.ones(len(x_lag)), x_lag]).T # add constant
y = np.array(x[1:]) # current x
beta_hat = np.linalg.solve(X.T @ X, X.T @ y) # solve for beta hat
e_hat = y - X @ beta_hat # compute residuals
phi = beta_hat[1] # pull out phi parameter
x_lag = x[:-1] # lagged x
X = np.array([np.ones(len(x_lag)), x_lag]).T # add constant

y = np.array(x[1:]) # current x

beta_hat = np.linalg.solve(X.T @ X, X.T @ y) # solve for beta hat
e_hat = y - X @ beta_hat # compute residuals
phi = beta_hat[1] # pull out phi parameter

# === compute periodogram on residuals === #
w, I_w = periodogram(e_hat, window=window, window_len=window_len)


# === compute periodogram on residuals === #
w, I_w = periodogram(e_hat, window=window, window_len=window_len)

Expand Down
36 changes: 19 additions & 17 deletions quantecon/ivp.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,30 @@


class IVP(integrate.ode):

r"""
Creates an instance of the IVP class.
def __init__(self, f, jac=None):
r"""
Creates an instance of the IVP class.
Parameters
----------
f : callable ``f(t, y, *f_args)``
Right hand side of the system of equations defining the ODE.
The independent variable, ``t``, is a ``scalar``; ``y`` is
an ``ndarray`` of dependent variables with ``y.shape ==
(n,)``. The function `f` should return a ``scalar``,
``ndarray`` or ``list`` (but not a ``tuple``).
jac : callable ``jac(t, y, *jac_args)``, optional(default=None)
Jacobian of the right hand side of the system of equations
defining the ODE.
Parameters
----------
f : callable ``f(t, y, *f_args)``
Right hand side of the system of equations defining the ODE.
The independent variable, ``t``, is a ``scalar``; ``y`` is
an ``ndarray`` of dependent variables with ``y.shape ==
(n,)``. The function `f` should return a ``scalar``,
``ndarray`` or ``list`` (but not a ``tuple``).
jac : callable ``jac(t, y, *jac_args)``, optional(default=None)
Jacobian of the right hand side of the system of equations
defining the ODE.
.. :math:
.. :math:
\mathcal{J}_{i,j} = \bigg[\frac{\partial f_i}{\partial y_j}\bigg]
\mathcal{J}_{i,j} = \bigg[\frac{\partial f_i}{\partial y_j}\bigg]
"""

def __init__(self, f, jac=None):

"""
super(IVP, self).__init__(f, jac)

def _integrate_fixed_trajectory(self, h, T, step, relax):
Expand Down
70 changes: 47 additions & 23 deletions quantecon/kalman.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""
Filename: kalman.py
Reference: http://quant-econ.net/py/kalman.html
Reference: https://lectures.quantecon.org/py/kalman.html
Implements the Kalman filter for a linear Gaussian state space model.
Expand All @@ -17,11 +17,16 @@ class Kalman(object):
r"""
Implements the Kalman filter for the Gaussian state space model
.. math::
x_{t+1} = A x_t + C w_{t+1}
y_t = G x_t + H v_t.
y_t = G x_t + H v_t
Here :math:`x_t` is the hidden state and :math:`y_t` is the measurement.
The shocks :math:`w_t` and :math:`v_t` are iid standard normals. Below
we use the notation
Here x_t is the hidden state and y_t is the measurement. The shocks
w_t and v_t are iid standard normals. Below we use the notation
.. math::
Q := CC'
R := HH'
Expand Down Expand Up @@ -52,7 +57,7 @@ class Kalman(object):
References
----------
http://quant-econ.net/py/kalman.html
https://lectures.quantecon.org/py/kalman.html
"""

Expand Down Expand Up @@ -91,28 +96,46 @@ def whitener_lss(self):
that system to the time-invariant whitener represenation
given by
.. math::
\tilde{x}_{t+1}^* = \tilde{A} \tilde{x} + \tilde{C} v
a = \tilde{G} \tilde{x}
where
.. math::
\tilde{x}_t = [x+{t}, \hat{x}_{t}, v_{t}]
and
\tilde{A} = [A 0 0
KG A-KG KH
0 0 0]
.. math::
\tilde{C} = [C 0
0 0
0 I]
\tilde{A} =
\begin{bmatrix}
A & 0 & 0 \\
KG & A-KG & KH \\
0 & 0 & 0 \\
\end{bmatrix}
\tilde{G} = [G -G H]
.. math::
with A, C, G, H coming from the linear state space system
that defines the Kalman instance
\tilde{C} =
\begin{bmatrix}
C & 0 \\
0 & 0 \\
0 & I \\
\end{bmatrix}
.. math::
\tilde{G} =
\begin{bmatrix}
G & -G & H \\
\end{bmatrix}
with :math:`A, C, G, H` coming from the linear state space system
that defines the Kalman instance
Returns
-------
Expand Down Expand Up @@ -147,18 +170,19 @@ def whitener_lss(self):

return whitened_lss


def prior_to_filtered(self, y):
r"""
Updates the moments (x_hat, Sigma) of the time t prior to the
time t filtering distribution, using current measurement y_t.
time t filtering distribution, using current measurement :math:`y_t`.
The updates are according to
x_{hat}^F = x_{hat} + Sigma G' (G Sigma G' + R)^{-1}
(y - G x_{hat})
Sigma^F = Sigma - Sigma G' (G Sigma G' + R)^{-1} G
Sigma
.. math::
\hat{x}^F = \hat{x} + \Sigma G' (G \Sigma G' + R)^{-1}
(y - G \hat{x})
\Sigma^F = \Sigma - \Sigma G' (G \Sigma G' + R)^{-1} G
\Sigma
Parameters
----------
Expand Down Expand Up @@ -210,15 +234,15 @@ def update(self, y):

def stationary_values(self):
"""
Computes the limit of Sigma_t as t goes to infinity by
solving the associated Riccati equation. Computation is via the
Computes the limit of :math:`\Sigma_t` as t goes to infinity by
solving the associated Riccati equation. Computation is via the
doubling algorithm (see the documentation in
`matrix_eqn.solve_discrete_riccati`).
Returns
-------
Sigma_infinity : array_like or scalar(float)
The infinite limit of Sigma_t
The infinite limit of :math:`\Sigma_t`
K_infinity : array_like or scalar(float)
The stationary Kalman gain.
Expand Down
6 changes: 3 additions & 3 deletions quantecon/lae.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""
r"""
Filename: lae.py
Authors: Thomas J. Sargent, John Stachurski,
Expand All @@ -11,12 +11,12 @@
\frac{1}{n} \sum_{i=0}^n p(X_{t-1}^i, y)
This is a density in y.
This is a density in :math:`y`.
References
----------
http://quant-econ.net/py/stationary_densities.html
https://lectures.quantecon.org/py/stationary_densities.html
"""
from textwrap import dedent
Expand Down
Loading

0 comments on commit a82ba69

Please sign in to comment.