Skip to content

Commit 8a3c5ff

Browse files
committed
DOC: Improve documentation
Fix some broken examples Fix minor typos
1 parent bbfc9cd commit 8a3c5ff

File tree

8 files changed

+64
-57
lines changed

8 files changed

+64
-57
lines changed

arch/unitroot/unitroot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,7 @@ class KPSS(UnitRootTest):
10571057
>>> print('{0:0.4f}'.format(kpss.stat))
10581058
0.2870
10591059
>>> print('{0:0.4f}'.format(kpss.pvalue))
1060-
0.1474
1060+
0.1473
10611061
>>> kpss.trend = 'ct'
10621062
>>> print('{0:0.4f}'.format(kpss.stat))
10631063
0.2075

doc/source/bootstrap/confidence-intervals.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ deviation and the Sharpe ratio.
2929

3030
The setup makes use of return data downloaded from Yahoo!
3131

32-
::
32+
.. code-block:: python
3333
3434
import datetime as dt
3535
@@ -47,7 +47,7 @@ The setup makes use of return data downloaded from Yahoo!
4747
4848
The main function used will return a 3-element array containing the parameters.
4949

50-
::
50+
.. code-block:: python
5151
5252
def sharpe_ratio(x):
5353
mu, sigma = 12 * x.mean(), np.sqrt(12 * x.var())
@@ -77,7 +77,7 @@ This example makes use of the percentile bootstrap which is conceptually the
7777
simplest method - it constructs many bootstrap replications and returns
7878
order statistics from these empirical distributions.
7979

80-
::
80+
.. code-block:: python
8181
8282
from arch.bootstrap import IIDBootstrap
8383
@@ -128,7 +128,7 @@ the :math:`\alpha/2` and :math:`1-\alpha/2` empirical quantiles of the bootstrap
128128
distribution. When :math:`\theta` is a vector, the empirical quantiles are
129129
computed element-by-element.
130130

131-
::
131+
.. code-block:: python
132132
133133
from arch.bootstrap import IIDBootstrap
134134
@@ -151,7 +151,7 @@ where :math:`\hat{\theta}^{\star}_{l}` and :math:`\hat{\theta}^{\star}_{u}` are
151151
the :math:`\alpha/2` and :math:`1-\alpha/2` empirical quantiles of the bootstrap
152152
distribution.
153153

154-
::
154+
.. code-block:: python
155155
156156
from arch.bootstrap import IIDBootstrap
157157
@@ -173,7 +173,7 @@ distribution. The confidence interval is then
173173
where :math:`\hat{\sigma}` is the bootstrap estimate of the parameter standard
174174
error.
175175

176-
::
176+
.. code-block:: python
177177
178178
from arch.bootstrap import IIDBootstrap
179179
@@ -204,7 +204,7 @@ The version that uses a nested bootstrap is simple to implement although it can
204204
be slow since it requires :math:`B` inner bootstraps of each of the :math:`B`
205205
outer bootstraps.
206206

207-
::
207+
.. code-block:: python
208208
209209
from arch.bootstrap import IIDBootstrap
210210
@@ -216,7 +216,7 @@ standard error of the parameters. In this example, this can be done using a
216216
method-of-moments argument and the delta-method. A detailed description of
217217
the mathematical formula is beyond the intent of this document.
218218

219-
::
219+
.. code-block:: python
220220
221221
def sharpe_ratio_se(params, x):
222222
mu, sigma, sr = params
@@ -236,7 +236,7 @@ the mathematical formula is beyond the intent of this document.
236236
The studentized bootstrap can then be implemented using the standard error
237237
function.
238238

239-
::
239+
.. code-block:: python
240240
241241
from arch.bootstrap import IIDBootstrap
242242
bs = IIDBootstrap(returns)
@@ -260,7 +260,7 @@ Bias-corrected (``bc``, ``bias-corrected`` or ``debiased``)
260260
The bias corrected bootstrap makes use of a bootstrap estimate of the bias to
261261
improve confidence intervals.
262262

