Ejemplo n.º 1
0
def examples_normexpand():
    skewnorm = SkewNorm_gen()
    rvs = skewnorm.rvs(5, size=100)
    normexpan = NormExpan_gen(rvs, mode='sample')

    smvsk = stats.describe(rvs)[2:]
    print 'sample: mu,sig,sk,kur'
    print smvsk

    dmvsk = normexpan.stats(moments='mvsk')
    print 'normexpan: mu,sig,sk,kur'
    print dmvsk
    print 'mvsk diff distribution - sample'
    print np.array(dmvsk) - np.array(smvsk)
    print 'normexpan attributes mvsk'
    print mc2mvsk(normexpan.cnt)
    print normexpan.mvsk

    from statsmodels.stats.momenthelpers import mvsk2mnc, mnc2mc
    mnc = mvsk2mnc(dmvsk)
    mc = mnc2mc(mnc)
    print 'central moments'
    print mc
    print 'non-central moments'
    print mnc

    pdffn = pdf_moments(mc)
    print '\npdf approximation from moments'
    print 'pdf at', mc[0] - 1, mc[0] + 1
    print pdffn([mc[0] - 1, mc[0] + 1])
    print normexpan.pdf([mc[0] - 1, mc[0] + 1])
Ejemplo n.º 2
0
def examples_normexpand():
    skewnorm = SkewNorm_gen()
    rvs = skewnorm.rvs(5,size=100)
    normexpan = NormExpan_gen(rvs, mode='sample')

    smvsk = stats.describe(rvs)[2:]
    print 'sample: mu,sig,sk,kur'
    print smvsk

    dmvsk = normexpan.stats(moments='mvsk')
    print 'normexpan: mu,sig,sk,kur'
    print dmvsk
    print 'mvsk diff distribution - sample'
    print np.array(dmvsk) - np.array(smvsk)
    print 'normexpan attributes mvsk'
    print mc2mvsk(normexpan.cnt)
    print normexpan.mvsk

    from statsmodels.stats.momenthelpers import mvsk2mnc, mnc2mc
    mnc = mvsk2mnc(dmvsk)
    mc = mnc2mc(mnc)
    print 'central moments'
    print mc
    print 'non-central moments'
    print mnc


    pdffn = pdf_moments(mc)
    print '\npdf approximation from moments'
    print 'pdf at', mc[0]-1,mc[0]+1
    print pdffn([mc[0]-1,mc[0]+1])
    print normexpan.pdf([mc[0]-1,mc[0]+1])
Ejemplo n.º 3
0
def examples_normexpand():
    skewnorm = SkewNorm_gen()
    rvs = skewnorm.rvs(5, size=100)
    normexpan = NormExpan_gen(rvs, mode='sample')

    smvsk = stats.describe(rvs)[2:]
    print('sample: mu,sig,sk,kur')
    print(smvsk)

    dmvsk = normexpan.stats(moments='mvsk')
    print('normexpan: mu,sig,sk,kur')
    print(dmvsk)
    print('mvsk diff distribution - sample')
    print(np.array(dmvsk) - np.array(smvsk))
    print('normexpan attributes mvsk')
    print(mc2mvsk(normexpan.cnt))
    print(normexpan.mvsk)

    mnc = mvsk2mnc(dmvsk)
    mc = mnc2mc(mnc)
    print('central moments')
    print(mc)
    print('non-central moments')
    print(mnc)

    pdffn = pdf_moments(mc)
    print('\npdf approximation from moments')
    print('pdf at', mc[0] - 1, mc[0] + 1)
    print(pdffn([mc[0] - 1, mc[0] + 1]))
    print(normexpan.pdf([mc[0] - 1, mc[0] + 1]))
Ejemplo n.º 4
0
def test_moment_conversion(mom):
    # this was initially written for an old version of moment_helpers
    # I'm not sure whether there are not redundant cases after moving functions

    # test moment -> cumulant
    assert_equal(mnc2cum(mc2mnc(mom[0])), mom[1])
    assert_equal(mnc2cum(mom[0]), mom[2])
    if len(mom) <= 4:
        assert_equal(mc2cum(mom[0]), mom[1])

    # test   cumulant -> moment
    assert_equal(cum2mc(mom[1]), mom[0])
    assert_equal(mc2mnc(cum2mc(mom[2])), mom[0])
    if len(mom) <= 4:
        assert_equal(cum2mc(mom[1]), mom[0])

    # round trip: mnc -> cum -> mc == mnc -> mc,
    assert_equal(cum2mc(mnc2cum(mom[0])), mnc2mc(mom[0]))

    # round trip: mc -> mnc -> mc ==  mc,
    assert_equal(mc2mnc(mnc2mc(mom[0])), mom[0])

    if len(mom[0]) == 4:
        # round trip: mc -> mvsk -> mc ==  mc
        assert_equal(mvsk2mc(mc2mvsk(mom[0])), mom[0])
