Example #1
0
    def _cdf(self, x, gamma):
        cte = np.sqrt(
            np.pi) * gamma_function(gamma - 0.5) / gamma_function(gamma)

        a = hyp2f1(0.5, gamma, 1.5, -x**2)

        return 0.5 + x * (a / cte)
Example #2
0
def compute_matrix_element(gamma):
    betai = gamma**2 / 2.0
    betaf = np.sqrt(1.0 + gamma**4 / 4.0)
    integral = np.pi * quad(func, 0, np.infty)[0]
    first_sqrt = (1.0 / (2.0**betai * gamma_function(betai + 1)))**0.5
    second_sqrt = (1.0 / (2.0**betaf * gamma_function(betaf + 1)))**0.5
    return R * gamma**2 / 2.0 / np.pi * first_sqrt * second_sqrt * integral
Example #3
0
	def _logpdf3d(self, x, loc, U, log_pdet, gamma,dim):
		# U is inverse Cholesky, so only one product U*x instead of sqrt(x*U*U.T*x)
		quaddist = np.square(np.dot(x - loc,U)).sum(axis=-1)

		a = 1.5*np.log(np.pi)
		b = np.log(gamma_function(0.5*(gamma-3.)))
		c = np.log(gamma_function(0.5*gamma))
		log_cte = a + b -c
		log_d = -0.5*gamma*np.log1p(quaddist) - log_cte - 3.*log_pdet
		
		return log_d
Example #4
0
    def levy(self):
        temp = np.power(
            ((gamma_function(1 + self.lamda) * np.sin(np.pi *
                                                      (self.lamda / 2))) /
             (gamma_function((1 + self.lamda) / 2) * self.lamda *
              np.power(2, ((self.lamda - 1) / 2)))), 1 / self.lamda)
        u = np.random.normal(0, temp)
        v = np.random.normal(0, 1)
        r = u / (np.power(abs(v), (1 / self.lamda)))

        return r  # random walk value
Example #5
0
	def _cdf3d(self, x, rc, gamma):
		# use only to generate random variates
		a = x**2 + rc**2
		hg = gamma_function(0.5*gamma)
		hg3 = gamma_function(0.5*(gamma-3.))
		hyp = hyp2f1(-0.5,0.5*gamma,0.5,-(x/rc)**2)
		b = -(rc**gamma)*a*(rc**2 + (gamma-1.)*(x**2))
		c = (rc**4)*(a**(0.5*gamma))*hyp

		up = 4.*(a**(-0.5*gamma))*hg*( b + c)
		down = (gamma-3.)*(gamma-1.)*np.sqrt(np.pi)*(rc**3)*x*hg3

		result = up/down
		return result
