Example #1
0
        def f(Jm, Jn, r, i, J, M, LRules, rulesX, rulesW, nu, k):
            sum2 = 0
            Jn1 = J[:, r]

            Jm1 = J[:, i]

            prod = 1.0
            for o in range(0, M):
                sumparc = 0

                for l in range(0, LRules):
                    x1 = rulesX[l]
                    factor1 = (1.0 / sqrt(factorial(int(Jn1[o])))) * (
                        1.0 / sqrt(factorial(int(Jm1[o]))))
                    prod1 = special.eval_hermitenorm(int(
                        Jn1[o]), x1) * special.eval_hermitenorm(
                            int(Jm1[o]), x1) * factor1
                    sumparc = sumparc + prod1 * rulesW[l] / (sqrt(2.0 * nu) *
                                                             pi * (o + 1))

                prod = prod * sumparc

            for l in range(0, LRules):
                x2 = rulesX[l]
                factor2 = (1.0 / sqrt(factorial(int(Jn[k]) - 1))) * (
                    1.0 / sqrt(factorial(int(Jm[k]))))
                sum2 = sum2 + special.eval_hermitenorm(
                    int(Jn[k]) - 1, x2) * special.eval_hermitenorm(
                        int(Jm[k]), x2) * rulesW[l] * factor2
            return sum2 * prod
Example #2
0
    def integr1(self):
        """
        Function to calculate first type of integrals.

        Parameters
        ----------
        J : array; shape([M, M])
            Array containing the grades of the polynomials
        M : int; max order polynomials
        rulesX : array; polynomials of Hermite evaluated
        rulesW : array; weights of polynomials of Hermite evaluated
        LRules : array; length of rulesX array
        nu : float; Diffusion coefficient
        u0 : callable(x); initial condition function

        Returns
        -------
        intg : float, value of first type of integral

        """

        intg = 0
        for k in range(0, self.LRules):
            prod = 1
            for i in range(0, self.M):
                if self.J[i, 0] > 0:
                    prod = prod * special.eval_hermitenorm(
                        int(self.J[i, 0]), self.rulesX[k])
            intg = intg + prod * self.rulesW[k]

        return intg
Example #3
0
    def calc_a(self, multindices):
        """

        This method builds the matrix containing the basis functions evaluated
        at sample locations, i.e. the matrix A in Au = b

        Parameters
        ----------
        multindices : list
            the list with the multi-indices

        Returns
        -------
        A : array
            the matrix with the evaluated basis functions for the samples

        """

        dimension = self.my_experiment.dimension
        x_u_scaled = self.my_experiment.x_u_scaled
        a_matrix = np.ones([self.my_experiment.size, len(multindices)])

        # generate the A matrix
        for i, multiindex in enumerate(multindices):
            for j in range(dimension):
                deg = multiindex[j]
                if self.my_experiment.polytypes[j] == 'Legendre':
                    a_matrix[:, i] *= special.eval_legendre(
                        deg, x_u_scaled[:, j])

                elif self.my_experiment.polytypes[j] == 'Hermite':
                    a_matrix[:, i] *= special.eval_hermitenorm(
                        deg, x_u_scaled[:, j])

        return a_matrix
Example #4
0
def hermite_function(indices, x, t=-1):
    output = 1
    unique_indices = set(indices)
    for i in unique_indices:
        order = indices.count(i)
        output *= eval_hermitenorm(order, x[t - i])
    return output
Example #5
0
 def integ(xSpace, M, J):
     Px = len(xSpace)
     H = np.zeros([M, Px])
     for k in range(0, Px):
         for j in range(1, M):
             prod = 1.0
             for i in range(0, M):
                 if J[i, j] > 0:
                     x1 = integrate.quad(f, 0, 1, args=[k])[0]
                     prod = prod * special.eval_hermitenorm(
                         int(J[i, j]),
                         x1) * (1.0 / sqrt(factorial(J[i, j])))
             H[j, k] = prod
         x2 = xSpace[k]
         H[0, k] = special.eval_hermitenorm(
             2, x2) - special.eval_hermitenorm(1, x2) + 1
     return H
