Example #1
0
def GaussHerWeights(n, S=1.0, positive=False):
    """
    """
    if (positive):
        y, wy = hermgauss(2 * n)
        y = y[n:]
        wwy = wy[n:]
    else:
        y, wwy = hermgauss(n)

    wy = np.exp(np.log(wwy) + y**2)

    return S * y, S * wy, S * wwy
def EIG_GaussianHermitte_matrix_Hybrid_MST(mu_mtx,sigma_mtx):
    """ this is the matrix implementation version"""
    """mu is the matrix of difference of two means (si-sj), sigma is the matrix of sigma of si-sj"""
    epsilon = 1e-9
    M,M_ = np.shape(mu_mtx)
    
    
    mu = np.reshape(mu_mtx, (1,-1))
    sigma = np.reshape(sigma_mtx, (1,-1))
    
  
    fs1 = lambda x: (1./(1+np.exp(-np.sqrt(2)*sigma*x-mu)))*(-np.log(1+np.exp(-np.sqrt(2)*sigma*x-mu)))/np.sqrt(math.pi);
    fs2 = lambda x: (1-1./(1+np.exp(-np.sqrt(2)*sigma*x-mu)))*(np.log(np.exp(-np.sqrt(2)*sigma*x-mu)/(1+np.exp(-np.sqrt(2)*sigma*x-mu))))/np.sqrt(math.pi);
    fs3 = lambda x: 1./(1+np.exp(-np.sqrt(2)*sigma*x-mu))/np.sqrt(math.pi);
    fs4 = lambda x: (1-1./(1+np.exp(-np.sqrt(2)*sigma*x-mu)))/np.sqrt(math.pi);
        
    x,w = herm.hermgauss(30)
    x = np.reshape(x,(-1,1))
    w = np.reshape(w,(-1,1))
    
    es1 = np.sum(w*fs1(x),0)
    es2 = np.sum(w*fs2(x),0)
    es3 = np.sum(w*fs3(x),0)
    es3 = es3*np.log(es3+epsilon)
    es4 = np.sum(w*fs4(x),0)
    es4 = es4*np.log(es4+epsilon)
    
    ret = es1 + es2 - es3 + es4
    ret = np.reshape(ret,(M,M_))
    ret = -np.triu(ret,1)
    return ret+ret.T
Example #3
0
def set_mu_sigma(_mu, _sigma):
    global mu, sigma, betavec, beta_weights
    mu = _mu
    sigma = _sigma
    betavec, beta_weights = hermgauss(15)
    betavec = mu + sigma * betavec * sqrt(2)
    beta_weights /= sum(beta_weights)
Example #4
0
def integrate_hermgauss_nd(func, mean, Sigma_x, order):
    """
	n-d Gauss-Hermite quadrature

	:param func: lambda x: y (x: vector of floats)
	:param mean: mean vector of normal weight function
	:param Sigma_x: covariance matrix of normal weight function
	:param order: the order of the integration rule
	:return: :math:`E[f(X)] (X \sim \mathcal{N}(\mu,\sigma^2)) = \int_{-\infty}^{\infty}f(x)p(x),\mathrm{d}x` with p being the normal density
	"""
    from itertools import product
    dim = len(mean)
    mean = np.array(mean)
    sigma = np.array([np.sqrt(Sigma_x[i][i]) for i in range(dim)])

    x, w = hermgauss(order)
    xs = product(x, repeat=dim)
    ws = np.array(list(product(w, repeat=dim)))
    y = []
    sqrt2 = np.sqrt(2)
    for i, x in enumerate(xs):
        y.append(func(x * sigma * sqrt2 + mean) * ws[i].prod())
    y = np.array(y)
    return y.sum() / np.sqrt(
        np.pi)**dim  # * 1/(sigma*np.sqrt(2*np.pi)) * sigma * np.sqrt(2)