Ejemplo n.º 5
0
def examples_normexpand():
    skewnorm = SkewNorm_gen()
    rvs = skewnorm.rvs(5,size=100)
    normexpan = NormExpan_gen(rvs, mode='sample')

    smvsk = stats.describe(rvs)[2:]
    print('sample: mu,sig,sk,kur')
    print(smvsk)

    dmvsk = normexpan.stats(moments='mvsk')
    print('normexpan: mu,sig,sk,kur')
    print(dmvsk)
    print('mvsk diff distribution - sample')
    print(np.array(dmvsk) - np.array(smvsk))
    print('normexpan attributes mvsk')
    print(mc2mvsk(normexpan.cnt))
    print(normexpan.mvsk)

    mnc = mvsk2mnc(dmvsk)
    mc = mnc2mc(mnc)
    print('central moments')
    print(mc)
    print('non-central moments')
    print(mnc)


    pdffn = pdf_moments(mc)
    print('\npdf approximation from moments')
    print('pdf at', mc[0]-1,mc[0]+1)
    print(pdffn([mc[0]-1,mc[0]+1]))
    print(normexpan.pdf([mc[0]-1,mc[0]+1]))
Ejemplo n.º 6
0
def test_moment_conversion():
    #this was initially written for an old version of moment_helpers
    #I'm not sure whether there are not redundant cases after moving functions
    ms  = [( [0.0, 1, 0, 3], [0.0, 1.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0] ),
           ( [1.0, 1, 0, 3], [1.0, 1.0, 0.0, 0.0], [1.0, 0.0, -1.0, 6.0] ),
           ( [0.0, 1, 1, 3], [0.0, 1.0, 1.0, 0.0], [0.0, 1.0, 1.0, 0.0] ),
           ( [1.0, 1, 1, 3], [1.0, 1.0, 1.0, 0.0], [1.0, 0.0, 0.0, 2.0] ),
           ( [1.0, 1, 1, 4], [1.0, 1.0, 1.0, 1.0], [1.0, 0.0, 0.0, 3.0] ),
           ( [1.0, 2, 0, 3], [1.0, 2.0, 0.0, -9.0], [1.0, 1.0, -4.0, 9.0] ),
           ( [0.0, 2, 1, 3], [0.0, 2.0, 1.0, -9.0], [0.0, 2.0, 1.0, -9.0] ),
           ( [1.0, 0.5, 0, 3], [1.0, 0.5, 0.0, 2.25], [1.0, -0.5, 0.5, 2.25] ), #neg.variance if mnc2<mnc1
           ( [0.0, 0.5, 1, 3], [0.0, 0.5, 1.0, 2.25], [0.0, 0.5, 1.0, 2.25] ),
           ( [0.0, 1, 0, 3, 0], [0.0, 1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0, 0.0] ),
           ( [1.0, 1, 0, 3, 1], [1.0, 1.0, 0.0, 0.0, 1.0], [1.0, 0.0, -1.0, 6.0, -20.0] )]

    for mom in ms:
        # test moment -> cumulant
        assert_equal(mnc2cum(mc2mnc(mom[0])),mom[1])
        assert_equal(mnc2cum(mom[0]),mom[2])
        if len(mom) <= 4:
            assert_equal(mc2cum(mom[0]),mom[1])

    for mom in ms:
        # test   cumulant -> moment
        assert_equal(cum2mc(mom[1]),mom[0])
        assert_equal(mc2mnc(cum2mc(mom[2])),mom[0])
        if len(mom) <= 4:
            assert_equal(cum2mc(mom[1]),mom[0])
            
    for mom in ms:
        #round trip: mnc -> cum -> mc == mnc -> mc, 
        assert_equal(cum2mc(mnc2cum(mom[0])),mnc2mc(mom[0]))
        

    for mom in ms:
        #round trip: mc -> mnc -> mc ==  mc, 
        assert_equal(mc2mnc(mnc2mc(mom[0])), mom[0])
        
    for mom in (m for m in ms if len(m) == 4):
        #round trip: mc -> mvsk -> mc ==  mc
        assert_equal(mvsk2mc(mc2mvsk(mom[0])), mom[0])
        #round trip: mc -> mvsk -> mnc ==  mc -> mnc
        assert_equal(mvsk2mnc(mc2mvsk(mom[0])), mc2mnc(mom[0])) 
