Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def med_estimate(self):
return _estimate_from_fit(
self.influent_data,
self.med_slope,
self.intercept,
xlog=self.log_infl,
ylog=self.log_effl,
)
"medslope": theilstats[0],
"intercept": theilstats[1],
"loslope": theilstats[2],
"hislope": theilstats[3],
"is_inverted": inverted,
}
else:
output = {
"medslope": 1 / theilstats[0],
"intercept": -1 * theilstats[1] / theilstats[0],
"loslope": 1 / theilstats[2],
"hislope": 1 / theilstats[3],
"is_inverted": inverted,
}
output["estimated_effluent"] = _estimate_from_fit(
infl,
output["medslope"],
output["intercept"],
xlog=log_infl,
ylog=log_effl,
)
output["estimate_error"] = (
self.paired_data.outflow.res.values - output["estimated_effluent"]
)
return output
if fitlogs in ["x", "both"]:
x = numpy.log(x)
if fitlogs in ["y", "both"]:
y = numpy.log(y)
x = sm.add_constant(x)
# fix the y-intercept at 0 if requested
# note that this is a nuance of the statsmodels package.
# `x[:, 0] = 5` doesn't fix the intercept at 5, for instance.
if through_origin:
x[:, 0] = 0
results = sm.OLS(y, x).fit()
yhat = _estimate_from_fit(
xhat,
results.params[1],
results.params[0],
xlog=fitlogs in ["x", "both"],
ylog=fitlogs in ["y", "both"],
)
if fitprobs in ["y", "both"]:
yhat = 100.0 * dist.cdf(yhat)
if fitprobs in ["x", "both"]:
xhat = 100.0 * dist.cdf(xhat)
return xhat, yhat, results
y = numpy.log(y)
# compute fit on original data
main_params = fitfxn(x, y, **kwargs)
# raw loop to estimate the bootstrapped fit parameters
bs_params = numpy.array(
[fitfxn(x[ii], y[ii], **kwargs) for ii in _make_boot_index(len(x), niter)]
)
# un-log, if necesssary
if xlog:
x = numpy.exp(x)
# compute estimate from original data fit
yhat = _estimate_from_fit(x, main_params[0], main_params[1], xlog=xlog, ylog=ylog)
# full array of estimates
bs_estimates = _estimate_from_fit(
x[:, None], bs_params[:, 0], bs_params[:, 1], xlog=xlog, ylog=ylog
)
# both alpha alphas
percentiles = 100 * numpy.array([alpha * 0.5, 1 - alpha * 0.5])
# lower, upper bounds
bounds = numpy.percentile(bs_estimates, percentiles, axis=1)
return fitestimate(x, yhat, bounds[0], bounds[1], xlog, ylog)
main_params = fitfxn(x, y, **kwargs)
# raw loop to estimate the bootstrapped fit parameters
bs_params = numpy.array(
[fitfxn(x[ii], y[ii], **kwargs) for ii in _make_boot_index(len(x), niter)]
)
# un-log, if necesssary
if xlog:
x = numpy.exp(x)
# compute estimate from original data fit
yhat = _estimate_from_fit(x, main_params[0], main_params[1], xlog=xlog, ylog=ylog)
# full array of estimates
bs_estimates = _estimate_from_fit(
x[:, None], bs_params[:, 0], bs_params[:, 1], xlog=xlog, ylog=ylog
)
# both alpha alphas
percentiles = 100 * numpy.array([alpha * 0.5, 1 - alpha * 0.5])
# lower, upper bounds
bounds = numpy.percentile(bs_estimates, percentiles, axis=1)
return fitestimate(x, yhat, bounds[0], bounds[1], xlog, ylog)
# maybe compute ppf of y
if fitprobs in ['y', 'both']:
y = dist.ppf(y / 100.)
# maybe compute log of x
if fitlogs in ['x', 'both']:
x = numpy.log(x)
# maybe compute log of y
if fitlogs in ['y', 'both']:
y = numpy.log(y)
yhat, results = algo._fit_simple(x, y, xhat, fitlogs=fitlogs)
if estimate_ci:
yhat_lo, yhat_hi = algo._bs_fit(x, y, xhat, fitlogs=fitlogs,
niter=niter, alpha=alpha)
else:
yhat_lo, yhat_hi = None, None
# maybe undo the ppf transform
if fitprobs in ['y', 'both']:
yhat = 100. * dist.cdf(yhat)
if yhat_lo is not None:
yhat_lo = 100. * dist.cdf(yhat_lo)
yhat_hi = 100. * dist.cdf(yhat_hi)
# maybe undo ppf transform
if fitprobs in ['x', 'both']:
xhat = 100. * dist.cdf(xhat)
results['yhat_lo'] = yhat_lo
x = dist.ppf(x / 100.)
xhat = dist.ppf(numpy.array(xhat) / 100.)
# maybe compute ppf of y
if fitprobs in ['y', 'both']:
y = dist.ppf(y / 100.)
# maybe compute log of x
if fitlogs in ['x', 'both']:
x = numpy.log(x)
# maybe compute log of y
if fitlogs in ['y', 'both']:
y = numpy.log(y)
yhat, results = algo._fit_simple(x, y, xhat, fitlogs=fitlogs)
if estimate_ci:
yhat_lo, yhat_hi = algo._bs_fit(x, y, xhat, fitlogs=fitlogs,
niter=niter, alpha=alpha)
else:
yhat_lo, yhat_hi = None, None
# maybe undo the ppf transform
if fitprobs in ['y', 'both']:
yhat = 100. * dist.cdf(yhat)
if yhat_lo is not None:
yhat_lo = 100. * dist.cdf(yhat_lo)
yhat_hi = 100. * dist.cdf(yhat_hi)
# maybe undo ppf transform
if fitprobs in ['x', 'both']:
import sys
import matplotlib
matplotlib.use('agg')
from matplotlib.pyplot import style
style.use('classic')
from probscale import tests
status = tests.test(*sys.argv[1:])
sys.exit(status)
Returns
-------
xhat, yhat : numpy arrays
Linear model estimates of ``x`` and ``y``.
results : dict
Dictionary of linear fit results. Keys include:
- slope
- intersept
- yhat_lo (lower confidence interval of the estimated y-vals)
- yhat_hi (upper confidence interval of the estimated y-vals)
"""
fitprobs = validate.fit_argument(fitprobs, "fitprobs")
fitlogs = validate.fit_argument(fitlogs, "fitlogs")
# maybe set xhat to default values
if xhat is None:
xhat = copy.copy(x)
# maybe set dist to default value
if dist is None:
dist = _minimal_norm
# maybe compute ppf of x
if fitprobs in ['x', 'both']:
x = dist.ppf(x / 100.)
xhat = dist.ppf(numpy.array(xhat) / 100.)
# maybe compute ppf of y
.. plot::
:context: close-figs
>>> fig = probplot(data, plottype='qq', probax='x',
... problabel='Theoretical Quantiles',
... datalabel='Observed values', bestfit=True,
... line_kws=dict(linestyle='-', linewidth=2),
... scatter_kws=dict(marker='s', alpha=0.5))
"""
if dist is None:
dist = _minimal_norm
# check input values
fig, ax = validate.axes_object(ax)
probax = validate.axis_name(probax, 'probability axis')
problabel = validate.axis_label(problabel)
datalabel = validate.axis_label(datalabel)
# default values for symbology options
scatter_kws = validate.other_options(scatter_kws)
line_kws = validate.other_options(line_kws)
pp_kws = validate.other_options(pp_kws)
# check plottype
plottype = validate.axis_type(plottype)
# !-- kwarg that only seaborn should use --!
_color = fgkwargs.get('color', None)
if _color is not None:
scatter_kws['color'] = _color