Example #5
0
def mvhermgauss(H: int, D: int):
    """
    This function is taken from GPflow: https://github.com/GPflow/GPflow
    Copied here rather than imported so that users don't need to install gpflow to use kalman-jax

    LICENSE:

        Copyright The Contributors to the GPflow Project. All Rights Reserved.

        Licensed under the Apache License, Version 2.0 (the "License");
        you may not use this file except in compliance with the License.
        You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

        Unless required by applicable law or agreed to in writing, software
        distributed under the License is distributed on an "AS IS" BASIS,
        WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
        See the License for the specific language governing permissions and
        limitations under the License.

    Return the evaluation locations 'xn', and weights 'wn' for a multivariate
    Gauss-Hermite quadrature.

    The outputs can be used to approximate the following type of integral:
    int exp(-x)*f(x) dx ~ sum_i w[i,:]*f(x[i,:])

    :param H: Number of Gauss-Hermite evaluation points.
    :param D: Number of input dimensions. Needs to be known at call-time.
    :return: eval_locations 'x' (H**DxD), weights 'w' (H**D)
    """
    gh_x, gh_w = hermgauss(H)
    x = np.array(list(itertools.product(*(gh_x, ) * D)))  # H**DxD
    w = np.prod(np.array(list(itertools.product(*(gh_w, ) * D))), 1)  # H**D
    return x, w
    def calcMagneticModel(self):
        self.x_herm, self.w_herm = hermgauss(int(self.params['orderHermite']))
        self.x_leg, self.w_leg = leggauss(int(self.params['orderLegendre']))
        self.I = self.params['i0'] * superball_css_coupled2.magnetic_formfactor(
            self.q, self.params['particleSize'], self.params['dShell'], self.
            params['dSurfactant'], self.params['pVal'], self.params['sldCore'],
            self.params['sldShell'], self.params['sldSurfactant'],
            self.params['sldSolvent'], self.params['sigParticleSize'],
            self.params['sigD'], self.params['magSldCore'],
            self.params['magSldShell'], self.params['magSldSurfactant'],
            self.params['magSldSolvent'], self.params['xi'],
            self.params['sin2alpha'], self.params['polarization'], self.x_herm,
            self.w_herm, self.x_leg, self.w_leg) + self.params['bg']

        self.r, self.sld = superball_css_coupled2.sld(
            self.params['particleSize'], self.params['dShell'],
            self.params['dSurfactant'], self.params['sldCore'],
            self.params['sldShell'], self.params['sldSurfactant'],
            self.params['sldSolvent'])

        self.rMag, self.sldMag = superball_css_coupled2.sld(
            self.params['particleSize'],
            self.params['dShell'],
            self.params['dSurfactant'],
            self.params['magSldCore'],
            self.params['magSldShell'],
            self.params['magSldSurfactant'],
            self.params['magSldSolvent'],
        )
Example #7
0
	def test_hermgauss(self):
		from numpy.polynomial.hermite import hermgauss
		from scipy.integrate import quad

		x,w = hermgauss(100)
		y = []
		sqrt2 = np.sqrt(2)
		for xi in x:
			y.append(np.cos(xi))
		y = np.array(y)
		h = lambda x: np.cos(x)*np.exp(-x**2)
		self.assertAlmostEqual( (y*w).sum(), quad(h,-10,10)[0], delta=1e-5)

		from skgpuppy.FFNI import FullFactorialNumericalIntegrationNaive
		from skgpuppy.Utilities import integrate_hermgauss_nd, integrate_hermgauss
		f = lambda x: np.sin(x[0])
		ffni = FullFactorialNumericalIntegrationNaive(f,np.array([1]))
		self.assertAlmostEqual(integrate_hermgauss(f,1,2,order=100),ffni.propagate(np.array([[4]]))[0],delta=1e-4)

		f = lambda x: np.cos(x[0]*x[1])
		stddev = 0.3
		Sigma_x = np.diag(np.array([stddev,stddev])**2)
		ffni = FullFactorialNumericalIntegrationNaive(f,np.array([1.0,1.0]))

		self.assertAlmostEqual(integrate_hermgauss_nd(f,[1.0,1.0],Sigma_x,10),ffni.propagate(Sigma_x)[0],delta=1e-4)
Example #8
0
 def t2_gt_t1_integral(self,
                       a: float,
                       b: float,
                       k1: int,
                       n1: int,
                       k2: int,
                       n2: int,
                       num_points=10) -> float:
     # Generate nodes for t1 integration, which is Gauss-Hermite since it's f(t1) * exp(-t1^2) over [-inf,inf].
     M = 8
     node, weight = hermgauss(num_points)
     # The normal distribution is proportional to exp(-theta2^2/2); change variables z = theta2/sqrt(2)
     # to get the form int f(z) exp(-z^2) dz used by Gauss-Hermite.
     scale = np.sqrt(2)
     node *= scale
     # For each t1, we calculate the integral over [t1, high] where high is slightly larger than the last t1 node.
     high = max(M, node[-1] + 1)
     integral_from_t1_to_inf = [
         quadrature(likelihood,
                    theta1,
                    high,
                    args=(a, b, k2, n2),
                    tol=1e-12,
                    rtol=1e-12,
                    maxiter=100)[0] for theta1 in node
     ]
     values = [
         irf(theta, a, b, k1, n1) * f
         for theta, f in zip(node, integral_from_t1_to_inf)
     ]
     return sum(values * weight) * scale / (2 * np.pi)
