Exemple #1
0
    def evaluate(self, x, h="silverman"):
        density = self.kernel.density
        return np.array([density(xx) for xx in x])


if __name__ == "__main__":
    PLOT = True
    from numpy import random
    import matplotlib.pyplot as plt
    import bandwidths as bw

    # 1 D case
    random.seed(142)
    x = random.standard_t(4.2, size=50)
    h = bw.bw_silverman(x)
    #NOTE: try to do it with convolution
    support = np.linspace(-10, 10, 512)

    kern = kernels.Gaussian(h=h)
    kde = KDE(x, kern)
    print kde.density(1.015469)
    print 0.2034675
    Xs = np.arange(-10, 10, 0.1)

    if PLOT:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(Xs, kde(Xs), "-")
        ax.set_ylim(-10, 10)
        ax.set_ylim(0, 0.4)
Exemple #2
0
    #NOTE: I (SS) made some changes to the Fortran
    # and the FFT stuff from Munro http://lib.stat.cmu.edu/apstat/97o
    # then compile everything and link to denest with f2py
    #Make pyf file as usual, then compile shared object
    #f2py denest.f -m denest2 -h denest.pyf
    #edit pyf
    #-c flag makes it available to other programs, fPIC builds a shared library
    #/usr/bin/gfortran -Wall -c -fPIC fft.f
    #f2py -c denest.pyf ./fft.o denest.f

    try:
        from denest2 import denest
        a = -3.4884382032045504
        b = 4.3671504686785605
        RANGE = b - a
        bw = bandwidths.bw_silverman(xi)

        ft, smooth, ifault, weights, smooth1 = denest(xi, a, b, bw,
                                                      np.zeros(512),
                                                      np.zeros(512), 0,
                                                      np.zeros(512),
                                                      np.zeros(512))
        # We use a different binning algo, so only accurate up to 3 decimal places
        np.testing.assert_almost_equal(f2, smooth, 3)
#NOTE: for debugging
#        y2 = forrt(weights)
#        RJ = np.arange(512/2+1)
#        FAC1 = 2*(np.pi*bw/RANGE)**2
#        RJFAC = RJ**2*FAC1
#        BC = 1 - RJFAC/(6*(bw/((b-a)/M))**2)
#        FAC = np.exp(-RJFAC)/BC
Exemple #3
0
#NOTE: I (SS) made some changes to the Fortran
# and the FFT stuff from Munro http://lib.stat.cmu.edu/apstat/97o
# then compile everything and link to denest with f2py
#Make pyf file as usual, then compile shared object
#f2py denest.f -m denest2 -h denest.pyf
#edit pyf
#-c flag makes it available to other programs, fPIC builds a shared library
#/usr/bin/gfortran -Wall -c -fPIC fft.f
#f2py -c denest.pyf ./fft.o denest.f

    try:
        from denest2 import denest
        a = -3.4884382032045504
        b = 4.3671504686785605
        RANGE = b - a
        bw = bandwidths.bw_silverman(xi)

        ft,smooth,ifault,weights,smooth1 = denest(xi,a,b,bw,np.zeros(512),np.zeros(512),0,
                np.zeros(512), np.zeros(512))
# We use a different binning algo, so only accurate up to 3 decimal places
        np.testing.assert_almost_equal(f2, smooth, 3)
#NOTE: for debugging
#        y2 = forrt(weights)
#        RJ = np.arange(512/2+1)
#        FAC1 = 2*(np.pi*bw/RANGE)**2
#        RJFAC = RJ**2*FAC1
#        BC = 1 - RJFAC/(6*(bw/((b-a)/M))**2)
#        FAC = np.exp(-RJFAC)/BC
#        SMOOTH = np.r_[FAC,FAC[1:-1]] * y2

#        dens = revrt(SMOOTH)
        return np.array([self.density(xx) for xx in x])

    def evaluate(self, x, h = "silverman"):
        density = self.kernel.density
        return np.array([density(xx) for xx in x])

if __name__ == "__main__":
    PLOT = True
    from numpy import random
    import matplotlib.pyplot as plt
    import bandwidths as bw

    # 1 D case
    random.seed(142)
    x = random.standard_t(4.2, size = 50)
    h = bw.bw_silverman(x)
    #NOTE: try to do it with convolution
    support = np.linspace(-10,10,512)


    kern = kernels.Gaussian(h = h)
    kde = KDE( x, kern)
    print kde.density(1.015469)
    print 0.2034675
    Xs = np.arange(-10,10,0.1)

    if PLOT:
        fig = plt.figure()
        ax = fig.add_subplot(111)
        ax.plot(Xs, kde(Xs), "-")
        ax.set_ylim(-10, 10)