263-
::
263+
.. code-block:: python
264264
265265
from arch.bootstrap import IIDBootstrap
266266
bs = IIDBootstrap(returns)
@@ -278,7 +278,7 @@ offer higher-order accuracy if some conditions are satisfied. Bias-corrected
278278
confidence intervals are a special case of BCa intervals where the acceleration
279279
parameter is set to 0.
280280

281-
::
281+
.. code-block:: python
282282
283283
from arch.bootstrap import IIDBootstrap
284284

doc/source/bootstrap/low-level-interface.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ from a function and the bootstrapped data.
88

99
This example makes use of monthly S&P 500 data.
1010

11-
::
11+
.. code-block:: python
1212
1313
import datetime as dt
1414
@@ -27,15 +27,15 @@ This example makes use of monthly S&P 500 data.
2727
The function will compute the Sharpe ratio -- the (annualized) mean divided by
2828
the (annualized) standard deviation.
2929

30-
::
30+
.. code-block:: python
3131
3232
import numpy as np
3333
def sharpe_ratio(x):
3434
return np.array([12 * x.mean() / np.sqrt(12 * x.var())])
3535
3636
The bootstrapped Sharpe ratios can be directly computed using `apply`.
3737

38-
::
38+
.. code-block:: python
3939
4040
import seaborn
4141
from arch.bootstrap import IIDBootstrap
@@ -59,7 +59,7 @@ arguments passed when constructing the instance.
5959
This example makes uses of simulated data to demonstrate how to use the
6060
bootstrap iterator.
6161

62-
::
62+
.. code-block:: python
6363
6464
import pandas as pd
6565
import numpy as np

doc/source/bootstrap/parameter-covariance-estimation.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ traditional estimators.
77
This example estimates the covariance of the mean, standard deviation and
88
Sharpe ratio of the S&P 500 using Yahoo! Finance data.
99

10-
::
10+
.. code-block:: python
1111
1212
import datetime as dt
1313
import pandas as pd
@@ -24,7 +24,7 @@ Sharpe ratio of the S&P 500 using Yahoo! Finance data.
2424
2525
The function that returns the parameters.
2626

27-
::
27+
.. code-block:: python
2828
2929
def sharpe_ratio(r):
3030
mu = 12 * r.mean(0)
@@ -36,7 +36,7 @@ Like all applications of the bootstrap, it is important to choose a bootstrap
3636
that captures the dependence in the data. This example uses the stationary
3737
bootstrap with an average block size of 12.
3838

39-
::
39+
.. code-block:: python
4040
4141
import pandas as pd
4242
from arch.bootstrap import StationaryBootstrap
@@ -50,7 +50,7 @@ bootstrap with an average block size of 12.
5050
5151
The output is
5252

53-
::
53+
.. code-block:: python
5454
5555
>>> params
5656
mu 8.148534

doc/source/bootstrap/semiparametric-parametric-bootstrap.rst

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ For simplicity, consider a semiparametric bootstrap of an OLS regression.
1616
The bootstrap step will combine the original parameter estimates and original
1717
regressors with bootstrapped residuals to construct a bootstrapped
1818
regressand. The bootstrap regressand and regressors can then be used to
19-
produce a bootstraped parameter estimate.
19+
produce a bootstrapped parameter estimate.
2020

2121
The user-provided function must:
2222

@@ -25,43 +25,48 @@ The user-provided function must:
2525
to construct bootstrapped residuals, simulate the regressand, and then
2626
estimate the bootstrapped parameters
2727