Example #9
0
  def calcMagneticModel(self):
    self.x_herm, self.w_herm = hermgauss(int(self.params['orderHermite']))
    self.x_leg, self.w_leg = leggauss(int(self.params['orderLegendre']))

    self.I = self.params['i0'] * cube.magnetic_formfactor(
      self.q,
      self.params['a'],
      self.params['sldCore'],
      self.params['sldSolvent'],
      self.params['sigA'],
      self.params['magSldCore'],
      self.params['magSldSolvent'],
      self.params['xi'],
      self.params['sin2alpha'],
      self.params['polarization'],
      self.x_herm, self.w_herm, self.x_leg, self.w_leg
    ) + self.params['bg']

    self.r, self.sld = cube.sld(
      self.params['a'],
      self.params['sldCore'],
      self.params['sldSolvent']
    )

    self.rMag, self.sldMag = cube.sld(
      self.params['a'],
      self.params['magSldCore'],
      self.params['magSldSolvent']
    )
  def calcModel(self):
    self.x_herm, self.w_herm = hermgauss(int(self.params['orderHermite']))
    self.x_leg, self.w_leg = leggauss(int(self.params['orderLegendre']))
    self.I = self.params['i0'] * superball_css_coupledvms.formfactor(
      self.q,
      self.params['particleSize'],
      self.params['dShell'],
      self.params['dSurfactant'],
      self.params['pVal'],
      self.params['sldCore'],
      self.params['sldShell'],
      self.params['sldSurfactant'],
      self.params['sldSolvent'],
      self.params['sigParticleSize'],
      self.x_herm, self.w_herm, self.x_leg, self.w_leg
    ) + self.params['bg']

    self.r, self.sld = superball_css_coupledvms.sld(
      self.params['particleSize'],
      self.params['dShell'],
      self.params['dSurfactant'],
      self.params['sldCore'],
      self.params['sldShell'],
      self.params['sldSurfactant'],
      self.params['sldSolvent']
    )
Example #11
0
def hermgauss_lognorm(n, sigma):

    x, w = hermgauss(n)
    x = np.exp(x * np.sqrt(2) * sigma - 0.5 * sigma**2)
    w = w / np.sqrt(np.pi)

    return x, w
Example #12
0
def set_mu_sigma(_mu,_sigma):
    global mu,sigma,betavec,beta_weights
    mu = _mu
    sigma = _sigma
    betavec,beta_weights = hermgauss(15)
    betavec= mu + sigma*betavec*sqrt(2)
    beta_weights /= sum(beta_weights)    
Example #13
0
    def quad2(self, mvn, k=17, dtype=tf.float64):
        """for compute 3-d integral in the most efficient way
        tri-variate quadrature method to evaluate expectation over tri-variate gaussian distribution"""
        sp1 = [1, 1, 1, -1]
        sp2 = [1, 1, -1, 1]
        x, w = hermgauss(k)
        xi, xj = tf.meshgrid(x, x)
        wi, wj = tf.meshgrid(w, w)
        xi = tf.cast(tf.reshape(xi, sp1), dtype)
        xj = tf.cast(tf.reshape(xj, sp1), dtype)
        wij = tf.cast(tf.reshape(wi * wj / pi, sp1), dtype)

        u1, u2 = tf.reshape(mvn['mu'][:, 0],
                            sp2), tf.reshape(mvn['mu'][:, 1], sp2)
        o1, o2 = tf.reshape(mvn['sigma'][:, 0],
                            sp2), tf.reshape(mvn['sigma'][:, 1], sp2)
        p = tf.reshape(mvn['rou'], sp2)

        xn = tf.cast(tf.reshape(x, sp1), dtype)
        wn = tf.cast(tf.reshape(w / sqrtpi, sp1), dtype)
        Xn = tf.concat([sqrt2 * o1 * xn + u1, sqrt2 * o2 * xn + u2], axis=1)

        a = tf.sqrt(1 + p) / 2 + tf.sqrt(1 - p) / 2
        b = tf.sqrt(1 + p) / 2 - tf.sqrt(1 - p) / 2
        z1 = a * xi + b * xj
        z2 = b * xi + a * xj
        xe = tf.stack([sqrt2 * o1 * z1 + u1, sqrt2 * o2 * z2 + u2], -1)

        return wn, Xn, wij, xe
