Skip to content

SciPy

SciPy supplies scientific algorithms for quant work. See how to select a solver, bracket a root and check convergence and residuals.

SciPy is an open-source Python library of scientific and numerical algorithms built on NumPy. It supplies routines for tasks such as solving equations, fitting models, integrating functions and working with probability distributions. For a quant, that means using a documented numerical method for a calibration or risk calculation while retaining responsibility for the inputs, assumptions and checks.
NumPy supplies the array foundation; SciPy adds a broader collection of specialised algorithms. Import the relevant public subpackage, such as from scipy import optimize, so a reader can see where the calculation comes from. The SciPy user guide maps its main subpackages.

Start with the mathematical task

You need to…A relevant SciPy entry pointCheck before trusting the answer
Solve one equation f(x)=0f(x)=0optimize.root_scalarDomain, solver assumptions and residual
Fit parameters to a vector of discrepanciesoptimize.least_squaresResidual scaling, bounds and identification
Minimise an objectiveoptimize.minimizeConstraints, starting point and whether the method is local
Integrate a functionintegrate.quadIntegrability, difficult regions and the reported error estimate
Evaluate a probability distributionstatsDistribution parameters and the model's suitability
A root, a minimum and a least-squares fit answer different questions. A local optimisation result does not establish a global optimum; a small fitting error does not establish that parameters are uniquely determined. The optimisation reference distinguishes these solver families.

A small solve you can check

For a first root-finding example, use the unitless toy equation f(x)=x2−2f(x)=x^2-2. It is continuous, with f(1)=−1f(1)=-1 and f(2)=2f(2)=2, so the interval [1,2][1,2] brackets a root. Its exact positive root is 2\sqrt{2}, giving an independent reference.
from scipy import optimize def f(x): return x * x - 2.0 result = optimize.root_scalar( f, bracket=(1.0, 2.0), method="brentq", xtol=1e-12, rtol=1e-12 ) print(result.converged, f"{result.root:.12f}") print(abs(f(result.root)) < 1e-12) # True 1.414213562373 # True
Executed with Python 3.14.4 and SciPy 1.18.0. root_scalar returns a result object: inspect converged and, when diagnosing failure, flag, rather than extracting root alone. Its API reference specifies the available methods and result fields.
The curve f(x) equals x squared minus two has endpoint values minus one at x equals one and plus two at x equals two; it crosses zero near 1.414213562373.
The curve f(x) equals x squared minus two has endpoint values minus one at x equals one and plus two at x equals two; it crosses zero near 1.414213562373.
The shaded interval is the supplied bracket. Continuity and the endpoint signs establish a root inside; this figure shows the equation, not the solver's iteration history.
Brent's method combines bracket safeguards with interpolation and does not require a derivative. Here it is a natural choice because the bracket is available. Newton's method can be attractive when a useful derivative and starting value are available, but it can fail or leave the intended domain. A sign change across a discontinuity alone does not establish a root. See the Brent solver contract for its continuity requirement and tolerances.
xtol is an absolute tolerance in the root variable; rtol is relative. They control root-location accuracy for this method, rather than specifying a tolerance on f(x)f(x). The explicit residual check in the code is separate. In a pricing application, choose tolerances with the units and materiality of the price discrepancy in mind; a small residual can still correspond to a large parameter error when the equation is poorly conditioned. Convergence also cannot validate the financial model or its market data.

Continue learning

Scientific Python places SciPy alongside arrays, labelled data and a clearly organised model script. Root-Finding and Numerical Integration develops bisection, Newton updates and a bond-yield application. Both have public previews; full lessons require membership.

Sources

Last updated
  • python
  • scientific computing
  • numerical methods
  • root finding
← Back to the glossaryBack to top ↑