Login
Login

Re: CointegrationMLE

Home 21090308 Forums Re: CointegrationMLE

#2031
bayu
Member

Hi Haksun,

I tried your code which you attached, and it works fine. However, when I tried to do the same on a pair, of which one is just a lag of the other, it failed with the error message “U is not invertible”. I would have expected to get coeff of 1 and -1.

I’ve attached the code below for you.

/*
* Copyright (c) Numerical Method Inc.
* http://www.numericalmethod.com/
*
* THIS SOFTWARE IS LICENSED, NOT SOLD.
*
* YOU MAY USE THIS SOFTWARE ONLY AS DESCRIBED IN THE LICENSE.
* IF YOU ARE NOT AWARE OF AND/OR DO NOT AGREE TO THE TERMS OF THE LICENSE,
* DO NOT USE THIS SOFTWARE.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITH NO WARRANTY WHATSOEVER,
* EITHER EXPRESS OR IMPLIED, INCLUDING, WITHOUT LIMITATION,
* ANY WARRANTIES OF ACCURACY, ACCESSIBILITY, COMPLETENESS,
* FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABILITY, NON-INFRINGEMENT,
* TITLE AND USEFULNESS.
*
* IN NO EVENT AND UNDER NO LEGAL THEORY,
* WHETHER IN ACTION, CONTRACT, NEGLIGENCE, TORT, OR OTHERWISE,
* SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
* ANY CLAIMS, DAMAGES OR OTHER LIABILITIES,
* ARISING AS A RESULT OF USING OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.numericalmethod.suanshu.examples;

import com.numericalmethod.suanshu.stats.cointegration.CointegrationMle;
import com.numericalmethod.suanshu.stats.timeseries.multivariate.realtime.SimpleMultiVariateTimeSeries;
import org.junit.Test;

/**
*
* @author Haksun Li
*/
public class TestingCoint {

    public static void main(String args[]) {
        System.out.println(“This class demonstrates how to compute the cointegrating factors (beta) and adjustment factors (alpha) for a system of multivariate time series.”);
        org.junit.runner.JUnitCore.main(“com.numericalmethod.suanshu.examples.TestingCoint”);
    }

