5
$\begingroup$

Is there a package to run simplified multivariate GARCH models in Python? I found the Arch package but that seems to work on only univariate models. I'd like to test out some of the more simple methods described in Bauwends et. al. (2006) like constant conditional correlation.

Python libraries are preferred though I'll play with R as well.

$\endgroup$

4 Answers 4

4
$\begingroup$

PYTHON
I have found this class from the statsmodels library for calculating Garch models.
Unfortunately, I have not seen MGARCH class/library. Below you can see the basic information about the garch models in mentioned class from the statsmodels. Probably you have to implement it by your own in python, so this class might be used as a starting point.

roadmap for garch:
* simple case
* starting values: garch11 explicit formulas
* arma-garch, assumed separable, blockdiagonal Hessian
* other standard garch: egarch, pgarch,
* non-normal distributions
* other methods: forecast, news impact curves (impulse response)

R
In R there is a package called mgarch which is available in this github repository and here you can find some examples.

$\endgroup$
3
$\begingroup$

I recently met the same problem and found a way to achieve it using R in Python.

   from rpy2.robjects import pandas2ri
   import rpy2.robjects as objects 
   import numpy as np

    # pd_rets - a pandas dataframe of daily returns, where the column names are the tickers of stocks and index is the trading days.

    # compute DCC-Garch in R using rmgarch package
    pandas2ri.activate()
    r_rets = pandas2ri.py2ri(pd_rets) # convert the daily returns from pandas dataframe in Python to dataframe in R
    r_dccgarch_code = """
                    library('rmgarch')
                    function(r_rets, n_days){
                            univariate_spec <- ugarchspec(mean.model = list(armaOrder = c(0,0)),
                                                        variance.model = list(garchOrder = c(1,1),
                                                                            variance.targeting = FALSE, 
                                                                            model = "sGARCH"),
                                                        distribution.model = "norm")
                            n <- dim(r_rets)[2]
                            dcc_spec <- dccspec(uspec = multispec(replicate(n, univariate_spec)),
                                                dccOrder = c(1,1),
                                                distribution = "mvnorm")
                            dcc_fit <- dccfit(dcc_spec, data=r_rets)
                            forecasts <- dccforecast(dcc_fit, n.ahead = n_days)
                            list(dcc_fit, forecasts@mforecast$H)
                    }
                    """
    r_dccgarch = robjects.r(r_dccgarch_code)
    r_res = r_dccgarch(r_rets,n_days)
    pandas2ri.deactivate()
    # end of R

    r_dccgarch_model = r_res[0] # model parameters
    r_forecast_cov = r_res[1] # forecasted covariance matrices for n_days

    # access and transform the covariance matrices in R format
    n_cols = pd_rets.shape[1] # get the number of stocks in pd_rets
    n_elements = n_cols*n_cols # the number of elements in each covariance matrix
    n_matrix = int(len(r_forecast_cov[0])/(n_elements))
    print(n_matrix) # this should be equal to n_days

    # sum the daily forecasted covariance matrices 
    cov_matrix = 0
    for i in range(n_matrix):
        i_matrix = np.array([v for v in r_forecast_cov[0][i*n_elements:(i+1)*n_elements]])
        i_matrix = i_matrix.reshape(n_cols,n_cols)    
        cov_matrix += i_matrix
$\endgroup$
1
$\begingroup$

Slight correction: the package in R is called rmgarch, not mgarch. It works well with rugarch, which provides a variety of univariate GARCH models. Both packages allow for parallelized computation on local cluster and return a nice and full set of fitted parameters, model specs, etc. I provided some additional links in this post.

$\endgroup$
1
$\begingroup$

mgarch is a python package for predicting volatility of daily returns in financial markets.

DCC-GARCH(1,1) for multivariate normal and student t. distribution.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.