28-
::
28+
.. code-block:: python
2929
3030
import numpy as np
3131
def ols(y, x, params=None, x_orig=None):
3232
if params is None:
33-
return np.linalg.pinv(x).dot(y)
33+
return np.linalg.pinv(x).dot(y).ravel()
3434
3535
# When params is not None
3636
# Bootstrap residuals
3737
resids = y - x.dot(params)
3838
# Simulated data
3939
y_star = x_orig.dot(params) + resids
4040
# Parameter estimates
41-
return np.linalg.pinv(x_orig).dot(y_star)
41+
return np.linalg.pinv(x_orig).dot(y_star).ravel()
42+
43+
44+
.. note::
45+
46+
The function should return a 1-dimensional array. ``ravel`` is used above to
47+
ensure that the parameters estimated are 1d.
4248

4349
This function can then be used to perform a semiparametric bootstrap
4450

45-
::
51+
.. code-block:: python
4652
4753
from arch.bootstrap import IIDBootstrap
48-
x = np.random.randn(100,3)
49-
e = np.random.randn(100,1)
50-
b = np.arange(1,4)
54+
x = np.random.randn(100, 3)
55+
e = np.random.randn(100, 1)
56+
b = np.arange(1, 4)[:, None]
5157
y = x.dot(b) + e
5258
bs = IIDBootstrap(y, x)
5359
ci = bs.conf_int(ols, 1000, method='percentile',
5460
sampling='semi', extra_kwargs={'x_orig': x})
5561
56-
5762
Using ``partial`` instead of ``extra_kwargs``
5863
=============================================
5964

6065
``functools.partial`` can be used instead to provide a wrapper function which
6166
can then be used in the bootstrap. This example fixed the value of ``x_orig``
6267
so that it is not necessary to use ``extra_kwargs``.
6368

64-
::
69+
.. code-block:: python
6570
6671
from functools import partial
6772
ols_partial = partial(ols, x_orig=x)
@@ -77,22 +82,22 @@ the bootstrap.
7782

7883
First, the function used must be account for this structure.
7984

80-
::
85+
.. code-block:: python
8186
8287
def ols_semi_v2(y, x, resids=None, params=None, x_orig=None):
8388
if params is None:
84-
return np.linalg.pinv(x).dot(y)
89+
return np.linalg.pinv(x).dot(y).ravel()
8590
8691
# Simulated data if params provided
8792
y_star = x_orig.dot(params) + resids
8893
# Parameter estimates
89-
return np.linalg.pinv(x_orig).dot(y_star)
94+
return np.linalg.pinv(x_orig).dot(y_star).ravel()
9095
9196
This version can then be used to *directly* implement a semiparametric
9297
bootstrap, although ultimately it is not meaningfully simpler than the
9398
previous method.
9499

95-
::
100+
.. code-block:: python
96101
97102
resids = y - x.dot(ols_semi_v2(y,x))
98103
bs = IIDBootstrap(y, x, resids=resids)
@@ -139,26 +144,26 @@ This example continues the OLS example from the semiparametric example,
139144
only assuming that residuals are normally distributed. The variance
140145
estimator is the MLE.
141146

142-
::
147+
.. code-block:: python
143148
144-
def ols_para(y, x, params=None, rng=None, x_orig=None):
149+
def ols_para(y, x, params=None, state=None, x_orig=None):
145150
if params is None:
146151
beta = np.linalg.pinv(x).dot(y)
147152
e = y - x.dot(beta)
148-
sigma2 = e.dot(e) / e.shape[0]
149-
return np.hstack([beta,sigma2])
153+
sigma2 = e.T.dot(e) / e.shape[0]
154+
return np.r_[beta.ravel(), sigma2.ravel()]
150155
151156
beta = params[:-1]
152157
sigma2 = params[-1]
153-
e = rng.standard_normal(x_orig.shape[0])
154-
ystar = x_orig.dot(params) + np.sqrt(sigma2) * e
158+
e = state.standard_normal(x_orig.shape[0])
159+
ystar = x_orig.dot(beta) + np.sqrt(sigma2) * e
155160
156161
# Use the plain function to compute parameters
157162
return ols_para(ystar, x_orig)
158163
159164
This function can then be used to form parametric bootstrap confidence intervals.
160165

