SciPy
02 / 02

Optimization, Integration & Stats

Optimization, Integration & Stats

Optimization

from scipy.optimize import minimize, curve_fit
import numpy as np

# Local minimum starting from an initial guess — only guaranteed LOCAL,
# different methods (BFGS, Nelder-Mead) can converge to different results
result = minimize(lambda x: (x[0] - 3) ** 2, x0=[0])
print(result.x)  # [3.]

# Fitting a model's parameters to data — reasonable p0 (initial guess)
# matters a lot for models with multiple local minima in the error surface
def exponential_decay(t, a, k):
    return a * np.exp(-k * t)

params, _ = curve_fit(exponential_decay, t_data, y_data, p0=[1.0, 0.1])

Integration & ODEs

from scipy.integrate import quad, solve_ivp

area, error_estimate = quad(lambda x: x ** 2, 0, 1)  # numeric integral, 0 to 1

# Solve dy/dt = -k*y — adaptive step size shrinks where the solution
# changes fast, grows where it's smooth, for a good accuracy/cost trade-off
solution = solve_ivp(lambda t, y: -0.5 * y, t_span=[0, 10], y0=[100], method='RK45')

Statistics

from scipy import stats

# Hypothesis test — p-value is P(this extreme a result | null hypothesis true),
# NOT the probability the null hypothesis itself is true
t_stat, p_value = stats.ttest_ind(group_a, group_b)
if p_value < 0.05:
    print('statistically significant difference')
# Statistical significance != practical significance — with a large enough
# sample, even a tiny, irrelevant difference can be "significant". Report
# an effect size alongside the p-value to judge whether it actually matters.

correlation, p = stats.pearsonr(x, y)

stats.norm.pdf(0)      # density at x=0
stats.norm.cdf(1.96)   # cumulative probability up to x=1.96
stats.norm.rvs(size=1000)  # random samples

Keep your own version of these notes — editable, searchable, and organised by your stack.

Start free