cds2.optimize
Optimization and root finding on scipy.optimize.
Curve-fitting diagnostics
curve_fit supports measurement uncertainty (sigma / absolute_sigma),
parameter bounds, solver selection and optional Jacobians. In addition to fitted
parameters and covariance, FitResult reports parameter standard errors,
predictions, residuals, RSS, RMSE, R² and residual degrees of freedom.
The residual diagnostics are calculated on the original, unweighted residuals;
sigma controls the parameter fit and covariance estimation.
import numpy as np
from cds2 import optimize
x = np.linspace(0.0, 4.0, 20)
y = 2.5 * x + 1.2
sigma = np.full_like(x, 0.2)
fit = optimize.curve_fit(
lambda t, slope, intercept: slope * t + intercept,
x,
y,
p0=[1.0, 0.0],
sigma=sigma,
absolute_sigma=True,
bounds=([0.0, -5.0], [5.0, 5.0]),
)
print(fit.params)
print(fit.parameter_std)
print(fit.rmse, fit.r_squared)
Optimization — convenience re-export of scipy.optimize with normalized result types; see cds2.metaheuristics and cds2.bayesopt for CDS-native global search.
OptimizationResult
dataclass
Outcome of an optimization or root-finding run.
Source code in src/cds2/optimize.py
29 30 31 32 33 34 35 36 37 | |
LinprogResult
dataclass
Outcome of a linear-programming solve.
Source code in src/cds2/optimize.py
40 41 42 43 44 45 46 47 | |
FitResult
dataclass
Non-linear least-squares fit and residual diagnostics.
The first two fields preserve the pre-5.x construction API. Additional
diagnostics default to None so existing manual FitResult
construction remains valid.
Source code in src/cds2/optimize.py
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
GlobalResult
dataclass
Outcome of a stochastic global optimization run.
Source code in src/cds2/optimize.py
177 178 179 180 181 182 183 184 185 186 | |
minimize(fun, x0, method='BFGS', **kwargs)
Minimize an unconstrained (or method-constrained) multivariate function.
Source code in src/cds2/optimize.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
minimize_scalar(fun, bracket=None, bounds=None, method=None)
Minimize a univariate function, optionally over a bounded interval.
Source code in src/cds2/optimize.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
root(fun, x0, method='hybr', **kwargs)
Find a root of a system of equations starting from x0.
Source code in src/cds2/optimize.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
find_root_scalar(f, a, b, **kwargs)
Root of a univariate function on a sign-changing bracket via Brent's method.
Source code in src/cds2/optimize.py
128 129 130 | |
newton_root(f, x0, fprime=None, tol=1.48e-08, maxiter=50)
Newton-Raphson (or secant when fprime omitted) root of a scalar function.
Source code in src/cds2/optimize.py
133 134 135 136 137 138 139 140 141 | |
linprog(c, A_ub=None, b_ub=None, A_eq=None, b_eq=None, bounds=None)
Minimize c @ x subject to linear inequality/equality constraints.
Source code in src/cds2/optimize.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
least_squares(fun, x0, **kwargs)
Solve a nonlinear least-squares problem given a residual function.
Source code in src/cds2/optimize.py
161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
curve_fit(f, xdata, ydata, p0=None, *, sigma=None, absolute_sigma=False, bounds=(-np.inf, np.inf), method=None, jac=None)
Fit model parameters by non-linear least squares.
Parameters mirror the highest-value controls from :func:scipy.optimize.curve_fit:
measurement uncertainty, absolute/relative uncertainty semantics, parameter
bounds, solver selection and an optional Jacobian. The returned diagnostics
are computed on the original (unweighted) residuals for easy model checking.
Source code in src/cds2/optimize.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
minimize_constrained(fun, x0, constraints=None, bounds=None, method='SLSQP', jac=None, **kwargs)
Minimize under equality/inequality constraints (SLSQP or trust-constr).
constraints uses SciPy's dict form, e.g.
{"type": "eq", "fun": lambda v: v[0] + v[1] - 1}.
Source code in src/cds2/optimize.py
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | |
differential_evolution(fun, bounds, maxiter=1000, popsize=15, seed=None, **kwargs)
Stochastic global minimization over a box via differential evolution.
Source code in src/cds2/optimize.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | |