Documentation#

PYCSE#

Module containing useful scientific and engineering functions.

  • Linear regression

  • Nonlinear regression.

  • Differential equation solvers.

See http://kitchingroup.cheme.cmu.edu/pycse

Copyright 2020, John Kitchin (see accompanying license files for details).

pycse.PYCSE.Rsquared(y, Y)#

Return R^2, or coefficient of determination.

y is a 1d array of observations. Y is a 1d array of predictions from a model.

Returns:
The R^2 value for the fit.
pycse.PYCSE.bic(x, y, model, popt)#

Compute the Bayesian information criterion (BIC).

Parameters:
modelfunction(x, …) returns prediction for y
poptoptimal parameters
yarray, known y-values
Returns:
BICfloat
https://en.wikipedia.org/wiki/Bayesian_information_criterion#Gaussian_special_case
pycse.PYCSE.ivp(f, tspan, y0, *args, **kwargs)#

Solve an ODE initial value problem.

Parameters:
ffunction
callable y’(x, y) = f(x, y)
tspanarray
The x points you want the solution at. The first and last points are used in
tspan in solve_ivp.
y0array
Initial conditions
*argstype
arbitrary positional arguments to pass to solve_ivp
**kwargstype arbitrary kwargs to pass to solve_ivp.
max_step is set to be the min diff of tspan. dense_output is set to True.
t_eval is set to the array specified in tspan.
Returns:
solution from solve_ivp
pycse.PYCSE.lbic(X, y, popt)#

Compute the Bayesian information criterion for a linear model.

Returns:
BICfloat
pycse.PYCSE.nlinfit(model, x, y, p0, alpha=0.05, **kwargs)#

Nonlinear regression with confidence intervals.

Parameters:
modelfunction f(x, p0, p1, …) = y
xarray of the independent data
yarray of the dependent data
p0array of the initial guess of the parameters
alpha100*(1 - alpha) is the confidence interval

i.e. alpha = 0.05 is 95% confidence

kwargs are passed to curve_fit.
Returns:
[p, pint, SE]

p is an array of the fitted parameters pint is an array of confidence intervals SE is an array of standard errors for the parameters.

pycse.PYCSE.nlpredict(X, y, model, loss, popt, xnew, alpha=0.05, ub=1e-05, ef=1.05)#

Prediction error for a nonlinear fit.

Parameters:
modelmodel function with signature model(x, …)
lossloss function the model was fitted with loss(…)
poptthe optimized paramters
xnewx-values to predict at
alphaconfidence level, 95% = 0.05
ubupper bound for smallest allowed Hessian eigenvalue
efeigenvalue factor for scaling Hessian
This function uses numdifftools for the Hessian and Jacobian.
See https://en.wikipedia.org/wiki/Prediction_interval#Unknown_mean,_unknown_variance
Returns:
y, yint, se
ypredicted values
yintprediction interval at alpha confidence interval
sestandard error of prediction
pycse.PYCSE.polyfit(x, y, deg, alpha=0.05, *args, **kwargs)#

Least squares polynomial fit with parameter confidence intervals.

Parameters:
xarray_like, shape (M,)

x-coordinates of the M sample points (x[i], y[i]).

yarray_like, shape (M,) or (M, K)

y-coordinates of the sample points. Several data sets of sample points sharing the same x-coordinates can be fitted at once by passing in a 2D-array that contains one dataset per column.

degint

Degree of the fitting polynomial

*args and **kwargs are passed to regress.
Returns:
[b, bint, se]
b is a vector of the fitted parameters
bint is a 2D array of confidence intervals
se is an array of standard error for each parameter.
pycse.PYCSE.predict(X, y, pars, XX, alpha=0.05, ub=1e-05, ef=1.05)#

Prediction interval for linear regression.

Based on the delta method.

Parameters:
Xknown x-value array, one row for each y-point
yknown y-value array
parsfitted parameters
XXx-value array to make predictions for
alphaconfidence level, 95% = 0.05
ubupper bound for smallest allowed Hessian eigenvalue
efeigenvalue factor for scaling Hessian
See See https://en.wikipedia.org/wiki/Prediction_interval#Unknown_mean,_unknown_variance
Returns
y, yint, pred_se
ythe predicted values
yint: confidence interval
pred_se: std error on predictions.
pycse.PYCSE.regress(A, y, alpha=0.05, *args, **kwargs)#

Linear least squares regression with confidence intervals.

Solve the matrix equation (A p = y) for p.

The confidence intervals account for sample size using a student T multiplier.

This code is derived from the descriptions at http://www.weibull.com/DOEWeb/confidence_intervals_in_multiple_linear_regression.htm and http://www.weibull.com/DOEWeb/estimating_regression_models_using_least_squares.htm

Parameters:
Aa matrix of function values in columns, e.g.

A = np.column_stack([T**0, T**1, T**2, T**3, T**4])

ya vector of values you want to fit
alpha100*(1 - alpha) confidence level
args and kwargs are passed to np.linalg.lstsq
Returns:
[b, bint, se]
b is a vector of the fitted parameters
bint is an array of confidence intervals. The ith row is for the ith parameter.
se is an array of standard error for each parameter.

pycse.utils#

Provides utility functions in pycse.

  1. Fuzzy comparisons for float numbers.

  2. An ignore exception decorator

  3. A handy function to read a google sheet.

pycse.utils.feq(x, y, epsilon=np.float64(2.220446049250313e-16))#

Fuzzy equals.

x == y with tolerance

pycse.utils.fge(x, y, epsilon=np.float64(2.220446049250313e-16))#

Fuzzy greater than or equal to .

x >= y with tolerance

pycse.utils.fgt(x, y, epsilon=np.float64(2.220446049250313e-16))#

Fuzzy greater than.

x > y with tolerance

pycse.utils.fle(x, y, epsilon=np.float64(2.220446049250313e-16))#

Fuzzy less than or equal to.

x <= y with tolerance

pycse.utils.flt(x, y, epsilon=np.float64(2.220446049250313e-16))#

Fuzzy less than.

x < y with tolerance

pycse.utils.ignore_exception(*exceptions)#

Ignore exceptions on decorated function.

>>> with ignore_exception(ZeroDivisionError):
...     print(1/0)
pycse.utils.read_gsheet(url, *args, **kwargs)#

Return a dataframe for the Google Sheet at url.

args and kwargs are passed to pd.read_csv The url should be viewable by anyone with the link.

pycse.plotly#

pycse.hashcache#