Example #14
0
 def _predict_non_logged_density(self, Fmu, Fvar, Y):
     if isinstance(self.invlink, RobustMax):
         gh_x, gh_w = hermgauss(self.num_gauss_hermite_points)
         p = self.invlink.prob_is_largest(Y, Fmu, Fvar, gh_x, gh_w)
         return p * (1 - self.invlink.epsilon) + (1. - p) * (
             self.invlink._eps_K1)
     else:
         raise NotImplementedError
Example #15
0
 def variational_expectations(self, Fmu, Fvar, Y):
     if isinstance(self.invlink, RobustMax):
         gh_x, gh_w = hermgauss(self.num_gauss_hermite_points)
         p = self.invlink.prob_is_largest(Y, Fmu, Fvar, gh_x, gh_w)
         return p * np.log(1 - self.invlink.epsilon) + (1. - p) * np.log(
             self.invlink._eps_K1)
     else:
         raise NotImplementedError
Example #16
0
    def test_logitnormal_moments(self):
        # global parameters for computing lognormals
        gh_loc, gh_weights = hermgauss(4)

        # log normal parameters
        lognorm_means = np.random.random((5, 3)) # should work for arrays now
        lognorm_infos = np.random.random((5, 3))**2 + 1
        alpha = 2 # dp parameter

        # draw samples
        num_draws = 10**5
        samples = np.random.normal(lognorm_means,
                        1/np.sqrt(lognorm_infos), size = (num_draws, 5, 3))
        logit_norm_samples = sp.special.expit(samples)

        # test lognormal means
        np_test.assert_allclose(
            np.mean(logit_norm_samples, axis = 0),
            ef.get_e_logitnormal(
                lognorm_means, lognorm_infos, gh_loc, gh_weights),
            atol = 3 * np.std(logit_norm_samples) / np.sqrt(num_draws))

        # test Elog(x) and Elog(1-x)
        log_logistic_norm = np.mean(np.log(logit_norm_samples), axis = 0)
        log_1m_logistic_norm = np.mean(np.log(1 - logit_norm_samples), axis = 0)

        tol1 = 3 * np.std(np.log(logit_norm_samples))/ np.sqrt(num_draws)
        tol2 = 3 * np.std(np.log(1 - logit_norm_samples))/ np.sqrt(num_draws)

        np_test.assert_allclose(
            log_logistic_norm,
            ef.get_e_log_logitnormal(
                lognorm_means, lognorm_infos, gh_loc, gh_weights)[0],
            atol = tol1)

        np_test.assert_allclose(
            log_1m_logistic_norm,
            ef.get_e_log_logitnormal(
                        lognorm_means, lognorm_infos, gh_loc, gh_weights)[1],
            atol = tol2)

        # test prior
        prior_samples = np.mean((alpha - 1) *
                            np.log(1 - logit_norm_samples), axis = 0)
        tol3 = 3 * np.std((alpha - 1) * np.log(1 - logit_norm_samples)) \
                    /np.sqrt(num_draws)
        np_test.assert_allclose(
            prior_samples,
            ef.get_e_dp_prior_logitnorm_approx(
                        alpha, lognorm_means, lognorm_infos, gh_loc, gh_weights),
            atol = tol3)

        x = np.random.normal(0, 1e2, size = 10)
        def e_log_v(x):
            return np.sum(ef.get_e_log_logitnormal(\
                        x[0:5], np.abs(x[5:10]), gh_loc, gh_weights)[0])
        check_grads(e_log_v, order=2)(x)
Example #17
0
    def points_and_weights(self, N=None, map_true_domain=False):
        if N is None:
            N = self.N
        if self.quad == "HG":
            points, weights = hermite.hermgauss(N)
            weights *= np.exp(points**2)
        else:
            raise NotImplementedError

        return points, weights
