本文整理汇总了Python中scipy.stats.chi2.ppf方法的典型用法代码示例。如果您正苦于以下问题:Python chi2.ppf方法的具体用法?Python chi2.ppf怎么用?Python chi2.ppf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类scipy.stats.chi2
的用法示例。
在下文中一共展示了chi2.ppf方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _ci_limits_var
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def _ci_limits_var(self, var):
"""
Used to determine the confidence intervals for the variance.
It calls test_var and when called by an optimizer,
finds the value of sig2_0 that is chi2.ppf(significance-level)
Parameters
----------
var_test : float
Hypothesized value of the variance
Returns
-------
diff : float
The difference between the log likelihood ratio at var_test and a
pre-specified value.
"""
return self.test_var(var)[0] - self.r0
示例2: visualize_pruning
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def visualize_pruning(w_norm, n_retained,
title='Initial model weights vs theoretical for pruning'):
fig, ax1 = plt.subplots()
ax1.set_title(title)
ax1.hist(w_norm, normed=True, bins=200, alpha=0.6, histtype='stepfilled',
range=[0, n_retained * 5])
ax1.axvline(x=n_retained, linewidth=1, color='r')
ax1.set_ylabel('PDF', color='b')
ax2 = ax1.twinx()
ax2.set_ylabel('Survival Function', color='r')
ax1.set_xlabel('w_norm')
x = np.linspace(chi2.ppf(0.001, n_retained),
chi2.ppf(0.999, n_retained), 100)
ax2.plot(x, chi2.sf(x, n_retained),
'g-', lw=1, alpha=0.6, label='chi2 pdf')
ax1.plot(x, chi2.pdf(x, n_retained),
'r-', lw=1, alpha=0.6, label='chi2 pdf')
示例3: _fisher_confint
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def _fisher_confint(self, alpha: float, observed: bool = False) -> List[float]:
"""Compute the Fisher information confidence interval for the MLE of the previous run.
Args:
alpha: Specifies the (1 - alpha) confidence level (0 < alpha < 1).
observed: If True, the observed Fisher information is used to construct the
confidence interval, otherwise the expected Fisher information.
Returns:
The Fisher information confidence interval.
"""
shots = self._ret['shots']
mle = self._ret['ml_value']
# approximate the standard deviation of the MLE and construct the confidence interval
std = np.sqrt(shots * self._compute_fisher_information(observed))
ci = mle + norm.ppf(1 - alpha / 2) / std * np.array([-1, 1])
# transform the confidence interval from [0, 1] to the target interval
return [self.a_factory.value_to_estimation(bound) for bound in ci]
示例4: _h_getMahalanobisRobust
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def _h_getMahalanobisRobust(dat, critical_alpha=0.01, good_rows=np.zeros(0)):
'''Calculate the Mahalanobis distance from the sample vector.'''
if good_rows.size == 0:
good_rows = np.any(~np.isnan(dat), axis=1)
try:
dat2fit = dat[good_rows]
assert not np.any(np.isnan(dat2fit))
robust_cov = MinCovDet().fit(dat2fit)
mahalanobis_dist = np.sqrt(robust_cov.mahalanobis(dat))
except ValueError:
# this step will fail if the covariance matrix is not singular. This happens if the data is not
# a unimodal symetric distribution. For example there is too many small noisy particles. Therefore
# I will take a safe option and return zeros in the mahalanobis
# distance if this is the case.
mahalanobis_dist = np.zeros(dat.shape[0])
# critial distance of the maholanobis distance using the chi-square distirbution
# https://en.wikiversity.org/wiki/Mahalanobis%27_distance
# http://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.chi2.html
maha_lim = chi2.ppf(1 - critical_alpha, dat.shape[1])
outliers = mahalanobis_dist > maha_lim
return mahalanobis_dist, outliers, maha_lim
示例5: chi2_cutoff
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def chi2_cutoff(D, cutoff=3.):
"""D-dimensional eqiuvalent of "n sigma" cut.
Evaluates the quantile function of the chi-squared distribution to determine
the limit for the chi^2 of samples wrt to GMM so that they satisfy the
68-95-99.7 percent rule of the 1D Normal distribution.
Args:
D (int): dimensions of the feature space
cutoff (float): 1D equivalent cut [in units of sigma]
Returns:
float: upper limit for chi-squared in D dimensions
"""
import scipy.stats
cdf_1d = scipy.stats.norm.cdf(cutoff)
confidence_1d = 1-(1-cdf_1d)*2
cutoff_nd = scipy.stats.chi2.ppf(confidence_1d, D)
return cutoff_nd
示例6: recover_spikes
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def recover_spikes(self, vbParam, pca, maha_dist=1):
N, D = pca.shape
# Cat: TODO: check if this maha thresholding recovering distance is good
threshold = np.sqrt(chi2.ppf(0.99, D))
# update rhat on full data
maskedData = mfm.maskData(pca[:,:,np.newaxis], np.ones([N, 1]), np.arange(N))
vbParam.update_local(maskedData)
# calculate mahalanobis distance
maha = mfm.calc_mahalonobis(vbParam, pca[:,:,np.newaxis])
idx_recovered = np.where(~np.all(maha >= threshold, axis=1))[0]
vbParam.rhat = vbParam.rhat[idx_recovered]
# zero out low assignment vals
if True:
vbParam.rhat[vbParam.rhat < self.assignment_delete_threshold] = 0
vbParam.rhat = vbParam.rhat/np.sum(vbParam.rhat,
1, keepdims=True)
return idx_recovered, vbParam
示例7: get_robust_scaling
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def get_robust_scaling(scale_typ, model, ds, circuitList, parameters, evaltree_cache, comm, memLimit):
"""
Get the per-circuit data scaling ("weights") for a given type of robust-data-scaling.
TODO: docstring - more details
"""
fitQty = _get_fit_qty(model, ds, circuitList, parameters, evaltree_cache, comm, memLimit)
#Note: fitQty[iCircuit] gives fit quantity for a single circuit, aggregated over outcomes.
expected = (len(ds.get_outcome_labels()) - 1) # == "k"
dof_per_box = expected; nboxes = len(circuitList)
pc = 0.05 # hardcoded (1 - confidence level) for now -- make into advanced option w/default
circuitWeights = {}
if scale_typ in ("robust", "Robust"):
# Robust scaling V1: drastically scale down weights of especially bad sequences
threshold = _np.ceil(_chi2.ppf(1 - pc / nboxes, dof_per_box))
for i, opstr in enumerate(circuitList):
if fitQty[i] > threshold:
circuitWeights[opstr] = expected / fitQty[i] # scaling factor
elif scale_typ in ("robust+", "Robust+"):
# Robust scaling V2: V1 + rescale to desired chi2 distribution without reordering
threshold = _np.ceil(_chi2.ppf(1 - pc / nboxes, dof_per_box))
scaled_fitQty = fitQty.copy()
for i, opstr in enumerate(circuitList):
if fitQty[i] > threshold:
circuitWeights[opstr] = expected / fitQty[i] # scaling factor
scaled_fitQty[i] = expected # (fitQty[i]*circuitWeights[opstr])
N = len(fitQty)
percentiles = [_chi2.ppf((i + 1) / (N + 1), dof_per_box) for i in range(N)]
for iBin, i in enumerate(_np.argsort(scaled_fitQty)):
opstr = circuitList[i]
fit, expected = scaled_fitQty[i], percentiles[iBin]
if fit > expected:
if opstr in circuitWeights: circuitWeights[opstr] *= expected / fit
else: circuitWeights[opstr] = expected / fit
return circuitWeights
示例8: ci_corr
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def ci_corr(self, sig=.05, upper_bound=None, lower_bound=None):
"""
Returns the confidence intervals for the correlation coefficient
Parameters
----------
sig : float
The significance level. Default is .05
upper_bound : float
Maximum value the upper confidence limit can be.
Default is 99% confidence limit assuming normality.
lower_bound : float
Minimum value the lower condidence limit can be.
Default is 99% confidence limit assuming normality.
Returns
-------
interval : tuple
Confidence interval for the correlation
"""
endog = self.endog
nobs = self.nobs
self.r0 = chi2.ppf(1 - sig, 1)
point_est = np.corrcoef(endog[:, 0], endog[:, 1])[0, 1]
if upper_bound is None:
upper_bound = min(.999, point_est + \
2.5 * ((1. - point_est ** 2.) / \
(nobs - 2.)) ** .5)
if lower_bound is None:
lower_bound = max(- .999, point_est - \
2.5 * (np.sqrt((1. - point_est ** 2.) / \
(nobs - 2.))))
llim = optimize.brenth(self._ci_limits_corr, lower_bound, point_est)
ulim = optimize.brenth(self._ci_limits_corr, point_est, upper_bound)
return llim, ulim
示例9: _fisher_confint
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def _fisher_confint(self, alpha: float = 0.05, observed: bool = False) -> List[float]:
"""Compute the `alpha` confidence interval based on the Fisher information.
Args:
alpha: The level of the confidence interval (must be <= 0.5), default to 0.05.
observed: If True, use observed Fisher information.
Returns:
float: The alpha confidence interval based on the Fisher information
Raises:
AssertionError: Call run() first!
"""
# Get the (observed) Fisher information
fisher_information = None
try:
fisher_information = self._ret['fisher_information']
except KeyError:
raise AssertionError("Call run() first!")
if observed:
fisher_information = self._compute_fisher_information(observed=True)
normal_quantile = norm.ppf(1 - alpha / 2)
confint = np.real(self._ret['value']) + \
normal_quantile / np.sqrt(fisher_information) * np.array([-1, 1])
mapped_confint = [self.a_factory.value_to_estimation(bound) for bound in confint]
return mapped_confint
示例10: _get_unsorted_edges
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def _get_unsorted_edges():
"""P10 - P90 unsorted edge coordinates"""
retval = {"low": chi2.ppf(0.1, 1), "high": chi2.ppf(0.9, 1)}
return retval
示例11: plot_2d_GMMs
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def plot_2d_GMMs(X, labels, means, covs, percentcontour=0.66, npoints=30):
"""
Given an observation array, a label vector (integer values), and GMM mean
and covariance parameters, plot the clusters and parameters.
"""
clabels = set(labels)
K = len(clabels)
if len(means) != len(covs) != K:
raise ValueError("Expecting the number of unique labels, means and"
"covariances to be the same!")
phi = np.linspace(-np.pi, np.pi, npoints)
circle = np.array([np.sin(phi), np.cos(phi)]).T
figure(figsize=(10, 10))
gca()
colors = cm.hsv(np.arange(K)/float(K))
for k, col in zip(clabels, colors):
# points
my_members = labels == k
scatter(X[my_members, 0], X[my_members, 1], c=col, marker='o', s=20)
# means
cluster_center = means[k, :]
scatter(cluster_center[0], cluster_center[1], c=col, marker='o', s=200)
# covariance
L = la.cholesky(np.array(covs[k]) * chi2.ppf(percentcontour, [3])
+ 1e-5 * np.eye(covs[k].shape[0]))
covpoints = circle.dot(L) + means[k, :]
plot(covpoints[:, 0], covpoints[:, 1], color=col, linewidth=3)
axis('tight')
axis('equal')
title('Clusters')
示例12: ljung_box_test
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def ljung_box_test(residuals, lags=[1,2,3], alpha=0.5):
from statsmodels.stats.diagnostic import acorr_ljungbox
from scipy.stats import chi2
stat, pval = acorr_ljungbox(residuals, lags=lags)
rows = []
for ct, Q in enumerate(stat):
lag = ct+1
p_value = 1 - chi2.cdf(Q, df=lag)
critical_value = chi2.ppf(1 - alpha, df=lag)
rows.append([lag, Q, p_value, critical_value, 'H0 accepted' if Q > critical_value else 'H0 rejected'])
return pd.DataFrame(rows, columns=['Lag','Statistic','p-Value','Critical Value', 'Result'])
示例13: ci_beta
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def ci_beta(self, param_num, beta_high, beta_low, sig=.05):
"""
Returns the confidence interval for a regression
parameter in the AFT model.
Parameters
---------
param_num: int
Parameter number of interest
beta_high: float
Upper bound for the confidence interval
beta_low:
Lower bound for the confidence interval
sig: float, optional
Significance level. Default is .05
Notes
----
If the function returns f(a) and f(b) must have different signs,
consider widening the search area by adjusting beta_low and
beta_high.
Also note that this process is computational intensive. There
are 4 levels of optimization/solving. From outer to inner:
1) Solving so that llr-critical value = 0
2) maximizing over nuisance parameters
3) Using EM at each value of nuisamce parameters
4) Using the _modified_Newton optimizer at each iteration
of the EM algorithm.
Also, for very unlikely nuisance parameters, it is possible for
the EM algorithm to not converge. This is not an indicator
that the solver did not find the correct solution. It just means
for a specific iteration of the nuisance parameters, the optimizer
was unable to converge.
If the user desires to verify the success of the optimization,
it is recommended to test the limits using test_beta.
"""
params = self.params()
self.r0 = chi2.ppf(1 - sig, 1)
ll = optimize.brentq(self._ci_limits_beta, beta_low,
params[param_num], (param_num))
ul = optimize.brentq(self._ci_limits_beta,
params[param_num], beta_high, (param_num))
return ll, ul
示例14: conf_int_el
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def conf_int_el(self, param_num, upper_bound=None,
lower_bound=None, sig=.05, method='nm',
stochastic_exog=1):
"""
Returns the confidence interval for a regression parameter when the
regression is forced through the origin
Parameters
----------
param_num : int
The parameter number to be tested. Note this uses python
indexing but the '0' parameter refers to the intercept term
upper_bound : float
The maximum value the upper confidence limit can be. The
closer this is to the confidence limit, the quicker the
computation. Default is .00001 confidence limit under normality
lower_bound : float
The minimum value the lower confidence limit can be.
Default is .00001 confidence limit under normality
sig : float, optional
The significance level. Default .05
method : str, optional
Algorithm to optimize of nuisance params. Can be 'nm' or
'powell'. Default is 'nm'.
Returns
-------
ci: tuple
The confidence interval for the parameter 'param_num'
"""
r0 = chi2.ppf(1 - sig, 1)
param_num = np.array([param_num])
if upper_bound is None:
upper_bound = (np.squeeze(self.model.fit().
conf_int(.0001)[param_num])[1])
if lower_bound is None:
lower_bound = (np.squeeze(self.model.fit().conf_int(.00001)
[param_num])[0])
f = lambda b0: self.el_test(np.array([b0]), param_num,
method=method,
stochastic_exog=stochastic_exog)[0] - r0
lowerl = optimize.brentq(f, lower_bound, self.params[param_num])
upperl = optimize.brentq(f, self.params[param_num], upper_bound)
return (lowerl, upperl)
示例15: ci_var
# 需要导入模块: from scipy.stats import chi2 [as 别名]
# 或者: from scipy.stats.chi2 import ppf [as 别名]
def ci_var(self, lower_bound=None, upper_bound=None, sig=.05):
"""
Returns the confidence interval for the variance.
Parameters
----------
lower_bound : float
The minimum value the lower confidence interval can
take. The p-value from test_var(lower_bound) must be lower
than 1 - significance level. Default is .99 confidence
limit assuming normality
upper_bound : float
The maximum value the upper confidence interval
can take. The p-value from test_var(upper_bound) must be lower
than 1 - significance level. Default is .99 confidence
limit assuming normality
sig : float
The significance level. Default is .05
Returns
--------
Interval : tuple
Confidence interval for the variance
Examples
--------
>>> import numpy as np
>>> import statsmodels.api as sm
>>> random_numbers = np.random.standard_normal(100)
>>> el_analysis = sm.emplike.DescStat(random_numbers)
>>> el_analysis.ci_var()
(0.7539322567470305, 1.229998852496268)
>>> el_analysis.ci_var(.5, 2)
(0.7539322567469926, 1.2299988524962664)
Notes
-----
If the function returns the error f(a) and f(b) must have
different signs, consider lowering lower_bound and raising
upper_bound.
"""
endog = self.endog
if upper_bound is None:
upper_bound = ((self.nobs - 1) * endog.var()) / \
(chi2.ppf(.0001, self.nobs - 1))
if lower_bound is None:
lower_bound = ((self.nobs - 1) * endog.var()) / \
(chi2.ppf(.9999, self.nobs - 1))
self.r0 = chi2.ppf(1 - sig, 1)
llim = optimize.brentq(self._ci_limits_var, lower_bound, endog.var())
ulim = optimize.brentq(self._ci_limits_var, endog.var(), upper_bound)
return llim, ulim