    /**
    * Compute the cointegration of a system of multivariate time series.
    *
    * We use the example from
    * Søren Johansen. Likelihood-Based Inference in Cointegrated Vector Autoregressive Models. Oxford University Press, USA. February 1, 1996.
    *
    * This values are obtained from R output of the following script:
    *
    library(urca)
    data(finland)
    sjf <- finland
    summary(ca.jo(sjf, ecdet = “none”, type=”eigen”, K=2))
    */
    @Test
    public void cointegration_0010() {
        double[][] finland = new double[][]{
{104.32,104.64},
{104.64,104.75},
{104.75,105.17},
{105.17,105.48},
{105.48,105.66},
{105.66,104.67},
{104.67,105.55},
{105.55,105.81},
{105.81,104.67},
{104.67,105.97},
{105.97,104.86},
{104.86,102.88},
{102.88,100.6},
{100.6,101.06},
{101.06,100.64},
{100.64,101.14},
{101.14,99.95},
{99.95,98.97},
{98.97,100.38},
{100.38,101.68},
{101.68,101.16},
{101.16,98.01},
{98.01,98.29},
{98.29,97.45},
{97.45,98.73},
{98.73,98.53},
{98.53,99.5},
{99.5,99.25},
{99.25,101.05},
{101.05,101.5},
{101.5,102.17},
{102.17,102.41},
{102.41,102.3},
{102.3,101.07},
{101.07,102.07},
{102.07,101.88},
{101.88,102.03},
{102.03,103.06},
{103.06,103.3},
{103.3,103.36},
{103.36,103.75},
{103.75,105.21},
{105.21,105.21},
{105.21,105.39},
{105.39,105.88},
{105.88,106.33},
{106.33,106.31},
{106.31,106.36},
{106.36,107.2},
{107.2,107.83},
{107.83,107.79},
{107.79,107.24},
{107.24,107.79},
{107.79,108.58},
{108.58,107.98},
{107.98,107.8},
{107.8,107.88},
{107.88,108.07},
{108.07,108.09},
{108.09,107.73},
{107.73,108.54},
{108.54,109.4},
{109.4,109.59},
{109.59,108.99},
{108.99,109.36},
{109.36,110.09},
{110.09,110.29},
{110.29,110.37},
{110.37,111.6},
{111.6,111.7},
{111.7,109.9},
{109.9,110.39},
{110.39,111.28},
{111.28,111.17},
{111.17,111.43},
{111.43,112.22},
{112.22,111.74},
{111.74,109.12},
{109.12,109.85},
{109.85,111.28},
{111.28,109.43},
{109.43,110.86},
{110.86,108.22},
{108.22,107.54},
{107.54,104.08},
{104.08,102.48},
{102.48,106.99},
{106.99,106.65},
{106.65,108.15},
{108.15,106.85},
{106.85,104.84},
{104.84,104.97},
{104.97,103.5},
{103.5,102.98},
{102.98,98.97},
{98.97,100.46},
{100.46,99.16},
{99.16,99.2},
{99.2,98.65},
{98.65,101.93},
{101.93,100.68},
{100.68,98.95},
{98.95,101.52},
{101.52,101.94},
{101.94,98.43},
{98.43,97.11},
{97.11,98.19},
{98.19,97.61},
{97.61,100.49},
{100.49,100.96},
{100.96,100.78},
{100.78,103.15},
{103.15,103.1},
{103.1,103.24},
{103.24,103.38},
{103.38,102.98},
{102.98,101.34},
{101.34,101.04},
{101.04,99.34},
{99.34,99.62},
{99.62,98.93},
{98.93,95.87},
{95.87,94.91},
{94.91,94.61},
{94.61,94.17},
{94.17,94.67},
{94.67,97.69},
{97.69,98.61},
{98.61,99.32},
{99.32,99.39},
{99.39,100.93},
{100.93,100.91},
{100.91,101.03},
{101.03,98.13},
{98.13,98.71},
{98.71,99.84},
{99.84,98.57},
{98.57,100.8},
{100.8,101.63},
{101.63,102.76},
{102.76,102.66},
{102.66,101.95},
{101.95,101.54},
{101.54,101.55},
{101.55,103.79},
{103.79,103.29},
{103.29,103.95},
{103.95,103.82},
{103.82,103.45},
{103.45,104.01},
{104.01,103.4},
{103.4,100.53},
{100.53,100},
{100,99.6},
{99.6,99.62},
{99.62,100.86},
{100.86,101.03},
{101.03,99.32},
{99.32,98.95},
{98.95,98.55},
{98.55,97.13},
{97.13,97.46},
{97.46,96.71},
{96.71,98.34},
{98.34,96.91},
{96.91,96.95},
{96.95,99.82},
{99.82,100.74},
{100.74,102.07},
{102.07,100.91},
{100.91,101.58},
{101.58,102.07},
{102.07,102.57},
{102.57,103.74},
{103.74,103.67},
{103.67,104.04},
{104.04,104.02},
{104.02,104.1},
{104.1,105.69},
{105.69,105.42},
{105.42,104.92},
{104.92,103.52},
{103.52,105.71},
{105.71,105.12},
{105.12,105.64},
{105.64,105.38},
{105.38,105.06},
{105.06,105.52},
{105.52,104.68},
{104.68,106.86},
{106.86,106.85},
{106.85,106.67},
{106.67,107.32},
{107.32,107.34},
{107.34,107.75},
{107.75,108.52},
{108.52,108.13},
{108.13,108.35},
{108.35,109.13},
{109.13,107.4},
{107.4,108.55},
{108.55,108.75},
{108.75,109},
{109,109.24},
{109.24,109.24},
{109.24,108.96},
{108.96,109.09},
{109.09,109.04},
{109.04,109.14},
{109.14,109.99},
{109.99,110.42},
{110.42,112.56},
{112.56,113.01},
{113.01,112.78},
{112.78,111.91},
{111.91,112.42},
{112.42,111.95},
{111.95,110.63},
{110.63,110.5},
{110.5,108.74},
{108.74,108.78},
{108.78,110.45},
{110.45,110.74},
{110.74,110.57},
{110.57,108.99},
{108.99,110.62},
{110.62,109.82},
{109.82,109.7},
{109.7,109.04},
{109.04,111.41},
{111.41,112.84},
{112.84,113.14},
{113.14,113},
{113,113.06},
{113.06,113.51},
{113.51,113.95},
{113.95,114.64},
{114.64,114.66},
{114.66,114.76},
{114.76,114.18},
{114.18,114.89},
{114.89,114.98},
{114.98,115.28},
{115.28,115.99},
{115.99,116.4},
{116.4,115.72},
{115.72,115.79},
{115.79,115.88},
{115.88,116.02},
{116.02,115.84},
{115.84,115.82}

        };

        CointegrationMle instance = new CointegrationMle(new SimpleMultiVariateTimeSeries(finland), true, 2);

        System.out.println(“eigenvalues:”);
        System.out.println(instance.eigenvalues());
        System.out.println(“cointegrating factors”);
        System.out.println(instance.beta());
        System.out.println(“speeds of adjustment”);
        System.out.println(instance.alpha());
        System.out.println(“ratio test statistics”);
        System.out.println(instance.getStats());
    }
}