Example #18
0
 def _set_quadrature_points(self, deg):
     self.deg = deg
     # these points and weights are designed for integrating with
     # the following weight function: :math:`f(x) = \exp(-x^2)`
     x, w = hermgauss(deg=deg)
     # the following modification is for integrating with the
     # standard normal distribution
     self.x = np.sqrt(2) * x
     w = w / np.sqrt(np.pi)
     self.w = np.asmatrix(w[:, np.newaxis])
Example #19
0
 def calcModel(self):
   self.x_herm, self.w_herm = hermgauss(int(self.params['orderHermite']))
   self.M = langevin.mu_weighted_magnetization(
     self.B,
     self.params['Ms'],
     self.params['mu'],
     self.params['T'],
     self.params['sigMu'],
     self.x_herm, self.w_herm
   ) + self.params['chi']*self.B
  def __init__(self, integrand, arity=2, height=2, model=None, **kwargs):
    self.integrand = integrand
    self.arity = arity
    self.height = height
    self.model = model

    # Quadrature terms (pre-multiplied)
    # abscissae, importances = leggauss(self.arity)
    abscissae, importances = hermgauss(self.arity)
    self._abscissae = np.sqrt(2)*abscissae
    self._importances = importances/np.sqrt(np.pi)
    def __init__(self, g, num_mixtures=5, num_quadrature_points=3):
        self.g = CompressedGraph(g)

        self.K = num_mixtures
        self.T = num_quadrature_points
        self.quad_x, self.quad_w = hermgauss(self.T)
        self.quad_w /= sqrt(pi)

        self.w_tau = np.zeros(self.K)
        self.w = np.zeros(self.K)
        self.eta_tau = dict()
        self.eta = dict(
        )  # key=rv, value={continuous eta: [k, [mu, var]], discrete eta: [k, d]}
Example #22
0
    def calcModel(self):
        self.x_herm, self.w_herm = hermgauss(int(self.params['orderHermite']))
        self.x_leg, self.w_leg = leggauss(int(self.params['orderLegendre']))

        self.I = self.params['i0'] * superball.formfactor(
            self.q, self.params['r'], self.params['pVal'],
            self.params['sldCore'], self.params['sldSolvent'],
            self.params['sigR'], self.x_herm, self.w_herm, self.x_leg,
            self.w_leg) + self.params['bg']

        self.r, self.sld = superball.sld(self.params['r'],
                                         self.params['sldCore'],
                                         self.params['sldSolvent'])
Example #23
0
def test_wheeler():
    """
    This function tests QBMM Wheeler inversion by comparing
    against numpy's Gauss-Hermite for given mu and sigma
    """
    num_nodes = 4
    config = {}
    config["qbmm"] = {}
    config["qbmm"]["governing_dynamics"] = "4*x - 2*x**2"
    config["qbmm"]["num_internal_coords"] = 1
    config["qbmm"]["num_quadrature_nodes"] = num_nodes
    config["qbmm"]["method"] = "qmom"
    ###
    ### QBMM
    qbmm_mgr = qbmm_manager(config)

    ###
    ### Tests

    # Anticipate success
    tol = 1.0e-10  # Round-off error in moments computation
    success = True

    # Test 1
    mu = 5.0
    sigma = 1.0
    ###
    ### Reference solution
    sqrt_pi = np.sqrt(np.pi)
    sqrt_two = np.sqrt(2.0)
    h_abs, h_wts = hermite_poly.hermgauss(num_nodes)
    g_abs = sqrt_two * sigma * h_abs + mu
    g_wts = h_wts / sqrt_pi

    ###
    ### QBMM
    moments = raw_gaussian_moments_univar(qbmm_mgr.num_moments, mu, sigma)
    my_abs, my_wts = qbmm_mgr.moment_invert(moments)

    ###
    ### Errors & Report
    diff_abs = my_abs - g_abs
    diff_wts = my_wts - g_wts

    error_abs = np.linalg.norm(my_abs - g_abs)
    error_wts = np.linalg.norm(my_wts - g_wts)

    assert error_abs < tol
    assert error_wts < tol
