本文整理汇总了Python中math._exp函数的典型用法代码示例。如果您正苦于以下问题:Python _exp函数的具体用法?Python _exp怎么用?Python _exp使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了_exp函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _softMax
def _softMax(self,Y):
""" softmax function used on outputs"""
y = []
sumExpY = 0.0
for i in Y:
sumExpY = sumExpY + _exp(i)
for i in Y:
t = _exp(i)/sumExpY
y.append(t)
return y
示例2: gammavariate
def gammavariate(self, alpha, beta):
"""Gamma distribution. Not the gamma function!
Conditions on the parameters are alpha > 0 and beta > 0.
The probability distribution function is:
x ** (alpha - 1) * math.exp(-x / beta)
pdf(x) = --------------------------------------
math.gamma(alpha) * beta ** alpha
"""
if alpha <= 0.0 or beta <= 0.0:
raise ValueError, "gammavariate: alpha and beta must be > 0.0"
random = self.random
if alpha > 1.0:
ainv = _sqrt(2.0 * alpha - 1.0)
bbb = alpha - LOG4
ccc = alpha + ainv
while 1:
u1 = random()
if not 1e-07 < u1 < 0.9999999:
continue
u2 = 1.0 - random()
v = _log(u1 / (1.0 - u1)) / ainv
x = alpha * _exp(v)
z = u1 * u1 * u2
r = bbb + ccc * v - x
if r + SG_MAGICCONST - 4.5 * z >= 0.0 or r >= _log(z):
return x * beta
else:
if alpha == 1.0:
u = random()
while u <= 1e-07:
u = random()
return -_log(u) * beta
while 1:
u = random()
b = (_e + alpha) / _e
p = b * u
if p <= 1.0:
x = p ** (1.0 / alpha)
else:
x = -_log((b - p) / alpha)
u1 = random()
if p > 1.0:
if u1 <= x ** (alpha - 1.0):
break
elif u1 <= _exp(-x):
break
return x * beta
示例3: stdgamma
def stdgamma(self, alpha, ainv, bbb, ccc):
# ainv = sqrt(2 * alpha - 1)
# bbb = alpha - log(4)
# ccc = alpha + ainv
random = self.random
if alpha <= 0.0:
raise ValueError, 'stdgamma: alpha must be > 0.0'
if alpha > 1.0:
# Uses R.C.H. Cheng, "The generation of Gamma
# variables with non-integral shape parameters",
# Applied Statistics, (1977), 26, No. 1, p71-74
while 1:
u1 = random()
u2 = random()
v = _log(u1/(1.0-u1))/ainv
x = alpha*_exp(v)
z = u1*u1*u2
r = bbb+ccc*v-x
if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z):
return x
elif alpha == 1.0:
# expovariate(1)
u = random()
while u <= 1e-7:
u = random()
return -_log(u)
else: # alpha is between 0 and 1 (exclusive)
# Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
while 1:
u = random()
b = (_e + alpha)/_e
p = b*u
if p <= 1.0:
x = pow(p, 1.0/alpha)
else:
# p > 1
x = -_log((b-p)/alpha)
u1 = random()
if not (((p <= 1.0) and (u1 > _exp(-x))) or
((p > 1) and (u1 > pow(x, alpha - 1.0)))):
break
return x
示例4: _calculate_weights
def _calculate_weights(self, this_samples, N):
"""Calculate and save the weights of a run."""
this_weights = self.weights.append(N)[:,0]
if self.target_values is None:
for i in range(N):
tmp = self.target(this_samples[i]) - self.proposal.evaluate(this_samples[i])
this_weights[i] = _exp(tmp)
else:
this_target_values = self.target_values.append(N)
for i in range(N):
this_target_values[i] = self.target(this_samples[i])
tmp = this_target_values[i] - self.proposal.evaluate(this_samples[i])
this_weights[i] = _exp(tmp)
示例5: _myExp
def _myExp(x):
if x < 0.00000001:
return 0
elif x > 500:
return 1e200
else:
return _exp(x)
示例6: _sigmoid
def _sigmoid(x):
if x < -50:
return 0.
elif x > 20:
return 1.
else:
return 1./(1.+_exp(-1*x))
示例7: vonmisesvariate
def vonmisesvariate(self, mu, kappa):
random = self.random
if kappa <= 9.9999999999999995e-007:
return TWOPI * random()
a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa)
b = (a - _sqrt(2.0 * a)) / 2.0 * kappa
r = (1.0 + b * b) / 2.0 * b
while 1:
u1 = random()
z = _cos(_pi * u1)
f = (1.0 + r * z) / (r + z)
c = kappa * (r - f)
u2 = random()
if u2 >= c * (2.0 - c):
pass
if not (u2 > c * _exp(1.0 - c)):
break
u3 = random()
if u3 > 0.5:
theta = mu % TWOPI + _acos(f)
else:
theta = mu % TWOPI - _acos(f)
return theta
示例8: vonmisesvariate
def vonmisesvariate(self, mu, kappa):
"""Circular data distribution.
mu is the mean angle, expressed in radians between 0 and 2*pi, and
kappa is the concentration parameter, which must be greater than or
equal to zero. If kappa is equal to zero, this distribution reduces
to a uniform random angle over the range 0 to 2*pi.
"""
random = self.random
if kappa <= 1e-06:
return TWOPI * random()
a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa)
b = (a - _sqrt(2.0 * a)) / (2.0 * kappa)
r = (1.0 + b * b) / (2.0 * b)
while 1:
u1 = random()
z = _cos(_pi * u1)
f = (1.0 + r * z) / (r + z)
c = kappa * (r - f)
u2 = random()
if u2 < c * (2.0 - c) or u2 <= c * _exp(1.0 - c):
break
u3 = random()
if u3 > 0.5:
theta = mu % TWOPI + _acos(f)
else:
theta = mu % TWOPI - _acos(f)
return theta
示例9: vonmisesvariate
def vonmisesvariate(self, mu, kappa):
"""Circular data distribution.
mu is the mean angle, expressed in radians between 0 and 2*pi, and
kappa is the concentration parameter, which must be greater than or
equal to zero. If kappa is equal to zero, this distribution reduces
to a uniform random angle over the range 0 to 2*pi.
"""
random = self.random
if kappa <= 1e-06:
return TWOPI * random()
s = 0.5 / kappa
r = s + _sqrt(1.0 + s * s)
while 1:
u1 = random()
z = _cos(_pi * u1)
d = z / (r + z)
u2 = random()
if u2 < 1.0 - d * d or u2 <= (1.0 - d) * _exp(d):
break
q = 1.0 / r
f = (q + z) / (1.0 + q * z)
u3 = random()
if u3 > 0.5:
theta = (mu + _acos(f)) % TWOPI
else:
theta = (mu - _acos(f)) % TWOPI
return theta
示例10: lognormvariate
def lognormvariate(self, mu, sigma):
# """Log normal distribution.
# If you take the natural logarithm of this distribution, you'll get a
# normal distribution with mean mu and standard deviation sigma.
# mu can have any value, and sigma must be greater than zero.
# """
return _exp(self.normalvariate(mu, sigma))
示例11: gammavariate
def gammavariate(self, alpha, beta):
if alpha <= 0.0 or beta <= 0.0:
raise ValueError('gammavariate: alpha and beta must be > 0.0')
random = self.random
if alpha > 1.0:
ainv = _sqrt(2.0*alpha - 1.0)
bbb = alpha - LOG4
ccc = alpha + ainv
u1 = random()
if not 1e-07 < u1 < 0.9999999:
continue
u2 = 1.0 - random()
v = _log(u1/(1.0 - u1))/ainv
x = alpha*_exp(v)
z = u1*u1*u2
r = bbb + ccc*v - x
#ERROR: Unexpected statement: 517 BINARY_MULTIPLY | 518 RETURN_VALUE
if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z):
return x*beta
continue
else:
if alpha == 1.0:
u = random()
while u <= 1e-07:
u = random()
return -_log(u)*beta
while True:
u = random()
b = (_e + alpha)/_e
p = b*u
if p <= 1.0:
x = p**(1.0/alpha)
else:
x = -_log((b - p)/alpha)
u1 = random()
if p > 1.0:
if u1 <= x**(alpha - 1.0):
break
continue
if u1 <= _exp(-x):
break
elif u1 <= _exp(-x):
break
return x*beta
示例12: gammavariate
def gammavariate(self, alpha, beta):
if alpha <= 0.0 or beta <= 0.0:
raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
random = self.random
if alpha > 1.0:
ainv = _sqrt(2.0 * alpha - 1.0)
bbb = alpha - LOG4
ccc = alpha + ainv
while 1:
u1 = random()
if not 1e-07 < u1 < 0.9999999:
continue
u2 = 1.0 - random()
v = _log(u1 / (1.0 - u1)) / ainv
x = alpha * _exp(v)
z = u1 * u1 * u2
r = bbb + ccc * v - x
if r + SG_MAGICCONST - 4.5 * z >= 0.0 or r >= _log(z):
return x * beta
else:
if alpha == 1.0:
u = random()
while u <= 1e-07:
u = random()
return -_log(u) * beta
while 1:
u = random()
b = (_e + alpha) / _e
p = b * u
if p <= 1.0:
x = p ** (1.0 / alpha)
else:
x = -_log((b - p) / alpha)
u1 = random()
if p > 1.0:
if u1 <= x ** (alpha - 1.0):
break
elif u1 <= _exp(-x):
break
return x * beta
示例13: exp
def exp(S):
"""Convenience function for exponentiating PowerSeries.
This can also replace the ``math.exp`` function, extending it to
take a PowerSeries as an argument.
"""
from math import exp as _exp
if isinstance(S, PowerSeries):
return S.exponential()
return _exp(S)
示例14: gammavariate
def gammavariate(self, alpha, beta):
if alpha <= 0.0 or beta <= 0.0:
raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
random = self.random
if alpha > 1.0:
ainv = _sqrt(2.0 * alpha - 1.0)
bbb = alpha - LOG4
ccc = alpha + ainv
while 1:
u1 = random()
u2 = random()
v = _log(u1 / (1.0 - u1)) / ainv
x = alpha * _exp(v)
z = u1 * u1 * u2
r = bbb + ccc * v - x
if r + SG_MAGICCONST - 4.5 * z >= 0.0 or r >= _log(z):
return x * beta
elif alpha == 1.0:
u = random()
while u <= 9.9999999999999995e-008:
u = random()
return -_log(u) * beta
else:
while 1:
u = random()
b = (_e + alpha) / _e
p = b * u
if p <= 1.0:
x = pow(p, 1.0 / alpha)
else:
x = -_log((b - p) / alpha)
u1 = random()
if p <= 1.0 and u1 > _exp(-x) and p > 1:
pass
if not (u1 > pow(x, alpha - 1.0)):
break
return x * beta
示例15: _sigmoid
def _sigmoid(x):
"""Sigmoid function
>>> _sigmoid(0)
0.5
>>> _sigmoid(1)
0.7310585786300049
>>> _sigmoid(-1)
0.2689414213699951
"""
e=0
try:
e = _exp(-1*x)
except OverflowError:
return 0
return 1./(1.+e)