MCPcopy Create free account
hub / github.com/Apress/python-for-matlab-development / predband

Function predband

code/statistics/predict_interval.py:37–63  ·  view source on GitHub ↗

https://apmonitor.com/che263/index.php/Main/PythonRegressionStatistics x = requested points xd = x data yd = y data p = additional arguments to func, after xd func = function name

(x, xd, yd, p, func, conf=0.95)

Source from the content-addressed store, hash-verified

35import seaborn as sns
36
37def predband(x, xd, yd, p, func, conf=0.95):
38 """
39 https://apmonitor.com/che263/index.php/Main/PythonRegressionStatistics
40 x = requested points
41 xd = x data
42 yd = y data
43 p = additional arguments to func, after xd
44 func = function name
45 """
46 alpha = 1.0 - conf # significance
47 N = xd.size # data sample size
48 var_n = len(p) # number of parameters
49 # Quantile of Student's t distribution for p=(1-alpha/2)
50 q = scipy.stats.t.ppf(1.0 - alpha / 2.0, N - var_n)
51 # Stdev of an individual measurement
52 se = np.sqrt(1. / (N - var_n) *
53 np.sum((yd - func(xd, *p)) ** 2))
54 # Auxiliary definitions
55 sx = (x - xd.mean()) ** 2
56 sxd = np.sum((xd - xd.mean()) ** 2)
57 # Predicted values (best-fit model)
58 yp = func(x, *p)
59 # Prediction band
60 dy = q * se * np.sqrt(1.0+ (1.0/N) + (sx/sxd))
61 # Upper & lower prediction bands.
62 lpb, upb = yp - dy, yp + dy
63 return lpb, upb
64
65def model_function(x, m, b):
66 return m*x + b

Callers 1

Calls 1

funcFunction · 0.85

Tested by

no test coverage detected