Example #24
0
    def test_100(self):
        x, w = herm.hermgauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = herm.hermvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1/np.sqrt(vv.diagonal())
        vv = vd[:,None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # check that the integral of 1 is correct
        tgt = np.sqrt(np.pi)
        assert_almost_equal(w.sum(), tgt)
Example #25
0
    def calcModel(self):
        self.x_herm, self.w_herm = hermgauss(int(self.params['orderHermite']))
        self.x_leg, self.w_leg = leggauss(int(self.params['orderLegendre']))

        self.I = self.params['i0'] * cube_cs_coupled.formfactor(
            self.q, self.params['particleSize'], self.params['d'],
            self.params['sldCore'], self.params['sldShell'], self.
            params['sldSolvent'], self.params['sigParticleSize'], self.x_herm,
            self.w_herm, self.x_leg, self.w_leg) + self.params['bg']

        self.r, self.sld = cube_cs_coupled.sld(self.params['particleSize'],
                                               self.params['d'],
                                               self.params['sldCore'],
                                               self.params['sldShell'],
                                               self.params['sldSolvent'])
Example #26
0
    def test_100(self):
        x, w = herm.hermgauss(100)

        # test orthogonality. Note that the results need to be normalized,
        # otherwise the huge values that can arise from fast growing
        # functions like Laguerre can be very confusing.
        v = herm.hermvander(x, 99)
        vv = np.dot(v.T * w, v)
        vd = 1 / np.sqrt(vv.diagonal())
        vv = vd[:, None] * vv * vd
        assert_almost_equal(vv, np.eye(100))

        # get_inds that the integral of 1 is correct
        tgt = np.sqrt(np.pi)
        assert_almost_equal(w.sum(), tgt)
Example #27
0
        def integrate_gaussherm(y0, x, h, deg=5):
            """
            Numerically integrate the integral in the statistic of Zheng 2000
            with Gauss-Hermitite quadrature.

            deg: degree of polynomials
            """
            import numpy
            from numpy.polynomial.hermite import hermgauss
            points, weights = hermgauss(deg)
            n = len(weights)
            vec_evals = np.empty(n)
            for i in range(n):
                vec_evals[i] = self._integrand_wo_gaussian(points[i], y0, x, h)
            integrated = weights.dot(vec_evals)
            return integrated
    def __init__(self, *args, **kws):
        """
        Constructor
        """
        super(GaussHermiteQuadrature, self).__init__(*args, **kws)

        # init gauss-legendre points
        for i in xrange(self._n):
            # get rootsArray in [-1, 1]
            rootsArray, weights = hermgauss(i + 1)
            # transform rootsArray to [0, 1]
            rootsArray = (rootsArray + 1) / 2.
            # normalize weights
            weights /= np.sum(weights)
            # zip them
            self._gaussPoints[i] = zip(rootsArray, weights)
Example #29
0
def convert_norm(number_of_nodes, variables):
    # convert Gauss–Hermite nodes and weights to integrate specific distributions

    variable_mus = [variables["diameter"][0], variables["length"][0], variables["rho_cu"][0],
                               variables["rho_fe"][0]]
    variable_sigmas = [variables["diameter"][1], variables["length"][1], variables["rho_cu"][1],
                               variables["rho_fe"][1]]

    points, weights = hermgauss(number_of_nodes)
    variable_points =[]
    variable_weights = [i / np.sqrt(np.pi) for i in weights]

    for j in range(len(variables)):
        variable_points.append([(np.sqrt(2) * i * variable_sigmas[j]) + variable_mus[j] for i in points])

    return variable_points, variable_weights
Example #30
0
 def marginal_integral(self,
                       a: float,
                       b: float,
                       k1: int,
                       n1: int,
                       k2: int,
                       n2: int,
                       num_points=10) -> float:
     # Generate nodes for t1 integration, which is Gauss-Hermite since it's f(t1) * exp(-t1^2) over [-inf,inf].
     node, weight = hermgauss(num_points)
     node *= np.sqrt(2)
     t1_values = [irf(theta, a, b, k1, n1) for theta in node]
     t1_integral = sum(t1_values * weight)
     t2_values = [irf(theta, a, b, k2, n2) for theta in node]
     t2_integral = sum(t2_values * weight)
     return t1_integral * t2_integral / np.pi
    def __init__(self, *args, **kws):
        """
        Constructor
        """
        super(GaussHermiteQuadrature, self).__init__(*args, **kws)

        # init gauss-legendre points
        for i in xrange(self._n):
            # get rootsArray in [-1, 1]
            rootsArray, weights = hermgauss(i + 1)
            # transform rootsArray to [0, 1]
            rootsArray = (rootsArray + 1) / 2.
            # normalize weights
            weights /= np.sum(weights)
            # zip them
            self._gaussPoints[i] = zip(rootsArray, weights)
    def __init__(self, g, num_mixtures=5, num_quadrature_points=3):
        self.g = g
        self.init_rv()

        self.K = num_mixtures
        self.T = num_quadrature_points
        self.quad_x, self.quad_w = hermgauss(self.T)
        self.quad_w /= sqrt(pi)

        self.w_tau = np.zeros(self.K)
        self.w = np.zeros(self.K)
        self.eta_tau = dict()
        self.eta = dict(
        )  # key=rv, value={continuous eta: [, [mu, var]], discrete eta: [k, d]}

        self.condition_weight = None
        self.condition_prob = 1
Example #33
0
def integrate_hermgauss(func,mean,sigma,order=1):
	"""
	1-d Gauss-Hermite quadrature

	:param func: lambda x: y (x: float)
	:param mean: mean of normal weight function
	:param sigma: standard dev of normal weight function
	:param order: the order of the integration rule
	:return: :math:`E[f(X)] (X \sim \mathcal{N}(\mu,\sigma^2)) = \int_{-\infty}^{\infty}f(x)p(x),\mathrm{d}x` with p being the normal density
	"""
	x,w = hermgauss(order)
	y = []
	sqrt2 = np.sqrt(2)
	for xi in x:
		y.append(func((xi*sigma*sqrt2+mean,))) #
	y = np.array(y)
	return (y*w).sum() / np.sqrt(np.pi) # * 1/(sigma*np.sqrt(2*np.pi)) * sigma * np.sqrt(2)
Example #34
0
File: gq.py Project: maremun/quffka
def generate_gq_weights(d, n, deg=2):
    '''
    Subsample points and weights for Gauss-Hermite quadrature.
    Args
        d: the number of dimensions of the integral.
        n: number of SR rules to be equivalent to in terms of output feature
           dimensionality D.
        deg: the degree of the polynomial that is approximated accurately by
             this quadrule is 2-1.

    Returns
        W_subsampled: the points, matrix of size d x D.
        A_subsampled: the weights, vector of size D.
    '''
    W, A = herm.hermgauss(deg)
    # Use ortho for higher dimensions
#     W, A = orth.line.schemes.hermite(d, 30)
    # make sure we are not working with object datatype
#     W = np.array(W, dtype='float64')
#     A = np.array(A, dtype='float64')
    # normalize weights
    # this is where the 1/sqrt(pi) constant is hidden
    # since sum of the weights is exactly the constant!
    A = A / A.sum()
    D = get_D(d, n)
    c = np.empty((d, D))
    I = np.arange(W.shape[0])
    for i in range(d):
        c[i] = np.random.choice(I, D, True, A)
    c = c.astype('int')
    W_subsampled = W[c]
    A_subsampled = np.sum(np.log(A[c]), axis=0)
    A_subsampled -= A_subsampled.max()
    A_subsampled = np.exp(A_subsampled)

#    I = np.arange(W.shape[0])
#    c = np.random.choice(I, d*L, True, A)
#    W_subsampled = np.reshape(W[c], (d,L))
#    A_subsampled = np.prod(np.reshape(A[c], (d,L)), axis=0)
##     W_subsampled = np.ascontiguousarray(W_subsampled.T).T  # for faster tensordot

    # normalize weights of subsampled points
    A_subsampled /= A_subsampled.sum()
    W_subsampled *= np.sqrt(2)
    return W_subsampled.T, np.sqrt(A_subsampled)
Example #35
0
def gauss_hermite(N, mu, sigma):
    """
    This function computes the nodes and weights for gauss-hermite quadrature.
    It is assumed that the variable you are trying to model is distributed
    normally with mean = mu and variance = sigma.

    Parameters
    ----------
    N: number, int
        The number of nodes in the support and weight vectors.

    mu: numnber, float
        The mean of the random variable.

    sigma: number, float
         The standard deviation of the random variable.

    Returns
    -------
    eps: list (array), dtype = float, shape = (N x 1)
        The support vector for the random variable.

    weights: list (array), dtype = float, shape = (N x 1)
        The weights associated with the nodes in eps.

    Notes
    -----
    This function calls numpy.polynomial.hermite.hermgauss to create an initial
    version of the eps, and weights vectors. This function assumes the random
    variable follows the standard normal distribution.

    We take the results of that function and apply the following transformation
    to the nodes (assume eps_numpy are the return values from the numpy func):
        eps = mu + sigma * eps_numpy

    The weights are unchanged in the transformation
    """
    eps_numpy, weights = hermgauss(N)

    eps = mu + sigma * eps_numpy

    # Make sure probabilities sum to 1
    weights /= sum(weights)

    return [eps, weights]
Example #36
0
def integrate_hermgauss_nd(func,mean,Sigma_x,order):
	"""
	n-d Gauss-Hermite quadrature

	:param func: lambda x: y (x: vector of floats)
	:param mean: mean vector of normal weight function
	:param Sigma_x: covariance matrix of normal weight function
	:param order: the order of the integration rule
	:return: :math:`E[f(X)] (X \sim \mathcal{N}(\mu,\sigma^2)) = \int_{-\infty}^{\infty}f(x)p(x),\mathrm{d}x` with p being the normal density
	"""
	from itertools import product
	dim = len(mean)
	mean = np.array(mean)
	sigma = np.array([np.sqrt(Sigma_x[i][i]) for i in range(dim)])

	x,w = hermgauss(order)
	xs = product(x,repeat=dim)
	ws = np.array(list(product(w,repeat=dim)))
	y = []
	sqrt2 = np.sqrt(2)
	for i,x in enumerate(xs):
		y.append(func(x*sigma*sqrt2+mean)*ws[i].prod())
	y = np.array(y)
	return y.sum() / np.sqrt(np.pi)**dim # * 1/(sigma*np.sqrt(2*np.pi)) * sigma * np.sqrt(2)
 def __init__(self, degree):
     self.degree = degree
     self.hermite_samples, self.hermite_weights = hermgauss(degree)
Example #38
0
"""
Created on Tue Jun 25 13:40:30 2013
Holds all of the code for the bellman equation
@author: dgevans
"""

from scipy.optimize import minimize_scalar

from numpy import * 
from mpi4py import MPI
from scipy.interpolate import SmoothBivariateSpline
from numpy.polynomial.hermite import hermgauss

#compute nodes for gaussian quadrature.
zhatvec,z_weights = hermgauss(10)
z_weights /= sum(z_weights)


def approximateValueFunction(TV,Para):
    '''
    Approximates the value function over the grid defined by Para.domain.  Uses
    mpi. Returns both an interpolated value function and the value of TV at each point
    in the domain.
    '''
    comm = MPI.COMM_WORLD
    #first split up domain for each process
    s = comm.Get_size()
    rank = comm.Get_rank()
    n = len(Para.domain)
    m = n//s
Example #39
0
#!/usr/bin/env python2
import os.path
import numpy as np
from numpy.polynomial.legendre import leggauss
from numpy.polynomial.hermite import hermgauss

lg_ns = map(lambda i: 2**i, range(3, 8))
for n in lg_ns:
    (lg_x, lg_w) = leggauss(n)
    fname = os.path.join('matlab', 'lg_weights_%d.csv' % n)
    np.savetxt(fname, np.column_stack((lg_x, lg_w)), delimiter=', ')

hg_ns = map(lambda i: 2**i, range(3, 8))
for n in hg_ns:
    (hg_x, hg_w) = hermgauss(n)
    fname = os.path.join('matlab', 'hg_weights_%d.csv' % n)
    np.savetxt(fname, np.column_stack((hg_x, hg_w)), delimiter=', ')
Example #40
0
#set parameters
mu = 1. #mean of beta
sigma = 0.2 #standard deviationof sigma
delta = 0.1 #exp(-delta) is discount factor
alpha = 0.6 #coefficient on W_{t+1}
theta = 1. #risk sensitivity parameter
kappa = 0.8 #coefficient on $x$

#interpolation region on xprime
min_xprime = -15.
max_xprime = 15.
xprimegrid = linspace(min_xprime,max_xprime,25)

#Compute Gaussian quadrature nodes
#nodes for $W_{t+1}
wvec,w_weights = hermgauss(15)
wvec *= sqrt(2) #sqrt(2) because nodes are for weighting function e^(-x^2)
w_weights /= sum(w_weights)

#Nodes for $\beta$ depends on mu an sigma, so need to set those with function
#set_mu_sigma
def set_mu_sigma(_mu,_sigma):
    global mu,sigma,betavec,beta_weights
    mu = _mu
    sigma = _sigma
    betavec,beta_weights = hermgauss(15)
    betavec= mu + sigma*betavec*sqrt(2)
    beta_weights /= sum(beta_weights)    
set_mu_sigma(mu,sigma)
    
class F: