Esempio n. 1
0
def estimate_bivariate_mle_jr():
    ndim = 2
    size = (10000, ndim)
    data = np.random.normal(size=size)
    eta, lam = 4, -.9
    skst = SkewStudent(eta=eta, lam=lam)
    data = skst.rvs(size=size)

    model = SkStJR(ndim=ndim, data=data)
    out = model.fit_mle()
    print(out)

    model.from_theta(out.x)

    fig, axes = plt.subplots(nrows=size[1], ncols=1)
    for innov, ax in zip(data.T, axes):
        sns.kdeplot(innov, ax=ax, label='data')

    lines = [ax.get_lines()[0].get_xdata() for ax in axes]
    lines = np.vstack(lines).T
    marginals = model.marginals(lines)

    for line, margin, ax in zip(lines.T, marginals.T, axes):
        ax.plot(line, margin, label='fitted')
        ax.legend()

    plt.show()
Esempio n. 2
0
def estimate_bivariate_mle_jr():
    ndim = 2
    size = (10000, ndim)
    data = np.random.normal(size=size)
    eta, lam = 4, -.9
    skst = SkewStudent(eta=eta, lam=lam)
    data = skst.rvs(size=size)

    model = SkStJR(ndim=ndim, data=data)
    out = model.fit_mle()
    print(out)

    model.from_theta(out.x)

    fig, axes = plt.subplots(nrows=size[1], ncols=1)
    for innov, ax in zip(data.T, axes):
        sns.kdeplot(innov, ax=ax, label='data')

    lines = [ax.get_lines()[0].get_xdata() for ax in axes]
    lines = np.vstack(lines).T
    marginals = model.marginals(lines)

    for line, margin, ax in zip(lines.T, marginals.T, axes):
        ax.plot(line, margin, label='fitted')
        ax.legend()

    plt.show()
Esempio n. 3
0
def simulate_bekk(param, nobs=1000, distr='normal', degf=10, lam=0):
    """Simulate data.

    Parameters
    ----------
    param : BEKKParams instance
        Attributes of this class hold parameter matrices
    nobs : int
        Number of observations to generate. Time series length
    distr : str
        Name of the distribution from which to generate innovations.

        Must be
            - 'normal'
            - 'student'
            - 'skewt'
    degf : int
        Degrees of freedom for Student or SkewStudent distributions
    lam : float
        Skewness parameter for Student or SkewStudent distributions.
        Must be between (-1, 1)

    Returns
    -------
    innov : (nobs, nstocks) array
        Multivariate innovation matrix

    """
    nstocks = param.amat.shape[0]
    if distr == 'normal':
        # Normal innovations
        mean, cov = np.zeros(nstocks), np.eye(nstocks)
        error = np.random.multivariate_normal(mean, cov, nobs)
    elif distr == 'student':
        # Student innovations
        error = np.random.standard_t(degf, size=(nobs, nstocks))
    elif distr == 'skewt':
        # Skewed Student innovations
        error = SkewStudent(eta=degf, lam=lam).rvs(size=(nobs, nstocks))
    else:
        raise ValueError('Unknown distribution!')
    # Standardize innovations
    error = (error - error.mean(0)) / error.std(0)
    hvar = np.empty((nobs, nstocks, nstocks))
    innov = np.zeros((nobs, nstocks))

    hvar[0] = param.get_uvar()
    intercept = param.cmat.dot(param.cmat.T)

    for i in range(1, nobs):
        innov2 = innov[i-1, np.newaxis].T * innov[i-1]
        hvar[i] = intercept + param.amat.dot(innov2).dot(param.amat.T) \
            + param.bmat.dot(hvar[i-1]).dot(param.bmat.T)
        hvar12 = sl.cholesky(hvar[i], 1)
        innov[i] = hvar12.dot(np.atleast_2d(error[i]).T).flatten()

    return innov, hvar
Esempio n. 4
0
def estimate_bivariate_mle_bl():
    ndim = 2
    size = (10000, ndim)
    data = np.random.normal(size=size)
    eta, lam = 4, -.9
    skst = SkewStudent(eta=eta, lam=lam)
    data = skst.rvs(size=size)

    model = SkStBL(ndim=ndim, data=data)
    out = model.fit_mle(method='L-BFGS-B')
    print(out)
Esempio n. 5
0
    def test_ppf(self):
        """Test ppf method."""

        skewt = SkewStudent()

        num = 50
        arg = np.linspace(.01, .99, num)
        ppf = skewt.ppf(arg)

        self.assertEqual(ppf.shape[0], num)
        self.assertIsInstance(skewt.ppf(.5), float)
