Ejemplo n.º 1
0
def paduavals2coefs(f):
    m = len(f)
    n = int(round(-1.5 + np.sqrt(.25 + 2 * m)))
    x = padua_points(n)
    idx = _find_m(n)
    w = 0 * x[0] + 1. / (n * (n + 1))
    idx1 = np.all(np.abs(x) == 1, axis=0)
    w[idx1] = .5 * w[idx1]
    idx2 = np.all(np.abs(x) != 1, axis=0)
    w[idx2] = 2 * w[idx2]

    G = np.zeros(idx.max() + 1)
    G[idx] = 4 * w * f

    use_dct = 100 < n
    if use_dct:
        C = np.rot90(dct(dct(G.T).T))  # , axis=1)
    else:
        t1 = np.r_[0:n + 1].reshape(-1, 1)
        Tn1 = np.cos(t1 * t1.T * np.pi / n)
        t2 = np.r_[0:n + 2].reshape(-1, 1)
        Tn2 = np.cos(t2 * t2.T * np.pi / (n + 1))
        C = np.dot(Tn2, np.dot(G, Tn1))

    C[0] = .5 * C[0]
    C[:, 1] = .5 * C[:, 1]
    C[0, -1] = .5 * C[0, -1]
    del C[-1]

    # Take upper-left triangular part:
    return np.fliplr(np.triu(np.fliplr(C)))
Ejemplo n.º 2
0
def paduavals2coefs(f):
    useFFTwhenNisMoreThan = 100
    m = len(f)
    n = int(round(-1.5 + np.sqrt(.25 + 2 * m)))
    x = padua_points(n)
    idx = _find_m(n)
    w = 0 * x[0] + 1. / (n * (n + 1))
    idx1 = np.all(np.abs(x) == 1, axis=0)
    w[idx1] = .5 * w[idx1]
    idx2 = np.all(np.abs(x) != 1, axis=0)
    w[idx2] = 2 * w[idx2]

    G = np.zeros(idx.max() + 1)
    G[idx] = 4 * w * f

    if (n < useFFTwhenNisMoreThan):
        t1 = np.r_[0:n + 1].reshape(-1, 1)
        Tn1 = np.cos(t1 * t1.T * np.pi / n)
        t2 = np.r_[0:n + 2].reshape(-1, 1)
        Tn2 = np.cos(t2 * t2.T * np.pi / (n + 1))
        C = np.dot(Tn2, np.dot(G, Tn1))
    else:

        # dct = @(c) chebtech2.coeffs2vals(c);
        C = np.rot90(dct(dct(G.T).T)) #, axis=1)

    C[0] = .5 * C[0]
    C[:, 1] = .5 * C[:, 1]
    C[0, -1] = .5 * C[0, -1]
    del C[-1]

    # Take upper-left triangular part:
    return np.fliplr(np.triu(np.fliplr(C)))
Ejemplo n.º 3
0
    def test_dct_and_dctn(self):
        a = np.arange(12).reshape((3, -1))

        y = wd.dct(a)
        x = wd.idct(y)
        assert_array_almost_equal(x, a)

        yn = wd.dctn(a)  # , shape=(10,), axes=(1,))
        xn = wd.idctn(yn)  # , axes=(1,))

        assert_array_almost_equal(xn, a)
Ejemplo n.º 4
0
    def hisj(self, data, inc=512, L=7):
        '''
        HISJ Improved Sheather-Jones estimate of smoothing parameter.

        Unlike many other implementations, this one is immune to problems
        caused by multimodal densities with widely separated modes. The
        estimation does not deteriorate for multimodal densities, because
        it do not assume a parametric model for the data.

        Parameters
        ----------
        data - a vector of data from which the density estimate is constructed
        inc  - the number of mesh points used in the uniform discretization

        Returns
        -------
        bandwidth - the optimal bandwidth

        References
        ----------
        Z. I. Botev, J. F. Grotowski, and D. P. Kroese (2010)
        "Kernel density estimation via diffusion"
        Annals of Statistics, Volume 38, Number 5, pages 2916-2957.
        '''
        A = np.atleast_2d(data)
        d, n = A.shape
        ste_constant = self.kernel.get_ste_constant(n)
        ax1, bx1 = self._get_grid_limits(A)

        ste_constant2 = _GAUSS_KERNEL.get_ste_constant(n)

        def fixed_point(t, N, I, a2):
            ''' this implements the function t-zeta*gamma^[L](t)'''

            prod = np.prod
            # L = 7
            logI = np.log(I)

            def fun(s, time):
                return (2 * pi ** (2 * s) *
                        (a2 * exp(s * logI - I * pi ** 2 * time)).sum())
            f = fun(L, t)
            for s in range(L - 1, 1, -1):
                K0 = prod(np.r_[1:2 * s:2]) / sqrt(2 * pi)
                const = (1 + (1. / 2) ** (s + 1. / 2)) / 3
                time = (2 * const * K0 / N / f) ** (2. / (3 + 2 * s))
                f = fun(s, time)
            return t - (2 * N * sqrt(pi) * f) ** (-2. / 5)

        h = np.empty(d)
        for dim in range(d):
            ax, bx = ax1[dim], bx1[dim]
            xa = np.linspace(ax, bx, inc)
            R = bx - ax

            c = gridcount(A[dim], xa)
            N = len(set(A[dim]))
            a = dct(c / len(A[dim]), norm=None)

            # now compute the optimal bandwidth^2 using the referenced method
            I = np.asfarray(np.arange(1, inc)) ** 2
            a2 = (a[1:] / 2) ** 2

            x = np.linspace(0, 0.1, 150)
            ai = x[0]
            bi = x[1]
            f0 = fixed_point(ai, N, I, a2)
            for xi in x[1:]:
                bi = xi
                f1 = fixed_point(bi, N, I, a2)
                if f1 * f0 <= 0:
                    # print('ai = %g, bi = %g' % (ai,bi))
                    break
                else:
                    ai = xi

            # use  fzero to solve the equation t=zeta*gamma^[5](t)
            try:
                t_star = optimize.brentq(lambda t: fixed_point(t, N, I, a2),
                                         a=ai, b=bi)
            except Exception as err:
                t_star = 0.28 * N ** (-2. / 5)
                warnings.warn('Failure in obtaining smoothing parameter'
                              ' ({})'.format(str(err)))

            # smooth the discrete cosine transform of initial data using t_star
            # a_t = a*exp(-np.arange(inc)**2*pi**2*t_star/2)
            # now apply the inverse discrete cosine transform
            # density = idct(a_t)/R;

            # take the rescaling of the data into account
            bandwidth = sqrt(t_star) * R

            # Kernel other than Gaussian scale bandwidth
            h[dim] = bandwidth * (ste_constant / ste_constant2) ** (1.0 / 5)
        # end  for dim loop
        return h