### Setup IPython Notebook Environment Source: https://github.com/bashtage/arch/blob/main/examples/unitroot_examples.ipynb Required imports and settings for running examples in an IPython notebook. Includes warnings suppression and matplotlib/seaborn configuration. ```python import warnings import matplotlib.pyplot as plt import seaborn as sns warnings.simplefilter("ignore") sns.set_style("darkgrid") plt.rc("figure", figsize=(16, 6)) plt.rc("savefig", dpi=90) plt.rc("font", family="sans-serif") plt.rc("font", size=14) ``` -------------------------------- ### Install ARCH using pip Source: https://github.com/bashtage/arch/blob/main/README.md Standard installation of the ARCH library from PyPI using pip. ```shell pip install arch ``` -------------------------------- ### Wrapper for Probit with Starting Parameters Source: https://github.com/bashtage/arch/blob/main/examples/bootstrap_examples.ipynb A wrapper for the Probit model that accepts starting parameters for the `fit` method. This can improve convergence speed. ```python def probit_wrap_start_params(endog, exog, start_params=None): return sm.Probit(endog, exog).fit(start_params=start_params, disp=0).params ``` -------------------------------- ### Install Latest ARCH from GitHub Source: https://github.com/bashtage/arch/blob/main/README.md Installs the latest development version of the ARCH library directly from its GitHub repository using pip. ```bash pip install git+https://github.com/bashtage/arch.git ``` -------------------------------- ### Install ARCH with Numba support (No Compilation) Source: https://github.com/bashtage/arch/blob/main/README.md This shell command installs the ARCH library using pip, disabling binary compilation by setting the ARCH_NO_BINARY environment variable. This is useful when Numba is installed and compilation is not desired. ```shell export ARCH_NO_BINARY=1 python -m pip install arch ``` -------------------------------- ### Install ARCH with Numba support (No Compilation - PowerShell) Source: https://github.com/bashtage/arch/blob/main/README.md This PowerShell command installs the ARCH library using pip, disabling binary compilation by setting the ARCH_NO_BINARY environment variable. This is useful when Numba is installed and compilation is not desired. ```powershell $env:ARCH_NO_BINARY=1 python -m pip install arch ``` -------------------------------- ### Performing Semiparametric Bootstrap with IIDBootstrap Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/semiparametric-parametric-bootstrap.rst Example of initializing IIDBootstrap and calculating confidence intervals using a custom OLS function for semiparametric bootstrapping. ```python from arch.bootstrap import IIDBootstrap x = np.random.randn(100, 3) e = np.random.randn(100, 1) b = np.arange(1, 4)[:, None] y = x.dot(b) + e bs = IIDBootstrap(y, x) ci = bs.conf_int(ols, 1000, method='percentile', sampling='semi', extra_kwargs={'x_orig': x}) ``` -------------------------------- ### Rolling Window Forecasting Example Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_forecasting.ipynb Demonstrates rolling window forecasting using 'first_obs' and 'last_obs' to maintain a fixed sample length. Forecasts are generated one step at a time from the final observation. ```python index = returns.index start_loc = 0 end_loc = np.where(index >= "2010-1-1")[0].min() forecasts = {} for i in range(20): sys.stdout.write(".") sys.stdout.flush() res = am.fit(first_obs=i, last_obs=i + end_loc, disp="off") temp = res.forecast(horizon=3).variance fcast = temp.iloc[0] forecasts[ fcast.name] = fcast print() print(pd.DataFrame(forecasts).T) ``` -------------------------------- ### Calculate Covariance with Starting Parameters Source: https://github.com/bashtage/arch/blob/main/examples/bootstrap_examples.ipynb Calculates the parameter covariance matrix using IIDBootstrap, providing the full sample estimated parameters as starting values to the wrapper. Resets the bootstrap object before calculation. ```python bs.reset() # Reset to original state for comparability cov = bs.cov( probit_wrap_start_params, 1000, extra_kwargs={"start_params": params.values} ) cov = pd.DataFrame(cov, index=exog.columns, columns=exog.columns) print(cov) ``` -------------------------------- ### Performing Semiparametric Bootstrap with Alternative Method Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/semiparametric-parametric-bootstrap.rst Example of using the alternative OLS function (ols_semi_v2) and initializing IIDBootstrap with pre-computed residuals for a semiparametric bootstrap. ```python resids = y - x.dot(ols_semi_v2(y,x)) bs = IIDBootstrap(y, x, resids=resids) bs.conf_int(ols_semi_v2, 1000, sampling='semi', extra_kwargs={'x_orig': x}) ``` -------------------------------- ### Construct ARCH Model using Constructor Source: https://github.com/bashtage/arch/blob/main/doc/source/univariate/introduction.rst Use the `arch_model` constructor for a simple and common way to specify ARCH models. This example fetches S&P 500 data and calculates returns to be used in the model. ```python import datetime as dt import pandas_datareader.data as web from arch import arch_model start = dt.datetime(2000, 1, 1) end = dt.datetime(2014, 1, 1) sp500 = web.DataReader('^GSPC', 'yahoo', start=start, end=end) returns = 100 * sp500['Adj Close'].pct_change().dropna() am = arch_model(returns) ``` -------------------------------- ### Load S&P 500 Data and Calculate Returns Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_forecasting.ipynb Loads S&P 500 historical data and calculates daily percentage returns. Ensure the 'arch' library is installed and data is accessible. ```python import sys import arch.data.sp500 import numpy as np import pandas as pd from arch import arch_model data = arch.data.sp500.load() market = data["Adj Close"] returns = 100 * market.pct_change().dropna() ``` -------------------------------- ### Setup Imports and Plotting Styles Source: https://github.com/bashtage/arch/blob/main/examples/univariate_using_fixed_variance.ipynb Required imports for plotting and setting up the plotting style using matplotlib and seaborn. This code is intended for IPython notebooks. ```python import matplotlib.pyplot as plt import seaborn as sns sns.set_style("darkgrid") plt.rc("figure", figsize=(16, 6)) plt.rc("savefig", dpi=90) plt.rc("font", family="sans-serif") plt.rc("font", size=14) ``` -------------------------------- ### Fit GJR-GARCH(1,1,1) with Power=1.0 Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_modeling.ipynb Fits a GJR-GARCH model with the `power` parameter set to 1.0, which is standard for GARCH models. This is equivalent to the previous example if `power` defaults to 1.0. ```python am = arch_model(returns, p=1, o=1, q=1, power=1.0) res = am.fit(update_freq=5) print(res.summary()) ``` -------------------------------- ### Recursive Forecast Generation Example Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_forecasting.ipynb Implements recursive forecasting by omitting 'first_obs', allowing the initial observation to remain constant while the end observation advances. This is similar to rolling windows but with a growing sample. ```python index = returns.index start_loc = 0 end_loc = np.where(index >= "2010-1-1")[0].min() forecasts = {} for i in range(20): sys.stdout.write(".") sys.stdout.flush() res = am.fit(last_obs=i + end_loc, disp="off") temp = res.forecast(horizon=3).variance fcast = temp.iloc[0] forecasts[ fcast.name] = fcast print() print(pd.DataFrame(forecasts).T) ``` -------------------------------- ### Install ARCH using Conda Source: https://github.com/bashtage/arch/blob/main/README.md Installs the ARCH library from the conda-forge channel using the conda package manager. Note the package name is 'arch-py'. ```bash conda install arch-py -c conda-forge ``` -------------------------------- ### Augmented Dickey-Fuller Test Example Source: https://github.com/bashtage/arch/blob/main/doc/source/unitroot/introduction.rst Demonstrates using the Augmented Dickey-Fuller test to check for a unit root in a time series. Requires pandas-datareader to fetch data. The trend is set to 'ct' for constant and linear time trend. ```python import datetime as dt import pandas_datareader.data as web from arch.unitroot import ADF start = dt.datetime(1919, 1, 1) end = dt.datetime(2014, 1, 1) df = web.DataReader(["AAA", "BAA"], "fred", start, end) df['diff'] = df['BAA'] - df['AAA'] adf = ADF(df['diff']) adf.trend = 'ct' print(adf.summary()) ``` -------------------------------- ### Long-run Covariance Estimation using Bartlett Kernel Source: https://github.com/bashtage/arch/blob/main/README.md This example shows how to estimate the long-run covariance of returns using the Bartlett kernel, commonly known as Newey-West. It requires the Bartlett class from arch.covariance.kernel and data loaded using arch.data.nasdaq. The bandwidth is selected automatically. ```python from arch.covariance.kernel import Bartlett from arch.data import nasdaq data = nasdaq.load() returns = data[["Adj Close"]].pct_change().dropna() cov_est = Bartlett(returns ** 2) # Get the long-run covariance cov_est.cov.long_run ``` -------------------------------- ### Generate Longer Horizon Forecasts Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_forecasting.ipynb Generates forecasts for a specified horizon (e.g., 5 steps ahead) by passing the 'horizon' parameter to the forecast method. This example prints the residual variance for the longer horizon. ```python forecasts = res.forecast(horizon=5) print(forecasts.residual_variance.iloc[-3:]) ``` -------------------------------- ### Create and Fit GARCH(1,1) Model Source: https://context7.com/bashtage/arch/llms.txt Demonstrates creating a basic GARCH(1,1) model with a constant mean and fitting it to sample returns data. Use disp='off' to suppress convergence output. ```python import numpy as np import pandas as pd from arch import arch_model # Generate sample returns data np.random.seed(42) returns = np.random.randn(1000) * 0.01 + 0.0001 # Basic GARCH(1,1) with constant mean model = arch_model(returns, mean='Constant', vol='GARCH', p=1, q=1) result = model.fit(disp='off') print(result.summary()) ``` -------------------------------- ### Initialize Bootstrap Iterator with Simulated Data Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/low-level-interface.rst Initializes an IIDBootstrap instance with simulated positional and keyword arguments. Requires pandas and numpy. ```python import pandas as pd import numpy as np from arch.bootstrap import IIDBootstrap x = np.random.randn(1000, 2) y = pd.DataFrame(np.random.randn(1000, 3)) z = np.random.rand(1000, 10) bs = IIDBootstrap(x, y=y, z=z) ``` -------------------------------- ### Build and Fit Constant Mean Model with GARCH and Student's t Source: https://context7.com/bashtage/arch/llms.txt Constructs a ConstantMean model with GARCH volatility and Student's t distribution components. It then fits the model and prints estimated parameters and statistics. ```python from arch.univariate import ConstantMean, GARCH, StudentsT import numpy as np # Create sample data np.random.seed(123) data = np.random.randn(500) * 0.02 # Build model with components model = ConstantMean(data) model.volatility = GARCH(p=1, o=0, q=1) model.distribution = StudentsT() # Fit and examine results result = model.fit(disp='off') print(f"Constant (mu): {result.params['mu']:.6f}") print(f"GARCH omega: {result.params['omega']:.6f}") print(f"GARCH alpha: {result.params['alpha[1]']:.4f}") print(f"GARCH beta: {result.params['beta[1]']:.4f}") print(f"Student-t df: {result.params['nu']:.2f}") # Access conditional volatility cond_vol = result.conditional_volatility print(f"Average conditional volatility: {cond_vol.mean():.6f}") # Standardized residuals std_resid = result.std_resid print(f"Std residuals mean: {std_resid.mean():.4f}, std: {std_resid.std():.4f}") ``` -------------------------------- ### Generate Analytical Variance Forecasts Source: https://github.com/bashtage/arch/blob/main/doc/source/univariate/forecasting.rst Generates 5-step ahead analytical variance forecasts for a fitted GARCH model and plots the results starting from the split date. ```python forecasts = res.forecast(horizon=5, start=split_date) forecasts.variance[split_date:].plot() ``` -------------------------------- ### Prepare for Simulation Study Source: https://github.com/bashtage/arch/blob/main/examples/multiple-comparison_examples.ipynb Initializes an empty list to store p-values and sets up parameters for a simulation study. Generates a specified number of seeds for replications, limiting bootstrap iterations per SPA application. ```python # Save the pvalues pvalues = [] b = 100 seeds = gen.integers(0, 2**31 - 1, b) ``` -------------------------------- ### Using functools.partial for Semiparametric Bootstrap Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/semiparametric-parametric-bootstrap.rst Demonstrates using functools.partial to wrap the OLS function, fixing 'x_orig' and simplifying the call to conf_int. ```python from functools import partial ols_partial = partial(ols, x_orig=x) ci = bs.conf_int(ols_partial, 1000, sampling='semi') ``` -------------------------------- ### Reproducible Simulation with RandomState Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_modeling.ipynb Create a reproducible simulation by initializing the model with a specific NumPy RandomState. This allows the simulation to be rerun with the exact same random values. Requires 'numpy' and 'arch' libraries. ```python import numpy as np from arch.univariate import ConstantMean, SkewStudent rs = np.random.RandomState([892380934, 189201902, 129129894, 9890437]) # Save the initial state to reset later state = rs.get_state() dist = SkewStudent(seed=rs) vol = GARCH(p=1, o=1, q=1) repro_mod = ConstantMean(None, volatility=vol, distribution=dist) repro_mod.simulate(res.params, 1000).head() ``` -------------------------------- ### Simulate Forecasts with Dynamic Exogenous Variables Source: https://github.com/bashtage/arch/blob/main/examples/univariate_forecasting_with_exogenous_variables.ipynb Simulate distinct paths for exogenous variables using the `sim_ar1` function, then generate forecast paths based on these simulated 'x' values. This accounts for the variance of 'x' in the forecast simulation. ```python simulations = [] rng = RandomState(20210301) for _ in range(100): x0_sim = sim_ar1(np.array([1, 0.8]), x0.iloc[-1], 10, rng) x1_sim = sim_ar1(np.array([2.5, 0.5]), x1.iloc[-1], 10, rng) x = {"x0": x0_sim, "x1": x1_sim} fcast = res.forecast(horizon=10, x=x, method="simulation", simulations=1) simulations.append(fcast.simulations.values) ``` -------------------------------- ### Standard Error Function for Studentized Bootstrap Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/confidence-intervals.rst Example of a standard error function required for the studentized bootstrap when analytical standard errors are available. It must return a 1-D array matching the number of parameters. ```python def sharpe_ratio_se(params, x): mu, sigma, sr = params y = 12 * x e1 = y - mu e2 = y ** 2.0 - sigma ** 2.0 errors = np.vstack((e1, e2)).T t = errors.shape[0] vcv = errors.T.dot(errors) / t D = np.array([[1, 0], [0, 0.5 * 1 / sigma], [1.0 / sigma, - mu / (2.0 * sigma**3)] ]) avar = D.dot(vcv /t).dot(D.T) return np.sqrt(np.diag(avar)) ``` -------------------------------- ### Fit ARCH Model and Display Parameters Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_modeling.ipynb Fit an ARCH model to data and display the estimated parameters. Requires the 'arch' and 'pandas' libraries. ```python res = arch_model(crude_ret, p=1, o=1, q=1, dist="skewt").fit(disp="off") pd.DataFrame(res.params) ``` -------------------------------- ### Simulate Benchmark and Model Losses Source: https://github.com/bashtage/arch/blob/main/examples/multiple-comparison_examples.ipynb Creates benchmark and model losses by adding measurement noise to factors, fitting OLS models on the first half of the data, and predicting/calculating MSE loss on the second half. This simulates a scenario with multiple potentially good forecasting models. ```python # Measurement noise bm_factors = factors + randn(t, 3) # Fit using first half, predict second half bm_beta = sm.OLS(y[:500], bm_factors[:500]).fit().params # MSE loss bm_losses = (y[500:] - bm_factors[500:].dot(bm_beta)) ** 2.0 # Number of models k = 500 model_factors = np.zeros((k, t, 3)) model_losses = np.zeros((500, k)) for i in range(k): # Add measurement noise model_factors[i] = factors + randn(1000, 3) # Compute regression parameters model_beta = sm.OLS(y[:500], model_factors[i, :500]).fit().params # Prediction and losses model_losses[:, i] = (y[500:] - model_factors[i, 500:].dot(model_beta)) ** 2.0 ``` -------------------------------- ### Load and Prepare Financial Data Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_modeling.ipynb Loads S&P 500 adjusted closing prices from Yahoo! Finance and calculates daily returns. This data is used for subsequent model fitting. ```python import datetime as dt import arch.data.sp500 st = dt.datetime(1988, 1, 1) en = dt.datetime(2018, 1, 1) data = arch.data.sp500.load() market = data["Adj Close"] returns = 100 * market.pct_change().dropna() ax = returns.plot() xlim = ax.set_xlim(returns.index.min(), returns.index.max()) ``` -------------------------------- ### Simulate Forecasts with Static Exogenous Variables Source: https://github.com/bashtage/arch/blob/main/examples/univariate_forecasting_with_exogenous_variables.ipynb Generate simulated forecast paths using fixed, out-of-sample 'x' values. This treats the exogenous variables as deterministic within the simulation. ```python x = {"x0": x0_oos[-1], "x1": x1_oos[-1]} sim_fixedx = res.forecast(horizon=10, x=x, method="simulation", simulations=100) sim_fixedx.simulations.values.std(1) ``` -------------------------------- ### Block Bootstrap Methods for Time Series Source: https://context7.com/bashtage/arch/llms.txt Compares StationaryBootstrap, CircularBlockBootstrap, and MovingBlockBootstrap for time series data, including optimal block length estimation. Requires numpy and specific bootstrap classes from arch.bootstrap. ```python from arch.bootstrap import StationaryBootstrap, CircularBlockBootstrap from arch.bootstrap import MovingBlockBootstrap, optimal_block_length import numpy as np np.random.seed(303) # Simulate AR(1) process n = 500 y = np.zeros(n) for t in range(1, n): y[t] = 0.7 * y[t-1] + np.random.randn() # Estimate optimal block length opt_block = optimal_block_length(y) print(f"Optimal block length (stationary): {opt_block['stationary'].values[0]:.1f}") print(f"Optimal block length (circular): {opt_block['circular'].values[0]:.1f}") # Stationary bootstrap block_size = int(opt_block['stationary'].values[0]) sb = StationaryBootstrap(block_size, y, seed=123) def ar1_coef(x): return np.corrcoef(x[1:], x[:-1])[0, 1] ci_sb = sb.conf_int(ar1_coef, reps=1000, method='percentile') print(f"AR(1) coefficient 95% CI: [{ci_sb[0, 0]:.3f}, {ci_sb[1, 0]:.3f}]") # Circular block bootstrap cbb = CircularBlockBootstrap(block_size, y, seed=123) ci_cbb = cbb.conf_int(ar1_coef, reps=1000, method='percentile') # Moving block bootstrap mbb = MovingBlockBootstrap(block_size, y, seed=123) ci_mbb = mbb.conf_int(ar1_coef, reps=1000, method='percentile') print(f"Stationary: [{ci_sb[0,0]:.3f}, {ci_sb[1,0]:.3f}]") print(f"Circular: [{ci_cbb[0,0]:.3f}, {ci_cbb[1,0]:.3f}]") print(f"Moving Block: [{ci_mbb[0,0]:.3f}, {ci_mbb[1,0]:.3f}]") ``` -------------------------------- ### Bootstraps Source: https://github.com/bashtage/arch/blob/main/doc/source/api.rst Classes for various bootstrapping methods. ```APIDOC ## Bootstraps This section lists classes for different bootstrapping techniques. #### Classes - **IIDBootstrap** (~arch.bootstrap.IIDBootstrap) - Independent and Identically Distributed bootstrap. - **IndependentSamplesBootstrap** (~arch.bootstrap.IndependentSamplesBootstrap) - Bootstrap for independent samples. - **StationaryBootstrap** (~arch.bootstrap.StationaryBootstrap) - Stationary bootstrap. - **CircularBlockBootstrap** (~arch.bootstrap.CircularBlockBootstrap) - Circular block bootstrap. - **MovingBlockBootstrap** (~arch.bootstrap.MovingBlockBootstrap) - Moving block bootstrap. ``` -------------------------------- ### Load and Prepare Financial Data Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/parameter-covariance-estimation.rst Loads S&P 500 data from Yahoo! Finance and prepares monthly returns. Requires datetime, pandas, and pandas_datareader. ```python import datetime as dt import pandas as pd import pandas_datareader.data as web start = dt.datetime(1951, 1, 1) end = dt.datetime(2014, 1, 1) sp500 = web.DataReader('^GSPC', 'yahoo', start=start, end=end) low = sp500.index.min() high = sp500.index.max() monthly_dates = pd.date_range(low, high, freq='M') monthly = sp500.reindex(monthly_dates, method='ffill') returns = 100 * monthly['Adj Close'].pct_change().dropna() ``` -------------------------------- ### Initialize and Fit GARCH(1,1) Model Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_forecasting.ipynb Initializes a GARCH(1,1) model with a Normal distribution and fits it to the return data. The 'update_freq' parameter controls how often parameter estimates are printed during fitting. ```python am = arch_model(returns, vol="Garch", p=1, o=0, q=1, dist="Normal") res = am.fit(update_freq=5) ``` -------------------------------- ### Load and Prepare NASDAQ Data Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_scenarios.ipynb Loads historical NASDAQ index data and computes daily percentage returns. Ensures data is clean by dropping any resulting NaN values. ```python import arch.data.nasdaq data = arch.data.nasdaq.load() nasdaq = data["Adj Close"] print(nasdaq.head()) ``` -------------------------------- ### Simulate Data from ARCH Model Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_modeling.ipynb Simulate data from an ARCH model using pre-estimated parameters. The simulation returns a DataFrame with simulated data, volatility, and errors. Requires the 'arch' library. ```python sim_mod = arch_model(None, p=1, o=1, q=1, dist="skewt") sim_data = sim_mod.simulate(res.params, 1000) sim_data.head() ``` -------------------------------- ### Calculate Basic Confidence Intervals Source: https://github.com/bashtage/arch/blob/main/examples/bootstrap_examples.ipynb Constructs basic confidence intervals for model parameters using IIDBootstrap. Requires IIDBootstrap, endog, exog, and the wrapper function. ```python ci = bs.conf_int(probit_wrap, 1000, method="basic") ci = pd.DataFrame(ci, index=["Lower", "Upper"], columns=exog.columns) print(ci) ``` -------------------------------- ### Methods and Properties Documentation Source: https://github.com/bashtage/arch/blob/main/doc/source/_templates/autosummary/class.rst This section covers the documentation of methods and properties within a class using `autosummary` and `rubric` directives. ```APIDOC ## Methods and Properties Documentation ### Description Documents public methods and properties of a class using `autosummary` and `rubric` directives. Private members (starting with '_') are generally excluded unless they are explicitly listed (e.g., '__call__'). ### Method N/A (Documentation Generation Directive) ### Endpoint N/A ### Parameters #### Path Parameters N/A #### Query Parameters N/A #### Request Body N/A ### Request Example N/A ### Response #### Success Response (200) N/A #### Response Example N/A ``` -------------------------------- ### Construct and Fit a GARCH Model Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_scenarios.ipynb Constructs a Constant Mean GARCH(1,1) model with Normal distribution and fits it to the return data. It's recommended to scale returns by 100 before fitting. ```python rets = 100 * nasdaq.pct_change().dropna() # Build components to set the state for the distribution random_state = np.random.RandomState(1) dist = Normal(seed=random_state) volatility = GARCH(1, 1, 1) mod = ConstantMean(rets, volatility=volatility, distribution=dist) ``` ```python res = mod.fit(disp="off") res ``` -------------------------------- ### Manually Assemble ARCH Model Components Source: https://github.com/bashtage/arch/blob/main/doc/source/univariate/introduction.rst Assemble an ARCH model by manually specifying the mean, volatility, and distribution components. This provides more granular control over the model specification. ```python from arch import ConstantMean, GARCH, Normal am = ConstantMean(returns) am.volatility = GARCH(1, 0, 1) am.distribution = Normal() ``` -------------------------------- ### ARX and AR-GARCH Model Fitting Source: https://context7.com/bashtage/arch/llms.txt Demonstrates fitting an ARX model with exogenous regressors and an AR-GARCH model with GJR-GARCH volatility. Ensure data 'y' and 'x' are properly defined before fitting. ```python from arch.univariate import ARX, GARCH # ARX with exogenous regressors arx = ARX(y, x=x, lags=1) arx_result = arx.fit(disp='off') # AR-GARCH model ar_garch = ARX(y, lags=1) ar_garch.volatility = GARCH(p=1, o=1, q=1) # GJR-GARCH ar_garch_result = ar_garch.fit(disp='off') print(f"Asymmetry (gamma): {ar_garch_result.params['gamma[1]']:.4f}") ``` -------------------------------- ### Wrapper for Probit Model Parameters Source: https://github.com/bashtage/arch/blob/main/examples/bootstrap_examples.ipynb A simple wrapper function that takes endog and exog, fits a Probit model, and returns the estimated parameters. Useful for direct use with bootstrap methods. ```python def probit_wrap(endog, exog): return sm.Probit(endog, exog).fit(disp=0).params ``` -------------------------------- ### Compute and Visualize Bootstrap Sharpe Ratios Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/low-level-interface.rst Computes bootstrap Sharpe ratios using IIDBootstrap and visualizes the results with a histogram. Requires seaborn and arch.bootstrap. ```python import seaborn from arch.bootstrap import IIDBootstrap bs = IIDBootstrap(returns) sharpe_ratios = bs.apply(sharpe_ratio, 1000) sharpe_ratios = pd.DataFrame(sharpe_ratios, columns=['Sharpe Ratio']) sharpe_ratios.hist(bins=20) ``` -------------------------------- ### Initialize Stationary Bootstrap Source: https://github.com/bashtage/arch/blob/main/examples/bootstrap_examples.ipynb Initializes a StationaryBootstrap object with a specified block length, data, and a random seed generated from entropy. This is used for time-series bootstrapping. ```python from arch.bootstrap import StationaryBootstrap # Initialize with entropy from random.org entropy = [877788388, 418255226, 989657335, 69307515] seed = np.random.default_rng(entropy) bs = StationaryBootstrap(12, excess_market, seed=seed) ``` -------------------------------- ### Compare Distributions for Crude Oil Returns Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_modeling.ipynb Fits three models to crude oil returns using Normal, Student's T, and Skewed Student's T distributions. Compares log-likelihoods and parameters. Requires pandas and collections.OrderedDict. ```python from collections import OrderedDict import arch.data.wti crude = arch.data.wti.load() crude_ret = 100 * crude.DCOILWTICO.dropna().pct_change().dropna() res_normal = arch_model(crude_ret).fit(disp="off") res_t = arch_model(crude_ret, dist="t").fit(disp="off") res_skewt = arch_model(crude_ret, dist="skewt").fit(disp="off") lls = pd.Series( OrderedDict( ( ("normal", res_normal.loglikelihood), ("t", res_t.loglikelihood), ("skewt", res_skewt.loglikelihood), ) ) ) print(lls) params = pd.DataFrame( OrderedDict( ( ("normal", res_normal.params), ("t", res_t.params), ("skewt", res_skewt.params), ) ) ) params ``` -------------------------------- ### Construct and Estimate GARCH(1,1) Model Source: https://github.com/bashtage/arch/blob/main/doc/source/univariate/forecasting.rst Constructs a GARCH(1,1) model with a Normal distribution using S&P 500 returns and estimates parameters using the first 10 years of data. ```python from arch import arch_model import datetime as dt import pandas_datareader.data as web start = dt.datetime(2000,1,1) end = dt.datetime(2014,1,1) sp500 = web.get_data_yahoo('^GSPC', start=start, end=end) returns = 100 * sp500['Adj Close'].pct_change().dropna() am = arch_model(returns, vol='Garch', p=1, o=0, q=1, dist='Normal') ``` ```python split_date = dt.datetime(2010,1,1) res = am.fit(last_obs=split_date) ``` -------------------------------- ### GARCH Volatility Models in Python Source: https://context7.com/bashtage/arch/llms.txt Implements and compares standard GARCH(1,1), GJR-GARCH(1,1,1), TARCH, EGARCH, and APARCH models. Requires numpy for data generation and arch.univariate for models. ```python from arch.univariate import ConstantMean, GARCH, EGARCH, APARCH import numpy as np np.random.seed(101) returns = np.random.randn(1000) * 0.015 # Standard GARCH(1,1) model = ConstantMean(returns) model.volatility = GARCH(p=1, o=0, q=1) result = model.fit(disp='off') # GJR-GARCH(1,1,1) for asymmetric effects model_gjr = ConstantMean(returns) model_gjr.volatility = GARCH(p=1, o=1, q=1) result_gjr = model_gjr.fit(disp='off') # TARCH (power=1.0) model_tarch = ConstantMean(returns) model_tarch.volatility = GARCH(p=1, o=1, q=1, power=1.0) result_tarch = model_tarch.fit(disp='off') # EGARCH for exponential GARCH model_egarch = ConstantMean(returns) model_egarch.volatility = EGARCH(p=1, o=1, q=1) result_egarch = model_egarch.fit(disp='off') # APARCH model model_aparch = ConstantMean(returns) model_aparch.volatility = APARCH(p=1, o=1, q=1) result_aparch = model_aparch.fit(disp='off') # Compare models using AIC/BIC print(f"GARCH AIC: {result.aic:.2f}, BIC: {result.bic:.2f}") print(f"GJR-GARCH AIC: {result_gjr.aic:.2f}, BIC: {result_gjr.bic:.2f}") print(f"EGARCH AIC: {result_egarch.aic:.2f}, BIC: {result_egarch.bic:.2f}") ``` -------------------------------- ### Forecast with Full-Size X Input Array Source: https://github.com/bashtage/arch/blob/main/examples/univariate_forecasting_with_exogenous_variables.ipynb Use an 'x' input array that matches the original 'y' data shape for forecasting. Only the necessary trailing values are used, simplifying origin point tracking. Other sizes are not allowed. ```python exog_fcast = np.array([x0_oos, x1_oos]) print(exog_fcast.shape) array_multi_forecasts = res.forecast(start=500, horizon=10, x=exog_fcast) np.max(np.abs(array_multi_forecasts.mean - multi_forecasts.mean)) ``` -------------------------------- ### Unit Root Tests in Python Source: https://context7.com/bashtage/arch/llms.txt Introduces unit root testing using ADF, DFGLS, KPSS, and Phillips-Perron tests. Requires numpy for data generation and arch.unitroot for the test classes. ```python from arch.unitroot import ADF, DFGLS, KPSS, PhillipsPerron import numpy as np np.random.seed(404) # Generate unit root process random_walk = np.cumsum(np.random.randn(500)) # Generate stationary process stationary = np.random.randn(500) ``` -------------------------------- ### Compare 1D and 2D X Forecasts for Single Variable Source: https://github.com/bashtage/arch/blob/main/examples/univariate_forecasting_with_exogenous_variables.ipynb Demonstrates that both 1-D and 2-D array formats for 'x' yield the same forecast results when a model has a single exogenous variable. ```python forecast_1d = res.forecast(horizon=10, x=x0_oos[-1]) forecast_2d = res.forecast(horizon=10, x=x0_oos[-1:]) print(forecast_1d.mean - forecast_2d.mean) ``` -------------------------------- ### Calculate Sharpe Ratio Parameters Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/parameter-covariance-estimation.rst Defines a function to calculate the mean, standard deviation, and Sharpe ratio from returns. Assumes monthly returns are provided. ```python def sharpe_ratio(r): mu = 12 * r.mean(0) sigma = np.sqrt(12 * r.var(0)) sr = mu / sigma return np.array([mu, sigma, sr]) ``` -------------------------------- ### Produce Multiple Forecasts with Dictionary Input Source: https://github.com/bashtage/arch/blob/main/examples/univariate_forecasting_with_exogenous_variables.ipynb Fit a model to a subset of data and then produce multiple forecasts for the remaining observations using a dictionary for exogenous variables. Ensure the 'x' values have the same number of rows as the forecast table. ```python res = mod.fit(disp="off", last_obs=500) exog_fcast = {"x0": x0_oos[-500:], "x1": x1_oos[-500:]} multi_forecasts = res.forecast(start=500, horizon=10, x=exog_fcast) multi_forecasts.mean.tail(10) ``` -------------------------------- ### Load and Prepare S&P 500 Data Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/low-level-interface.rst Loads monthly S&P 500 data and calculates percentage returns. Requires pandas and pandas-datareader. ```python import datetime as dt import pandas as pd import pandas_datareader.data as web start = dt.datetime(1951, 1, 1) end = dt.datetime(2014, 1, 1) sp500 = web.DataReader('^GSPC', 'yahoo', start=start, end=end) low = sp500.index.min() high = sp500.index.max() monthly_dates = pd.date_range(low, high, freq='M') monthly = sp500.reindex(monthly_dates, method='ffill') returns = 100 * monthly['Adj Close'].pct_change().dropna() ``` -------------------------------- ### Generate True Out-of-Sample Forecasts Source: https://github.com/bashtage/arch/blob/main/examples/univariate_forecasting_with_exogenous_variables.ipynb This code demonstrates how to generate true out-of-sample forecasts by first forecasting the exogenous variables and then using these forecasts as inputs to the main model's forecast method. ```python mod = arch_model(y, x=exog, mean="ARX", lags=1) res = mod.fit(disp="off") actual_x_oos = { "x0": res0.forecast(horizon=10).mean, "x1": res1.forecast(horizon=10).mean, } fcasts = res.forecast(horizon=10, x=actual_x_oos) fcasts.mean ``` -------------------------------- ### Generate Simulation-Based Variance Forecasts Source: https://github.com/bashtage/arch/blob/main/doc/source/univariate/forecasting.rst Generates 5-step ahead simulation-based variance forecasts for a fitted GARCH model. ```python forecasts = res.forecast(horizon=5, start=split_date, method='simulation') ``` -------------------------------- ### Fixed Window Forecasting Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_forecasting.ipynb Implements fixed-window forecasting by specifying 'last_obs' during model fitting. This uses data up to the specified date to generate all subsequent forecasts. Note that 'last_obs' follows Python sequence rules, excluding the specified date from the sample. ```python res = am.fit(last_obs="2011-1-1", update_freq=5) forecasts = res.forecast(horizon=5) print(forecasts.variance.dropna().head()) ``` -------------------------------- ### IID Bootstrap with Asymptotic Normal Approximation Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/confidence-intervals.rst Use the 'norm' method for confidence intervals based on asymptotic normal approximation. Requires IIDBootstrap and returns data. ```python from arch.bootstrap import IIDBootstrap bs = IIDBootstrap(returns) ci = bs.conf_int(sharpe_ratio, 1000, method='norm') ``` -------------------------------- ### Fit ARX Models and Prepare Exogenous Forecasts Source: https://github.com/bashtage/arch/blob/main/examples/univariate_forecasting_with_exogenous_variables.ipynb Fit AR(1) models for exogenous variables to obtain their conditional expectations. These are then used to construct in-sample forecasts. ```python res0 = ARX(exog["x0"], lags=1).fit() res1 = ARX(exog["x1"], lags=1).fit()) forecast_x = pd.concat( [res0.forecast(start=0).mean, res1.forecast(start=0).mean], axis=1 ) forecast_x.columns = ["x0f", "x1f"] in_samp_forcast_exog = {"x0": forecast_x[[ "x0f"]], "x1": forecast_x[[ "x1f"]].shift(-1)} cast = res.forecast(horizon=1, x=in_samp_forcast_exog, start=0) cast.mean ``` -------------------------------- ### Prepare Data for Probit Model Source: https://github.com/bashtage/arch/blob/main/examples/bootstrap_examples.ipynb Constructs the dependent variable (admit) and independent variables (gre, gpa) arrays for a Probit model. A constant term is added to the exogenous variables. ```python import statsmodels.api as sm endog = binary[["admit"]] exog = binary[["gre", "gpa"]] const = pd.Series(np.ones(exog.shape[0]), index=endog.index) const.name = "Const" exog = pd.DataFrame([const, exog.gre, exog.gpa]).T ``` -------------------------------- ### Display Estimated Parameters and Covariance Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/parameter-covariance-estimation.rst Prints the calculated parameters (mean, sigma, Sharpe Ratio) and their estimated covariance matrix. ```python >>> params mu 8.148534 sigma 14.508540 SR 0.561637 dtype: float64 >>> param_cov mu sigma SR mu 3.729435 -0.442891 0.273945 sigma -0.442891 0.495087 -0.049454 SR 0.273945 -0.049454 0.020830 ``` -------------------------------- ### Visualize Value at Risk (VaR) Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_forecasting.ipynb Calculates and plots the Value at Risk (VaR) based on conditional mean and volatility, comparing it with actual returns. Requires 'cond_mean', 'cond_var', 'returns', 'np', 'pd', 'markers', and 'labels' to be defined. ```python value_at_risk = -cond_mean.values - np.sqrt(cond_var).values * q.values[None, :] value_at_risk = pd.DataFrame(value_at_risk, columns=["1%", "5%"], index=cond_var.index) ax = value_at_risk.plot(legend=False) xl = ax.set_xlim(value_at_risk.index[0], value_at_risk.index[-1]) rets_2018 = returns["2018":].copy() rets_2018.name = "S&P 500 Return" c = [] for idx in value_at_risk.index: if rets_2018[idx] > -value_at_risk.loc[idx, "5%"]: c.append("#000000") elif rets_2018[idx] < -value_at_risk.loc[idx, "1%"]: c.append("#BB0000") else: c.append("#BB00BB") c = np.array(c, dtype="object") for color in np.unique(c): sel = c == color ax.scatter( rets_2018.index[sel], -rets_2018.loc[sel], marker=markers[color], c=c[sel], label=labels[color], ) ax.set_title("Filtered Historical Simulation VaR") leg = ax.legend(frameon=False, ncol=3) ``` -------------------------------- ### Re-run Bootstrap with Optimal Block Length Source: https://github.com/bashtage/arch/blob/main/examples/bootstrap_examples.ipynb Re-initializes the Stationary Bootstrap using the estimated optimal block length and re-runs the analysis. This may reveal slightly more extreme values in the distribution. ```python # Reinitialize using the same entropy rs = np.random.default_rng(entropy) bs = StationaryBootstrap(opt.loc["Mkt-RF", "stationary"], excess_market, seed=seed) results = bs.apply(sharpe_ratio, 2500) SR = pd.DataFrame(results[:, -1:], columns=["SR"]) fig = SR.hist(bins=40) ``` -------------------------------- ### Load and Plot Crude Oil Prices Source: https://github.com/bashtage/arch/blob/main/examples/unitroot_cointegration_examples.ipynb Loads crude oil price data and plots the natural logarithm of WTI and Brent spot prices. ```python import numpy as np from arch.data import crude data = crude.load() log_price = np.log(data) ax = log_price.plot() xl = ax.set_xlim(log_price.index.min(), log_price.index.max()) ``` -------------------------------- ### Estimate Probit Model Parameters Source: https://github.com/bashtage/arch/blob/main/examples/bootstrap_examples.ipynb Estimates Probit model parameters using statsmodels and prints the results. Requires endog and exog data. ```python mod = sm.Probit(endog, exog) fit = mod.fit(disp=0) params = fit.params print(params) ``` -------------------------------- ### Generate Parametric Bootstrap Confidence Intervals Source: https://github.com/bashtage/arch/blob/main/doc/source/bootstrap/semiparametric-parametric-bootstrap.rst This snippet shows how to create an IIDBootstrap instance and use it to compute percentile confidence intervals using the parametric sampling method. The `extra_kwargs` are passed to the `ols_para` function. ```python bs = IIDBootstrap(y,x) ci = bs.conf_int(ols_para, 1000, method='percentile', sampling='parametric', extra_kwargs={'x_orig': x}) ``` -------------------------------- ### Kwiatkowski-Phillips-Schmidt-Shin Test Source: https://github.com/bashtage/arch/blob/main/examples/unitroot_examples.ipynb Instantiates and runs the KPSS test with default settings. The summary of the test results is printed. The null hypothesis for this test is stationarity. ```python from arch.unitroot import KPSS kpss = KPSS(default) print(kpss.summary().as_text()) ``` -------------------------------- ### Generate and Plot Bootstrap Forecasts Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_forecasting.ipynb Generates bootstrap-based forecasts and plots the simulated residual variances against the expected variance. Requires numpy. ```python forecasts = res.forecast(horizon=5, method="bootstrap") sims = forecasts.simulations lines = plt.plot(x, sims.residual_variances[-1, ::5].T, color="#9cb2d6", alpha=0.5) lines[0].set_label("Simulated path") line = plt.plot(x, forecasts.variance.iloc[-1].values, color="#002868") line[0].set_label("Expected variance") plt.gca().set_xticks(x) plt.gca().set_xlim(1, 5) legend = plt.legend() ``` -------------------------------- ### Simulate GARCH Model Forecasts Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_scenarios.ipynb Generates simulation-based forecasts for residual variance using the fitted GARCH model. This method converges to analytical forecasts as the number of simulations increases. ```python sim_forecasts = res.forecast(start="1-1-2017", method="simulation", horizon=10) print(sim_forecasts.residual_variance.dropna().head()) ``` -------------------------------- ### Import Libraries for ARCH Modeling Source: https://github.com/bashtage/arch/blob/main/examples/univariate_volatility_scenarios.ipynb Imports necessary libraries for data manipulation, plotting, and ARCH modeling. Sets up plotting styles and figure parameters. ```python from __future__ import annotations import matplotlib.pyplot as plt import numpy as np import pandas as pd import seaborn as sns from arch.univariate import GARCH, ConstantMean, Normal sns.set_style("darkgrid") plt.rc("figure", figsize=(16, 6)) plt.rc("savefig", dpi=90) plt.rc("font", family="sans-serif") plt.rc("font", size=14) ``` -------------------------------- ### Bootstrap Confidence Intervals for Sharpe Ratio Source: https://github.com/bashtage/arch/blob/main/README.md This snippet demonstrates how to compute bootstrap confidence intervals for the Sharpe ratio of financial returns. It requires pandas, numpy, and the IIDBootstrap class from arch.bootstrap. Ensure data is preprocessed into monthly returns. ```python import datetime as dt import pandas as pd import numpy as np import pandas_datareader.data as web start = dt.datetime(1951,1,1) end = dt.datetime(2014,1,1) sp500 = web.get_data_yahoo('^GSPC', start=start, end=end) start = sp500.index.min() end = sp500.index.max() monthly_dates = pd.date_range(start, end, freq='M') monthly = sp500.reindex(monthly_dates, method='ffill') returns = 100 * monthly['Adj Close'].pct_change().dropna() def sharpe_ratio(x): mu, sigma = 12 * x.mean(), np.sqrt(12 * x.var()) return np.array([mu, sigma, mu / sigma]) from arch.bootstrap import IIDBootstrap bs = IIDBootstrap(returns) ci = bs.conf_int(sharpe_ratio, 1000, method='percentile') ```