Ejemplo n.º 7
0
    def __init__(self, args, **kwds):
        # todo: replace with super call
        distributions.rv_continuous.__init__(
            self,
            name="Normal Expansion distribution",
            shapes=" ",
            extradoc="""
        The distribution is defined as the Gram-Charlier expansion of
        the normal distribution using the first four moments. The pdf
        is given by

        pdf(x) = (1+ skew/6.0 * H(xc,3) + kurt/24.0 * H(xc,4))*normpdf(xc)

        where xc = (x-mu)/sig is the standardized value of the random variable
        and H(xc,3) and H(xc,4) are Hermite polynomials

        Note: This distribution has to be parameterized during
        initialization and instantiation, and does not have a shape
        parameter after instantiation (similar to frozen distribution
        except for location and scale.) Location and scale can be used
        as with other distributions, however note, that they are relative
        to the initialized distribution.
        """,
        )
        # print args, kwds
        mode = kwds.get("mode", "sample")

        if mode == "sample":
            mu, sig, sk, kur = stats.describe(args)[2:]
            self.mvsk = (mu, sig, sk, kur)
            cnt = mvsk2mc((mu, sig, sk, kur))
        elif mode == "mvsk":
            cnt = mvsk2mc(args)
            self.mvsk = args
        elif mode == "centmom":
            cnt = args
            self.mvsk = mc2mvsk(cnt)
        else:
            raise ValueError("mode must be 'mvsk' or centmom")

        self.cnt = cnt
        # self.mvsk = (mu,sig,sk,kur)
        # self._pdf = pdf_moments(cnt)
        self._pdf = pdf_mvsk(self.mvsk)
Ejemplo n.º 8
0
    def __init__(self, args, **kwds):
        #todo: replace with super call
        distributions.rv_continuous.__init__(
            self,
            name='Normal Expansion distribution',
            shapes=' ',
            extradoc='''
        The distribution is defined as the Gram-Charlier expansion of
        the normal distribution using the first four moments. The pdf
        is given by

        pdf(x) = (1+ skew/6.0 * H(xc,3) + kurt/24.0 * H(xc,4))*normpdf(xc)

        where xc = (x-mu)/sig is the standardized value of the random variable
        and H(xc,3) and H(xc,4) are Hermite polynomials

        Note: This distribution has to be parameterized during
        initialization and instantiation, and does not have a shape
        parameter after instantiation (similar to frozen distribution
        except for location and scale.) Location and scale can be used
        as with other distributions, however note, that they are relative
        to the initialized distribution.
        ''')
        #print args, kwds
        mode = kwds.get('mode', 'sample')

        if mode == 'sample':
            mu, sig, sk, kur = stats.describe(args)[2:]
            self.mvsk = (mu, sig, sk, kur)
            cnt = mvsk2mc((mu, sig, sk, kur))
        elif mode == 'mvsk':
            cnt = mvsk2mc(args)
            self.mvsk = args
        elif mode == 'centmom':
            cnt = args
            self.mvsk = mc2mvsk(cnt)
        else:
            raise ValueError("mode must be 'mvsk' or centmom")

        self.cnt = cnt
        #self.mvsk = (mu,sig,sk,kur)
        #self._pdf = pdf_moments(cnt)
        self._pdf = pdf_mvsk(self.mvsk)
Ejemplo n.º 9
0
def test_multidimensional(test_vals):
    assert_almost_equal(cum2mc(mnc2cum(mc2mnc(test_vals).T).T).T, test_vals)
    assert_almost_equal(cum2mc(mc2cum(test_vals).T).T, test_vals)
    assert_almost_equal(mvsk2mc(mc2mvsk(test_vals).T).T, test_vals)