本文整理汇总了Python中scipy.misc.derivative方法的典型用法代码示例。如果您正苦于以下问题:Python misc.derivative方法的具体用法?Python misc.derivative怎么用?Python misc.derivative使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.misc
的用法示例。
在下文中一共展示了misc.derivative方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_PR78MIX
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def test_PR78MIX():
# Copied and pasted example from PR78.
eos = PR78MIX(Tcs=[632], Pcs=[5350000], omegas=[0.734], zs=[1], T=299., P=1E6)
three_props = [eos.V_l, eos.H_dep_l, eos.S_dep_l]
expect_props = [8.351960066075052e-05, -63764.64948050847, -130.737108912626]
assert_allclose(three_props, expect_props)
# Fugacities
eos = PR78MIX(T=115, P=1E6, Tcs=[126.1, 190.6], Pcs=[33.94E5, 46.04E5], omegas=[0.6, 0.7], zs=[0.5, 0.5], kijs=[[0,0],[0,0]])
# Numerically test fugacities at one point, with artificially high omegas
def numerical_fugacity_coefficient(n1, n2=0.5, switch=False, l=True):
if switch:
n1, n2 = n2, n1
tot = n1+n2
zs = [i/tot for i in [n1,n2]]
a = PR78MIX(T=115, P=1E6, Tcs=[126.1, 190.6], Pcs=[33.94E5, 46.04E5], omegas=[0.6, 0.7], zs=zs, kijs=[[0,0],[0,0]])
phi = a.phi_l if l else a.phi_g
return tot*log(phi)
phis = [[derivative(numerical_fugacity_coefficient, 0.5, dx=1E-6, order=25, args=(0.5, i, j)) for i in [False, True]] for j in [False, True]]
assert_allclose(phis, [eos.lnphis_g, eos.lnphis_l])
示例2: test_UNIFAC_misc
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def test_UNIFAC_misc():
from scipy.misc import derivative
from math import log
T = 273.15 + 60
def gE_T(T):
xs = [0.5, 0.5]
gammas = UNIFAC(chemgroups=[{1:2, 2:4}, {1:1, 2:1, 18:1}], T=T, xs=xs)
return R*T*sum(xi*log(gamma) for xi, gamma in zip(xs, gammas))
def hE_T(T):
to_diff = lambda T: gE_T(T)/T
return -derivative(to_diff, T,dx=1E-5, order=7)*T**2
# A source gives 854.758 for hE, matching to within a gas constant
assert_allclose(hE_T(T), 854.771631451345)
assert_allclose(gE_T(T), 923.6408846044955)
示例3: calculate_engine_temperature_derivatives
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def calculate_engine_temperature_derivatives(
times, engine_coolant_temperatures):
"""
Calculates the derivative of the engine temperature [°C/s].
:param times:
Time vector [s].
:type times: numpy.array
:param engine_coolant_temperatures:
Engine coolant temperature vector [°C].
:type engine_coolant_temperatures: numpy.array
:return:
Derivative of the engine temperature [°C/s].
:rtype: numpy.array
"""
from statsmodels.nonparametric.smoothers_lowess import lowess
par = dfl.functions.calculate_engine_temperature_derivatives
temp = lowess(
engine_coolant_temperatures, times, is_sorted=True,
frac=par.tw * len(times) / (times[-1] - times[0]) ** 2, missing='none'
)[:, 1].ravel()
return _derivative(times, temp)
示例4: interval
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def interval(self, alpha, *args, **kwds):
"""
Confidence interval with equal areas around the median.
Parameters
----------
alpha : array_like of float
Probability that an rv will be drawn from the returned range.
Each value should be in the range [0, 1].
arg1, arg2, ... : array_like
The shape parameter(s) for the distribution (see docstring of the
instance object for more information).
loc : array_like, optional
location parameter, Default is 0.
scale : array_like, optional
scale parameter, Default is 1.
Returns
-------
a, b : ndarray of float
end-points of range that contain ``100 * alpha %`` of the rv's
possible values.
"""
alpha = asarray(alpha)
if np.any((alpha > 1) | (alpha < 0)):
raise ValueError("alpha must be between 0 and 1 inclusive")
q1 = (1.0-alpha)/2
q2 = (1.0+alpha)/2
a = self.ppf(q1, *args, **kwds)
b = self.ppf(q2, *args, **kwds)
return a, b
## continuous random variables: implement maybe later
##
## hf --- Hazard Function (PDF / SF)
## chf --- Cumulative hazard function (-log(SF))
## psf --- Probability sparsity function (reciprocal of the pdf) in
## units of percent-point-function (as a function of q).
## Also, the derivative of the percent-point function.
示例5: _pdf
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def _pdf(self, x, *args):
return derivative(self._cdf, x, dx=1e-5, args=args, order=5)
## Could also define any of these
示例6: norm_lls_grad
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def norm_lls_grad(y, params):
'''Jacobian of normal loglikelihood wrt mean mu and variance sigma2
Parameters
----------
y : array, 1d
normally distributed random variable
params: array, (nobs, 2)
array of mean, variance (mu, sigma2) with observations in rows
Returns
-------
grad : array (nobs, 2)
derivative of loglikelihood for each observation wrt mean in first
column, and wrt variance in second column
Notes
-----
this is actually the derivative wrt sigma not sigma**2, but evaluated
with parameter sigma2 = sigma**2
'''
mu, sigma2 = params.T
dllsdmu = (y-mu)/sigma2
dllsdsigma2 = ((y-mu)**2/sigma2 - 1)/np.sqrt(sigma2)
return np.column_stack((dllsdmu, dllsdsigma2))
示例7: normgrad
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def normgrad(y, x, params):
'''Jacobian of normal loglikelihood wrt mean mu and variance sigma2
Parameters
----------
y : array, 1d
normally distributed random variable with mean x*beta, and variance sigma2
x : array, 2d
explanatory variables, observation in rows, variables in columns
params: array_like, (nvars + 1)
array of coefficients and variance (beta, sigma2)
Returns
-------
grad : array (nobs, 2)
derivative of loglikelihood for each observation wrt mean in first
column, and wrt scale (sigma) in second column
assume params = (beta, sigma2)
Notes
-----
TODO: for heteroscedasticity need sigma to be a 1d array
'''
beta = params[:-1]
sigma2 = params[-1]*np.ones((len(y),1))
dmudbeta = mean_grad(x, beta)
mu = np.dot(x, beta)
#print(beta, sigma2)
params2 = np.column_stack((mu,sigma2))
dllsdms = norm_lls_grad(y,params2)
grad = np.column_stack((dllsdms[:,:1]*dmudbeta, dllsdms[:,:1]))
return grad
示例8: norm_dlldy
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def norm_dlldy(y):
'''derivative of log pdf of standard normal with respect to y
'''
return -y
示例9: ts_dlldy
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def ts_dlldy(y, df):
'''derivative of log pdf of standardized (?) t with respect to y
Notes
-----
parameterized for garch, with mean 0 and variance 1
'''
#(df+1)/2. / (1 + y**2/(df-2.)) * 2.*y/(df-2.)
#return -(df+1)/(df-2.) / (1 + y**2/(df-2.)) * y
return -(df+1)/(df) / (1 + y**2/(df)) * y
示例10: locscale_grad
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def locscale_grad(y, loc, scale, dlldy, *args):
'''derivative of log-likelihood with respect to location and scale
Parameters
----------
y : array_like
data points of random variable at which loglike is evaluated
loc : float
location parameter of distribution
scale : float
scale parameter of distribution
dlldy : function
derivative of loglikelihood fuction wrt. random variable x
args : array_like
shape parameters of log-likelihood function
Returns
-------
dlldloc : array
derivative of loglikelihood wrt location evaluated at the
points given in y
dlldscale : array
derivative of loglikelihood wrt scale evaluated at the
points given in y
'''
yst = (y-loc)/scale #ystandardized
dlldloc = -dlldy(yst, *args) / scale
dlldscale = -1./scale - dlldy(yst, *args) * (y-loc)/scale**2
return dlldloc, dlldscale
示例11: isobaric_expansion
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def isobaric_expansion(V, dV_dT):
r'''Calculate the isobaric coefficient of a thermal expansion, given its
molar volume at a certain `T` and `P`, and its derivative of molar volume
with respect to `T`.
.. math::
\beta = \frac{1}{V}\left(\frac{\partial V}{\partial T} \right)_P
Parameters
----------
V : float
Molar volume at `T` and `P`, [m^3/mol]
dV_dT : float
Derivative of molar volume with respect to `T`, [m^3/mol/K]
Returns
-------
beta : float
Isobaric coefficient of a thermal expansion, [1/K]
Notes
-----
For an ideal gas, this expression simplified to:
.. math::
\beta = \frac{1}{T}
Examples
--------
Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid):
>>> isobaric_expansion(0.000130229900873546, 1.58875261849113e-7)
0.0012199599384121608
References
----------
.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition.
New York: McGraw-Hill Professional, 2000.
'''
return dV_dT/V
示例12: isothermal_compressibility
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def isothermal_compressibility(V, dV_dP):
r'''Calculate the isothermal coefficient of a compressibility, given its
molar volume at a certain `T` and `P`, and its derivative of molar volume
with respect to `P`.
.. math::
\kappa = -\frac{1}{V}\left(\frac{\partial V}{\partial P} \right)_T
Parameters
----------
V : float
Molar volume at `T` and `P`, [m^3/mol]
dV_dP : float
Derivative of molar volume with respect to `P`, [m^3/mol/Pa]
Returns
-------
kappa : float
Isothermal coefficient of a compressibility, [1/Pa]
Notes
-----
For an ideal gas, this expression simplified to:
.. math::
\kappa = \frac{1}{P}
Examples
--------
Calculated for hexane from the PR EOS at 299 K and 1 MPa (liquid):
>>> isothermal_compressibility(0.000130229900873546, -2.72902118209903e-13)
2.095541165119158e-09
References
----------
.. [1] Poling, Bruce E. The Properties of Gases and Liquids. 5th edition.
New York: McGraw-Hill Professional, 2000.
'''
return -dV_dP/V
示例13: calculate_derivative
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def calculate_derivative(self, T, method, order=1):
r'''Method to calculate a derivative of a property with respect to
temperature, of a given order using a specified method. Uses SciPy's
derivative function, with a delta of 1E-6 K and a number of points
equal to 2*order + 1.
This method can be overwritten by subclasses who may perfer to add
analytical methods for some or all methods as this is much faster.
If the calculation does not succeed, returns the actual error
encountered.
Parameters
----------
T : float
Temperature at which to calculate the derivative, [K]
method : str
Method for which to find the derivative
order : int
Order of the derivative, >= 1
Returns
-------
derivative : float
Calculated derivative property, [`units/K^order`]
'''
return derivative(self.calculate, T, dx=1e-6, args=[method], n=order, order=1+order*2)
示例14: T_dependent_property_derivative
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def T_dependent_property_derivative(self, T, order=1):
r'''Method to obtain a derivative of a property with respect to
temperature, of a given order. Methods found valid by
`select_valid_methods` are attempted until a method succeeds. If no
methods are valid and succeed, None is returned.
Calls `calculate_derivative` internally to perform the actual
calculation.
.. math::
\text{derivative} = \frac{d (\text{property})}{d T}
Parameters
----------
T : float
Temperature at which to calculate the derivative, [K]
order : int
Order of the derivative, >= 1
Returns
-------
derivative : float
Calculated derivative property, [`units/K^order`]
'''
if self.method:
# retest within range
if self.test_method_validity(T, self.method):
try:
return self.calculate_derivative(T, self.method, order)
except: # pragma: no cover
pass
sorted_valid_methods = self.select_valid_methods(T)
for method in sorted_valid_methods:
try:
return self.calculate_derivative(T, method, order)
except:
pass
return None
示例15: calculate_derivative_P
# 需要导入模块: from scipy import misc [as 别名]
# 或者: from scipy.misc import derivative [as 别名]
def calculate_derivative_P(self, P, T, method, order=1):
r'''Method to calculate a derivative of a temperature and pressure
dependent property with respect to pressure at constant temperature,
of a given order using a specified method. Uses SciPy's derivative
function, with a delta of 0.01 Pa and a number of points equal to
2*order + 1.
This method can be overwritten by subclasses who may perfer to add
analytical methods for some or all methods as this is much faster.
If the calculation does not succeed, returns the actual error
encountered.
Parameters
----------
P : float
Pressure at which to calculate the derivative, [Pa]
T : float
Temperature at which to calculate the derivative, [K]
method : str
Method for which to find the derivative
order : int
Order of the derivative, >= 1
Returns
-------
d_prop_d_P_at_T : float
Calculated derivative property at constant temperature,
[`units/Pa^order`]
'''
f = lambda P: self.calculate_P(T, P, method)
return derivative(f, P, dx=1e-2, n=order, order=1+order*2)