# -*- coding: utf-8 -*- """ Created on Sun May 09 22:23:22 2010 Author: josef-pktd Licese: BSD """ import numpy as np from numpy.testing import assert_almost_equal from scipy import stats from statsmodels.sandbox.distributions.extras import ( ExpTransf_gen, LogTransf_gen, squarenormalg, absnormalg, negsquarenormalg, squaretg) #define these as module globals l, s = 0.0, 1.0 ppfq = [0.1, 0.5, 0.9] xx = [0.95, 1.0, 1.1] nxx = [-0.95, -1.0, -1.1] def test_loggamma(): #'Results for expgamma' loggammaexpg = LogTransf_gen(stats.gamma) cdftr = loggammaexpg._cdf(1,10) cdfst = stats.loggamma.cdf(1,10) assert_almost_equal(cdfst, cdftr, 14) cdftr = loggammaexpg._cdf(2,15) cdfst = stats.loggamma.cdf(2,15) assert_almost_equal(cdfst, cdftr, 14) def test_loglaplace(): #if x is laplace then y = exp(x) is loglaplace #parameters are tricky #the stats.loglaplace parameter is the inverse scale of x loglaplaceexpg = ExpTransf_gen(stats.laplace) cdfst = stats.loglaplace.cdf(3,3) #0.98148148148148151 #the parameters are shape, loc and scale of underlying laplace cdftr = loglaplaceexpg._cdf(3,0,1./3) assert_almost_equal(cdfst, cdftr, 14) class CheckDistEquivalence(object): #no args, kwds yet def test_cdf(self): #'\nsquare of standard normal random variable is chisquare with dof=1 distributed' cdftr = self.dist.cdf(xx, *self.trargs, **self.trkwds) sfctr = 1-self.dist.sf(xx, *self.trargs, **self.trkwds) #sf complement cdfst = self.statsdist.cdf(xx, *self.stargs, **self.stkwds) assert_almost_equal(cdfst, cdftr, 14) assert_almost_equal(cdfst, sfctr, 14) def test_pdf(self): #'\nsquare of standard normal random variable is chisquare with dof=1 distributed' pdftr = self.dist.pdf(xx, *self.trargs, **self.trkwds) pdfst = self.statsdist.pdf(xx, *self.stargs, **self.stkwds) assert_almost_equal(pdfst, pdftr, 13) def test_ppf(self): #'\nsquare of standard normal random variable is chisquare with dof=1 distributed' ppftr = self.dist.ppf(ppfq, *self.trargs, **self.trkwds) ppfst = self.statsdist.ppf(ppfq, *self.stargs, **self.stkwds) assert_almost_equal(ppfst, ppftr, 13) def test_rvs(self): rvs = self.dist.rvs(*self.trargs, **{'size':100}) mean_s = rvs.mean(0) mean_d, var_d = self.dist.stats(*self.trargs, **{'moments':'mv'}) if np.any(np.abs(mean_d) < 1): assert_almost_equal(mean_d, mean_s, 1) else: assert_almost_equal(mean_s/mean_d, 1., 0) #tests 0.5>> np.random.seed(464239857) >>> rvstsq = squaretg.rvs(10,size=100000) >>> squaretg.moment(4,10) 2734.3750000000009 >>> (rvstsq**4).mean() 2739.672765170933 >>> squaretg.moment(3,10) 78.124999999997044 >>> (rvstsq**3).mean() 84.13950048850549 >>> squaretg.stats(10, moments='mvsk') (array(1.2500000000000022), array(4.6874999999630909), array(5.7735026919777912), array(106.00000000170148)) >>> stats.describe(rvstsq) (100000, (3.2953470738423724e-009, 92.649615690914473), 1.2534924690963247, 4.7741427958594098, 6.1562177957041895, 100.99331166052181) ''' # checking the distribution # fraction of observations in each decile dec = squaretg.ppf(np.linspace(0.,1,11),10) freq,edges = np.histogram(rvstsq, bins=dec) print(freq/float(len(rvstsq))) import matplotlib.pyplot as plt freq,edges,_ = plt.hist(rvstsq, bins=50, range=(0,4),normed=True) edges += (edges[1]-edges[0])/2.0 plt.plot(edges[:-1], squaretg.pdf(edges[:-1], 10), 'r') #plt.show() #plt.close() ''' >>> plt.plot(edges[:-1], squaretg.pdf(edges[:-1], 10), 'r') [] >>> plt.fill(edges[4:8], squaretg.pdf(edges[4:8], 10), 'r') [] >>> plt.show() >>> plt.fill_between(edges[4:8], squaretg.pdf(edges[4:8], 10), y2=0, 'r') SyntaxError: non-keyword arg after keyword arg (, line 1) >>> plt.fill_between(edges[4:8], squaretg.pdf(edges[4:8], 10), 0, 'r') Traceback (most recent call last): AttributeError: 'module' object has no attribute 'fill_between' >>> fig = figure() Traceback (most recent call last): NameError: name 'figure' is not defined >>> ax1 = fig.add_subplot(311) Traceback (most recent call last): NameError: name 'fig' is not defined >>> fig = plt.figure() >>> ax1 = fig.add_subplot(111) >>> ax1.fill_between(edges[4:8], squaretg.pdf(edges[4:8], 10), 0, 'r') Traceback (most recent call last): AttributeError: 'AxesSubplot' object has no attribute 'fill_between' >>> ax1.fill(edges[4:8], squaretg.pdf(edges[4:8], 10), 0, 'r') Traceback (most recent call last): ''' import pytest pytest.main([__file__, '-vvs', '-x', '--pdb'])