Example #6
0
 def grad(self, x, order):
     """ Return the first derivate of hermite polynomial evaluated at x
     Args:
       x (double) - evaulation point
       order (int) - order of polynomial
     Returns:
       H_x (double)
     """
     return order * eval_hermitenorm(order - 1, x)
Example #7
0
 def fun(self, x, order):
     """ Return the hermite polynomial evaluated at x
     Args:
       x (double) - evaulation point
       order (int) - order of polynomial
     Returns:
       H_x (double)
     """
     return eval_hermitenorm(order, x)
def test_reduction_to_probabilist_polys():
    """Tests that the multidimensional hermite polynomials reduce to the regular probabilist' hermite polynomials in the appropriate limit"""
    x = np.arange(-1, 1, 0.1)
    init = 1
    n_max = 5
    A = np.ones([init, init], dtype=complex)
    vals = np.array(
        [hermite_multidimensional(A, n_max, y=np.array([x0], dtype=complex)) for x0 in x]
    ).T
    expected = np.array([eval_hermitenorm(i, x) for i in range(len(vals))])
    assert np.allclose(vals, expected)
Example #9
0
    def SimulaX(self):
        """
        Function to simulate X.

        Parameters
        ----------
        J : array; shape([M, M])
            Array containing the grades of the polynomials
        M : int; number of ODEs
        xSpace : array; discretized real space
        nu : array; Diffusion coefficient
        u0 : callable(x); initial condition function

        Returns
        -------
        H : array, shape(M, len(xSpace))
            Array containing hermite polynomials evaluated to simulate real space

        """
        def evalXSin(x, k):

            return sqrt(2.0 * self.nu) * k * pi * sqrt(
                2.0 / pi) * (self.u0(x)) * (sin(k * pi * x))

        Px = len(self.x)
        H = np.zeros([self.M, Px])
        for k in range(0, Px):
            for j in range(1, self.M):
                prod = 1.0
                for i in range(0, self.M):
                    if self.J[i, j] > 0:
                        x1 = integrate.quad(evalXSin, 0, 1, args=k)[0]
                        prod = prod * special.eval_hermitenorm(
                            int(self.J[i, j]),
                            x1) * (1.0 / sqrt(factorial(self.J[i, j])))
                H[j, k] = prod
            x2 = self.x[k]
            H[0,
              k] = special.eval_hermitenorm(2, x2) - special.eval_hermitenorm(
                  1, x2) + 1
        return H
Example #10
0
    def evaluate(self, x):
        x = np.array(x).flatten()

        # normalize data
        x_normed = Polynomials.standardize_normal(
            x,
            mean=self.distributions.parameters['loc'],
            std=self.distributions.parameters['scale'])

        # evaluate standard Hermite polynomial, orthogonal w.r.t. the PDF of N(0,1)
        h = eval_hermitenorm(self.degree, x_normed)

        # normalization constant
        st_herm_norm = np.sqrt(math.factorial(self.degree))

        h = h / st_herm_norm

        return h
Example #11
0
            def constants(xSpace, M, J, rulesX, rulesW, nu):
                Lx = len(xSpace)
                ci = np.zeros([M, Lx])

                for z in range(0, Lx):
                    for i in range(0, M):
                        sum2 = 0
                        for k in range(0, M):
                            sum1 = 0
                            if J[k, i] > 0:
                                for y in range(0, self.LRules):
                                    sum1 = sum1 + special.eval_hermitenorm(
                                        int(J[k, i]),
                                        rulesX[y]) * rulesX[y] * rulesW[y]
                            sum2 = sum2 + sum1 * f(
                                xSpace[z]) * sqrt(2.0) / (sqrt(2.0 * nu) * pi *
                                                          (k + 1))
                        ci[i, z] = sum2
                    return ci