Example #6
0
    def kernelKNN(self, xi, xj):
        """
		kNN kernel
		"""
        dim = len(xi)
        if self.volume is None:
            self.volume = np.pi**(dim // 2) / gamma_function(dim // 2 + 1)
        dist = np.linalg.norm(xi - xj, 2)
        density_at_mean = self.knnK / (self.Num_points * self.volume) * dist
        return density_at_mean
Example #7
0
def weibull_fit(x):
    '''Fit Weibull parameters using PWM.

    Return (shape, location, scale).

    See section 2.2.3.1 of [1] and [2].
    '''
    M0 = M(0, x)
    M1 = M(1, x)
    M2 = M(2, x)
    M3 = M(3, x)

    delta = (4 * (M0 * (3 * M2 - M3 - M1) - M1**2) /
             (M0 - 8 * M1 + 12 * M2 - 4 * M3))

    gamma = log(2) / log((2 * M1 - M0) / (2 * (5 * M1 - M0 - 6 * M2 + 2 * M3)))

    beta = (M0 - delta) / gamma_function(1 + 1 / gamma)

    return gamma, delta, beta
Example #8
0
def normal_inv_gamma(alpha, beta, delta, gamma, mu, sigma):
    """Return the probability density function for the normal
    inverse gamma density at (mu, sigma)
    
    Args:
        alpha: shape of variance
        beta: scale of variance
        delta: mean of mu
        gamma: precision of mu
        mu: normal mean
        sigma: normal standard deviation
    Returns:
        a probability density function
    """
    # You will find scipy.special.gamma useful
    mantissa_part = (np.sqrt(gamma) / sigma * np.sqrt(2 * np.pi)) * (
        beta**alpha / gamma_function(alpha)) * (1 / sigma**2)**(alpha + 1)
    exponent_part = -1 * (2 * beta + gamma * ((delta - mu)**2)) / 2 * sigma**2
    pdf = mantissa_part * np.exp(exponent_part)

    return pdf
Example #9
0
def weibull_fit(x):
    '''Fit Weibull parameters using PWM.

    Return (shape, location, scale).

    See section 2.2.3.1 of [1] and [2].
    '''
    M0 = M(0, x)
    M1 = M(1, x)
    M2 = M(2, x)
    M3 = M(3, x)

    delta = (4 * (M0 * (3 * M2 - M3 - M1) - M1**2) /
             (M0 - 8*M1 + 12*M2 - 4*M3))

    gamma = log(2) / log((2*M1 - M0) /
                         (2 * (5*M1 - M0 - 6*M2 + 2*M3)))

    beta = (M0 - delta) / gamma_function(1 + 1/gamma)

    return gamma, delta, beta
Example #10
0
def gaussian_square(n, t, fwhm):
    """We obtain a normalized function of the form

         2⋅n
      ⎛t⎞
     -⎜─⎟
      ⎝τ⎠
     ────────
        2
    ℯ

    with the given (amplitude square) fwhm.
    """
    if str(n) == "oo":
        return heaviside_pi(t / fwhm) / np.sqrt(fwhm)
    elif n < 1:
        raise ValueError
    else:
        tau = fwhm * np.log(2)**(-1 / (2.0 * n)) / 2
        X = t / tau
        norm = np.sqrt(2 * tau * gamma_function(1 + 1 / (2.0 * n)))
        return np.exp(-X**(2 * n) / 2) / norm
def normal_inv_gamma(alpha, beta, delta, gamma, mu, sigma):
    """Return the probability density function for the normal
    inverse gamma density at (mu, sigma)
    
    Args:
        alpha: shape of variance
        beta: scale of variance
        delta: mean of mu
        gamma: precision of mu
        mu: normal mean
        sigma: normal standard deviation
    Returns:
        a probability density function
    """
    # You will find scipy.special.gamma useful

    t1 = np.sqrt(gamma) / (sigma * np.sqrt(2 * np.pi))
    t2 = beta**alpha / gamma_function(alpha)
    t3 = (1 / (sigma**2))**(alpha + 1)
    t4 = np.exp(-(2 * beta + gamma * ((delta - mu)**2)) / (2 * (sigma**2)))

    return t1 * t2 * t3 * t4
Example #12
0
def _variance_power_folded_norm(a, sigma=1):
    # Calculates E[X^2] where X ~ power normal distribution (a)
    return (2**(a+0.5) * sigma**(2*a) * gamma_function(a + 0.5)) / np.sqrt(2*np.pi)
Example #13
0
    def _pdf(self, x, gamma):

        cte = np.sqrt(
            np.pi) * gamma_function(gamma - 0.5) / gamma_function(gamma)
        nx = (1. + x**2)**(-gamma)
        return nx / cte
Example #14
0
 def dirichlet_pdf_log(self, x, alpha):
     return np.sum(np.log(np.power(x, alpha - 1))) - np.sum(
         np.log(gamma_function(alpha))) + np.log(
             gamma_function(np.sum(alpha)))
Example #15
0
	def _pdf(self,x,gamma):

		cte = np.sqrt(np.pi)*gamma_function(0.5*(gamma-1.))/gamma_function(gamma/2.)
		nx = (1. + x**2)**(-0.5*gamma)
		return nx/cte
Example #16
0
def mean_power_folded_norm(a, sigma=1):
    # Calculates the mean of a power folded normal distribution
    return (2**((a+1)/2) * sigma**a * gamma_function((a+1)/2)) / np.sqrt(2*np.pi)
Example #17
0
def _gamma_pdf(x, alpha=1.0, beta=1.0):
    # Simple wrapper for the scipy gamma function
    top = beta**alpha * x**(alpha - 1) * np.exp(-beta * x)
    return top / gamma_function(alpha)
               (2 * np.square(sigma[i]))) for i in range(len(p))
    ], 1), 1)
accuracy_loss = -1 * tf.reduce_mean(f)

# calculate entropy estimation
mutual_squared_distances = tf.reduce_sum(
    tf.square(tf.expand_dims(y, 0) - tf.expand_dims(y, 1)),
    2,
    name='mutual_squared_distances')
nearest_distances = tf.sqrt(
    -1 * tf.nn.top_k(-1 * mutual_squared_distances, k=2)[0][:, 1] + 1e-7,
    name='nearest_distances')
entropy_estimate = tf.identity(
    tf.reduce_mean(tf.log(nearest_distances + 1e-7)) +
    tf.digamma(tf.cast(train_batch_size, tf.float32)) + np.euler_gamma +
    (0.5) * np.log(np.pi) - np.log(gamma_function(1 + 1 / 2)),
    name='entropy_estimate')
diversity_loss = -1 * entropy_estimate

# this is the l2 regularization
gauge_fixing = tf.reduce_mean(tf.reduce_sum(tf.square(y - 0.0), 1),
                              name='gauge_fixing')

loss = accuracy_loss + lamda * diversity_loss + gamma * gauge_fixing

learning_rate_ = tf.Variable(learning_rate,
                             dtype=tf.float32,
                             trainable=False,
                             name='learning_rate')
update_learning_rate = tf.assign(learning_rate_,
                                 learning_rate_ * learning_rate_rate,
Example #19
0
def mf(x):
    V1 = p0 * beta * (
        x / M0)**alpha * np.exp(-(x / M0)**beta) / gamma_function(alpha / beta)
    V2 = pg * (x / Mg)**gamma * np.exp(-(x / Mg))
    return V1 + V2