Johansen cointegration test in python - python

I can't find any reference on funcionality to perform Johansen cointegration test in any Python module dealing with statistics and time series analysis (pandas and statsmodel). Does anybody know if there's some code around that can perform such a test for cointegration among time series?

This is now implemented in Python's statsmodels:
from statsmodels.tsa.vector_ar.vecm import coint_johansen
x = getx() # dataframe of n series for cointegration analysis
jres = coint_johansen(x, det_order=0, k_ar_diff=1)
For a full description of inputs/results, see the documentation.

statsmodels doesn't have a Johansen cointegration test. And, I have never seen it in any other python package either.
statsmodels has VAR and structural VAR, but no VECM (vector error correction models) yet.
update:
As Wes mentioned, there is now a pull request for Johansen's cointegration test for statsmodels. I have translated the matlab version in LeSage's spatial econometrics toolbox and wrote a set of tests to verify that we get the same results.
It should be available in the next release of statsmodels.
update 2:
The test for cointegration coint_johansen was included in statsmodels 0.9.0 together with the vector error correction models VECM.
(see also 3rd answer)

See http://github.com/statsmodels/statsmodels/pull/453

Check this: https://searchcode.com/codesearch/view/88477497/
It provides a library where you can find the Johansen cointegration test.

Related

Do we have an implementation of Bayesian structural time series in Python?

We are looking for a close pythonian implementation of the r library bsts.
To be precise, I'm looking for something that allows me to emulate the functionality of 'add_regressor' from fbprophet.
Have already tried Pybsts (the kernel kept dying), and
According to a thread on tensorflow_probability Github account, it doesn't support multivariate mode yet.
Any help would be appreciated.
Thanks
This blog post from Tensorflow Probability shows how to add an exogenous regressor with the TFP structural time series tools. In particular, check out the usage of the temperature_effect variable in the Example: Forecasting Demand for Electricity section!
I recently wrote a version of R's bsts package in Python. It doesn't have all of bsts's features, but it does have options for level, trend, seasonality, and regression. The syntax closely follows statsmodels' UnobservedComponents module. You can find the code and description of the package here: https://github.com/devindg/pybuc.

How to perform the Friedman test and post-hoc test in Python?

I know that Python's SciPy library has the function for the Friedman test
But, it is not enough as I need more information for posthoc test.
So, how I do Friedman test and posthoc test (e.g. Nemenyi or Bonferroni-Dunn test) and draw a critical difference diagram entirely on Python?
You can perform the posthoc tests with the scikit-posthocs package or with the STAC library.
I use Nemenyi's test from scikit-posthocs. They also include a recommendation on how to plot the results.

Welch's ANOVA in Python

I'm not sure if stackoverflow is the best forum for this, but anyway...
Scipy implements ANOVA using stats.f_oneway, which assumes equal variances. It says in the docs that if the variances are unequal, one could consider the Kruskal-Wallis test instead.
However, what I want is Welch's ANOVA. Scipy has a Welch t-test, but of course this doesn't work if I have more than two groups.
What I find interesting is that scipy used to have stats.oneway which allowed for an equal variance setting. However, it has been deprecated.
Is there an easy way to implement Welch's ANOVA in Python?
Just required the same thing. I had to copy code from R package. Also requested scipy.stats to add this feature. Here is the ~10 lines of code for the implementation
https://github.com/scipy/scipy/issues/11122
The pingouin package has a Welch's ANOVA implemented. You can find the documentation for it at https://pingouin-stats.org/generated/pingouin.welch_anova.html.

statsmodels heteroscedasticity breusch pagan test doesn't match

When I estimate the breach-pagan test step by step doesn't math with the bp value in statsmodels:
Here is link to the nb in spanish.
I have been tested in Gretl and my 'manual' estimation is correct, but I want to know why is the difference.
Check the Koenker version in Gretl.
I don't find a Gretl reference right now, but according to the unit tests, the version in statsmodels is equal to the Koenker version of the Breusch-Pagan test.
I don't see an option for the original Breusch-Pagan test, but that one is not robust to non-normality (assumption on 4th moment, IIRC).
In general, many of the Lagrange multiplier specification tests have several versions that are asymptotically equivalent but differ in small samples, for example statsmodels reports LM test and the F-test version. Additionally, they differ which extra assumptions are required for their validity.
For example statsmodels does not have yet a heteroscedasticity test that is robust to autocorrelation. There are again several versions and I have not seen them available as standard option in any package yet.

Migrating from Stata to Python

Some coworkers who have been struggling with Stata 11 are asking for my help to try to automate their laborious work. They mainly use 3 commands in Stata:
tsset (sets a time series analysis)
as in: tsset year_column, yearly
varsoc (Obtain lag-order selection statistics for VARs)
as in: varsoc column_a column_b
vec (vector error-correction model)
as in: vec column_a column_b, trend(con) lags(1) noetable
Does anyone know any scientific library that I can use through python for this same functionality?
I believe both scikits.timeseries and econpy / pytrix implement vector autoregression methods, but I haven't put either through their paces.
scikits.timeseries is mainly for data handling and has only some statistical, econometric analysis and no vectorautoregression. pytrix has some econometrics functions but also no VAR.
(At least last time I looked.)
scikits.statsmodels and pandas both have VAR, pandas also does the data handling for time series. I haven't seen any vector error correction models in python yet, but scikits.statsmodels is getting close.
http://groups.google.ca/group/pystatsmodels?hl=en&pli=1
Check out scikits.statsmodels.tsa.api.VAR (may need to get the latest development version--use Google) and, in check out the documentation for it:
http://statsmodels.sourceforge.net/devel/vector_ar.html#var
These models integrate with pandas also. I'll be working in the coming months to improve integration of pandas with the rest of statsmodels
Vector Error Correction Models have not been implemented yet but are on the TODO list!
Use Rpy2 and call the R var package.
I have absolutely no clue what any of those do, but NumPy and SciPy. Maybe Sage or SymPy.

Categories