Example #12
0
    def u02(self):
        """
        Function to calculate the constants of the system of ordinary differential equations given the initial condition

        Parameters
        ----------
        J : array; shape(M, M)
            Array containing the grades of the polynomials
        N : int; order max of the polynomials
        M : int; number of ODEs
        rulesX : array; polynomials of Hermite evaluated
        rulesW : array; weights of polynomials of Hermite evaluated
        LRules : array; length of rulesX array
        xSpace : array; discretized real space
        nu : array; Diffusion coefficient
        u0 : callable(x); initial condition function

        Returns
        -------
        ci: array, shape(M, len(xSpace))
            Array containing the constants of the system of ordinary differential equations

        """
        Lx = len(self.x)
        ci = np.zeros([self.M, Lx])

        for z in range(0, Lx):
            for i in range(0, self.M):
                sum2 = 0
                for k in range(0, self.M):
                    sum1 = 0
                    if self.J[k, i] > 0:
                        for y in range(0, self.LRules):
                            sum1 = sum1 + special.eval_hermitenorm(
                                int(self.J[k, i]), self.rulesX[y]
                            ) * self.rulesX[y] * self.rulesW[y]
                    sum2 = sum2 + sum1 * self.u0(
                        self.x[z]) * sqrt(2.0) / (sqrt(2.0 * self.nu) * pi *
                                                  (k + 1))
                ci[i, z] = sum2
        return ci
Example #13
0
def gen(n, x, name):
    """Generate fixture data and write to file.

    # Arguments

    * `n`: degree(s)
    * `x`: domain
    * `name::str`: output filename

    # Examples

    ``` python
    python> n = 1
    python> x = linspace(-1000, 1000, 2001)
    python> gen(n, x, './data.json')
    ```
    """
    y = eval_hermitenorm(n, x)
    if isinstance(n, np.ndarray):
        data = {
            "n": n.tolist(),
            "x": x.tolist(),
            "expected": y.tolist()
        }
    else:
        data = {
            "n": n,
            "x": x.tolist(),
            "expected": y.tolist()
        }

    # Based on the script directory, create an output filepath:
    filepath = os.path.join(DIR, name)

    # Write the data to the output filepath as JSON:
    with open(filepath, "w") as outfile:
        json.dump(data, outfile)
Example #14
0
def pdf4(phi, a, m=4, verbose=False):
	'''
	This implementation expands the posterior distribution in terms of the standard normal
	distribution using the Gram-Charlier series (http://en.wikipedia.org/wiki/Edgeworth_series).
	This series generally doesn't converge, but can be really fast.  Generally, don't use more 
	than four or five terms (m=4).  This makes the distribution accurate to the fourth moment, 
	the kurtosis and is much better than using a normal distribution (which is the first two moments). 
	
	Actually, just use pdf3.
	
	References:
	https://indico.cern.ch/event/74919/session/16/contribution/88/material/slides/1.pdf
	'''

	sigma = math.sqrt(b_l(2,a))
	mu = b_l(1,a)
	z = (phi - mu)/sigma

	C = [1.,-1.,-1.,1.]

	normpart = stats.norm.pdf(phi, loc=mu, scale=sigma)
	seriespart = [  b_l(k,a)/(math.factorial(k) * sigma**(k) )  *  special.eval_hermitenorm(k, z)[0] for k in range(3,m+1) ]

	return normpart * (1 + np.sum(seriespart))
Example #15
0
 def f1(x, y, Jij):
     return abs((-1)**Jij * (special.eval_hermitenorm(Jij, x) -
                             special.eval_hermitenorm(Jij, y)) /
                factorial(Jij + 1)**2)**2