Esempio n. 6
0
def estimate_bivariate_mle_bl():
    ndim = 2
    size = (10000, ndim)
    data = np.random.normal(size=size)
    eta, lam = 4, -.9
    skst = SkewStudent(eta=eta, lam=lam)
    data = skst.rvs(size=size)

    model = SkStBL(ndim=ndim, data=data)
    out = model.fit_mle(method='L-BFGS-B')
    print(out)
Esempio n. 7
0
    def test_ppf(self):
        """Test ppf method."""

        skewt = SkewStudent()

        num = 50
        arg = np.linspace(.01, .99, num)
        ppf = skewt.ppf(arg)

        self.assertEqual(ppf.shape[0], num)
        self.assertIsInstance(skewt.ppf(.5), float)
Esempio n. 8
0
    def test_cdf(self):
        """Test cdf method."""

        skewt = SkewStudent()

        num = 50
        arg = np.linspace(-1, 1, num)
        cdf = skewt.cdf(arg)

        self.assertEqual(cdf.shape[0], num)
        self.assertIsInstance(skewt.cdf(0), float)
Esempio n. 9
0
    def test_cdf(self):
        """Test cdf method."""

        skewt = SkewStudent()

        num = 50
        arg = np.linspace(-1, 1, num)
        cdf = skewt.cdf(arg)

        self.assertEqual(cdf.shape[0], num)
        self.assertIsInstance(skewt.cdf(0), float)
Esempio n. 10
0
    def test_init(self):
        """Test __init__."""

        skewt = SkewStudent()

        self.assertIsInstance(skewt.eta, float)
        self.assertIsInstance(skewt.lam, float)

        eta, lam = 5., -.2
        skewt = SkewStudent(eta=eta, lam=lam)

        self.assertEqual(skewt.eta, eta)
        self.assertEqual(skewt.lam, lam)
Esempio n. 11
0
    def test_compare_with_t(self):
        """Compare with standard t distribution."""

        eta = 5
        skewt = SkewStudent(eta=eta, lam=0)
        scale = 1/(eta/(eta-2))**.5
        standt = t(eta, scale=scale)
        arg = np.linspace(-2, 2, 100)

        np.testing.assert_array_almost_equal(skewt.pdf(arg), standt.pdf(arg))
        np.testing.assert_array_almost_equal(skewt.cdf(arg), standt.cdf(arg))

        arg = np.linspace(.01, .99, 100)

        np.testing.assert_array_almost_equal(skewt.ppf(arg), standt.ppf(arg))
Esempio n. 12
0
    def test_compare_with_t(self):
        """Compare with standard t distribution."""

        eta = 5
        skewt = SkewStudent(eta=eta, lam=0)
        scale = 1 / (eta / (eta - 2))**.5
        standt = t(eta, scale=scale)
        arg = np.linspace(-2, 2, 100)

        np.testing.assert_array_almost_equal(skewt.pdf(arg), standt.pdf(arg))
        np.testing.assert_array_almost_equal(skewt.cdf(arg), standt.cdf(arg))

        arg = np.linspace(.01, .99, 100)

        np.testing.assert_array_almost_equal(skewt.ppf(arg), standt.ppf(arg))
Esempio n. 13
0
    def test_rvs(self):
        """Test ppf method."""

        skewt = SkewStudent()

        rvs = skewt.rvs()

        self.assertIsInstance(rvs, float)

        size = 2
        rvs = skewt.rvs(size=size)

        self.assertIsInstance(rvs, np.ndarray)
        self.assertEqual(rvs.shape, (size, ))

        size = (2, 3)
        rvs = skewt.rvs(size=size)

        self.assertIsInstance(rvs, np.ndarray)
        self.assertEqual(rvs.shape, size)
Esempio n. 14
0
    def test_rvs(self):
        """Test ppf method."""

        skewt = SkewStudent()

        rvs = skewt.rvs()

        self.assertIsInstance(rvs, float)

        size = 2
        rvs = skewt.rvs(size=size)

        self.assertIsInstance(rvs, np.ndarray)
        self.assertEqual(rvs.shape, (size, ))

        size = (2, 3)
        rvs = skewt.rvs(size=size)

        self.assertIsInstance(rvs, np.ndarray)
        self.assertEqual(rvs.shape, size)
Esempio n. 15
0
# -*- coding: utf-8 -*-
"""Examples.

"""
from __future__ import print_function, division

import numpy as np
import matplotlib.pylab as plt
import seaborn as sns
from scipy.optimize import minimize

from skewstudent import SkewStudent


if __name__ == '__main__':

    eta, lam = 5, -.5
    param = [eta, lam]

    sns.set_context('paper')
    skewt = SkewStudent(eta=eta, lam=lam)

    data = skewt.rvs(2000)

    sns.kdeplot(data)
    plt.show()

    bounds = [(2.01, 300), (-1, 1)]
    res = minimize(skewt.loglikelihood, [10, 0], args=(data,), method='SLSQP',
                   bounds=bounds)
    print(res)