161-
::
166+
.. code-block:: python
162167
163168
bs = IIDBootstrap(y,x)
164169
ci = bs.conf_int(ols_para, 1000, method='percentile',

doc/source/unitroot/introduction.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ contains a single variable.
77

88
All tests share a common structure. The key elements are:
99

10-
* `stat` - Returns the test statistic
11-
* `pvalue` - Returns the p-value of the test statistic
12-
* `lags` - Sets or gets the number of lags used in the model. In most test, can be `None` to trigger automatic selection.
13-
* `trend` - Sets of gets the trend used in the model. Supported trends vary by model, but include:
10+
- `stat` - Returns the test statistic
11+
- `pvalue` - Returns the p-value of the test statistic
12+
- `lags` - Sets or gets the number of lags used in the model. In most test, can be ``None`` to trigger automatic selection.
13+
- `trend` - Sets or gets the trend used in the model. Supported trends vary by model, but include:
14+
1415
- `'nc'`: No constant
1516
- `'c'`: Constant
1617
- `'ct'`: Constant and time trend
1718
- `'ctt'`: Constant, time trend and quadratic time trend
18-
* `summary()` - Returns a summary object that can be printed to get a formatted table
19+
20+
- `summary()` - Returns a summary object that can be printed to get a formatted table
1921

2022

2123
Basic Example
@@ -26,7 +28,7 @@ defined as the difference between the yields of large portfolios of BAA and AAA
2628
uses a constant and time trend.
2729

2830

29-
::
31+
.. code-block:: python
3032
3133
import datetime as dt
3234

doc/source/univariate/forecasting.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ the choices available for forecasting. The model can be described as
4141
4242
In code this model can be constructed using data from the S&P 500 using
4343

44-
::
44+
.. code-block:: python
4545
4646
from arch import arch_model
4747
import datetime as dt
@@ -55,7 +55,7 @@ In code this model can be constructed using data from the S&P 500 using
5555
The model will be estimated using the first 10 years to estimate parameters
5656
and then forecasts will be produced for the final 5.
5757

58-
::
58+
.. code-block:: python
5959
6060
split_date = dt.datetime(2010,1,1)
6161
res = am.fit(last_obs=split_date)
@@ -79,7 +79,7 @@ Variance forecasts are constructed for the conditional variances as
7979
& = & \omega + \left(\alpha + \beta\right) E_{t}[\sigma^2_{t+h-1}] \, h \geq 2
8080
\end{eqnarray}
8181
82-
::
82+
.. code-block:: python
8383
8484
forecasts = res.forecast(horizon=5, start=split_date)
8585
forecasts.variance[split_date:].plot()
@@ -121,7 +121,7 @@ The final variance forecasts are then computed using the :math:`B` simulations
121121
E_t[\epsilon^2_{t+h}] = \sigma^2_{t+h} = B^{-1}\sum_{b=1}^B \sigma^2_{t+h,b}.
122122
\end{equation}
123123
124-
::
124+
.. code-block:: python
125125
126126
forecasts = res.forecast(horizon=5, start=split_date, method='simulation')
127127
@@ -188,7 +188,7 @@ The three core attributes are
188188

189189
Each attribute contains a ``DataFrame`` with a common structure.
190190

191-
::
191+
.. code-block:: python
192192
193193
print(forecasts.variance.tail())
194194
@@ -213,7 +213,7 @@ made using data **up to and including** December 31, 2013.
213213
By default forecasts are only produced for observations after the final
214214
observation used to estimate the model.
215215

216-
::
216+
.. code-block:: python
217217
218218
day = dt.timedelta(1)
219219
print(forecasts.variance[split_date - 5 * day:split_date + 5 * day])

0 commit comments

Comments
 (0)