Example #16
0
def m2fs_ghlb_profile(fname, tracefname, tracestdfname, fibermapfname,
                      x_begin, x_end,
                      dy=5, legorder=6, ghorder=10,
                      make_plot=True, suffix=""):
    """ Use the same dy as in m2fs_trace_orders """
    Nleg = legorder + 1
    Ngh = ghorder + 1

    outdir = os.path.dirname(fname)
    outname = os.path.basename(fname)[:-5]+suffix
    fname_fitparams = os.path.join(outdir,outname+"_ghlb_fitparams.txt")
    fname_allyarr = os.path.join(outdir,outname+"_ghlb_yarr.npy")
    fname_alldatarr = os.path.join(outdir,outname+"_ghlb_data.npy")
    fname_allerrarr = os.path.join(outdir,outname+"_ghlb_errs.npy")
    fname_allprofiles = os.path.join(outdir,outname+"_ghlb_profiles.npy")
    
    Nparam=Nleg*Ngh
    
    data, edata, header = read_fits_two(fname)
    nx, ny = data.shape
    coeffcen = np.loadtxt(tracefname)
    coeffstd = np.loadtxt(tracestdfname)
    fibmap = np.loadtxt(fibermapfname)
    npeak, norder = len(coeffcen), fibmap.shape[1]
    # Generate valid pixels: could do this in a more refined way
    xarrlist = [np.arange(nx) for i in range(norder)]
    xarrlist[0] = np.arange(nx-x_begin)+x_begin
    xarrlist[-1] = np.arange(x_end)
    
    ## SECOND TRACE:
    # For each order, fit spatial profile to the full trace simultaneously
    # Following Kelson ghlb.pdf
    # But fitting each fiber separately in some specified extraction region
    # rather than solving for the distortion functions
    
    # Go 1 pixel further on each side, ensure an odd number
    Nextract = int(dy)+4 + int(int(dy) % 2 == 0)
    dextract = int(Nextract/2.)
    vround = np.vectorize(lambda x: int(round(x)))
    start = time.time()
    # Outputs
    allyarr = np.zeros((npeak,Nextract,nx))
    fitparams = np.zeros((npeak,Nleg*Ngh))
    alldatarr = np.full((npeak,nx,Nextract), np.nan)
    allerrarr = np.full((npeak,nx,Nextract), np.nan)
    allfitarr = np.full((npeak,nx,Nextract), np.nan)
    for ipeak in range(npeak):
        # Initialize peak locations, widths, and data arrays
        xarr = xarrlist[ipeak % norder]
        Nxarr = len(xarr)
        Nxy = Nxarr*Nextract
        
        ypeak = np.polyval(coeffcen[ipeak],xarr)
        ystdv = np.polyval(coeffstd[ipeak],xarr)
        assert np.all(ystdv > 0)
        assert np.all(ystdv > 0)
        intypeak = vround(ypeak)
        intymin = intypeak - dextract
        intymax = intypeak + dextract + 1
        yarr = np.array([np.arange(intymin[j],intymax[j]) for j in range(Nxarr)])
        allyarr[ipeak,:,xarr] = yarr
        
        # Grab the relevant data into a flat array for fitting
        datarr = np.zeros((Nxarr,Nextract))
        errarr = np.zeros((Nxarr,Nextract))
        for j,jx in enumerate(xarr):
            datarr[j,:] =  data[jx,intymin[j]:intymax[j]]
            errarr[j,:] = edata[jx,intymin[j]:intymax[j]]
        assert np.all(errarr > 0), (errarr <= 0).sum()
        alldatarr[ipeak,xarr,:] = datarr
        allerrarr[ipeak,xarr,:] = errarr
        # Apply simple estimate of spectrum flux: sum everything
        specestimate = np.sum(datarr, axis=1)
        
        # Set up the polynomial grid in xl and ys
        ys = (yarr.T-ypeak)/ystdv
        expys = np.exp(-ys**2/2.)
        xl = (xarr-np.mean(xarr))/(Nxarr/2.)
        polygrid = np.zeros((Nxarr,Nextract,Nleg,Ngh))
        La = [special.eval_legendre(a,xl)*specestimate for a in range(Nleg)]
        GHb = [special.eval_hermitenorm(b,ys)*expys for b in range(Ngh)]
        for a in range(Nleg):
            for b in range(Ngh):
                polygrid[:,:,a,b] = (GHb[b]*La[a]).T
        polygrid = np.reshape(polygrid,(Nxy,Nparam))
        
        # this is a *linear* least squares fit!
        Xarr = np.reshape(polygrid,(Nxy,Nparam))
        Yarr = np.ravel(datarr)
        # use errors as weights. note that we don't square it, the squaring happens later
        warr = np.ravel(errarr)**-1
        wXarr = (Xarr.T*warr).T
        wYarr = warr*Yarr
        pfit, residues, rank, svals = linalg.lstsq(wXarr, wYarr)
        fitparams[ipeak] = pfit
        fity = pfit.dot(Xarr.T).reshape(Nxarr,Nextract)
        allfitarr[ipeak][xarr,:] = fity
    print("GHLB took {:.1f}s".format(time.time()-start))
    np.savetxt(fname_fitparams,fitparams)
    np.save(fname_allyarr,allyarr)
    np.save(fname_alldatarr,alldatarr)
    np.save(fname_allerrarr,allerrarr)
    np.save(fname_allprofiles,allfitarr)

    if make_plot:
        import matplotlib.pyplot as plt
        Nrow, Ncol = fibmap.shape
        fig1, axes1 = plt.subplots(Nrow,Ncol,figsize = (Ncol*6,Nrow*2.5))
        fig2, axes2 = plt.subplots(Nrow,Ncol,figsize = (Ncol*6,Nrow*2.5))
        fig3, axes3 = plt.subplots(Nrow,Ncol,figsize = (Ncol*6,Nrow*2.5))
        fig4, axes4 = plt.subplots(Nrow,Ncol,figsize = (Ncol*6,Nrow*2.5))
        zplot = np.linspace(-6,6)
        normz = np.exp(-0.5*zplot**2)/(2*np.sqrt(np.pi))
        vmax3 = np.nanpercentile(alldatarr,[99.9])[0]
        vmin4 = np.nanpercentile(allerrarr,[10])[0]
        vmax4 = np.nanpercentile(allerrarr,[90])[0]
        for ipeak, (ax1,ax2,ax3,ax4) in enumerate(zip(axes1.flat,axes2.flat,axes3.flat,axes4.flat)):
            z = (alldatarr[ipeak].T-allfitarr[ipeak].T)/allerrarr[ipeak].T
            im = ax1.imshow(z, origin='lower', aspect='auto',vmin=-6,vmax=6)
            ax2.hist(np.ravel(z[np.isfinite(z)]),bins=zplot,normed=True)
            ax2.plot(zplot,normz,lw=.5)
            im = ax3.imshow(alldatarr[ipeak].T, origin='lower', aspect='auto',vmin=0,vmax=vmax3)
            im = ax4.imshow(alldatarr[ipeak].T, origin='lower', aspect='auto',vmin=vmin4,vmax=vmax4)
        outpre = outdir+"/"+outname
        fig1.savefig(outpre+"_ghlbresid_zimg.png",bbox_inches='tight')
        fig2.savefig(outpre+"_ghlbresid_hist.png",bbox_inches='tight')
        fig3.savefig(outpre+"_ghlbresid_img.png",bbox_inches='tight')
        fig4.savefig(outpre+"_ghlbresid_err.png",bbox_inches='tight')
        plt.close(fig1); plt.close(fig2); plt.close(fig3); plt.close(fig4)
Example #17
0
 def f2(x, y, Jij):
     return abs((-1)**Jij * special.eval_hermitenorm(Jij, y) /
                factorial(Jij + 1)**2)**2