tensorquant.numericalhandles package
Submodules
tensorquant.numericalhandles.interpolation module
- class tensorquant.numericalhandles.interpolation.LinearInterp(x, y)[source]
Bases:
objectLinear interpolation.
This class provides a simple linear interpolation method for a given set of x and y values. It computes interpolated values for a given term by linearly interpolating between the known data points.
- Parameters:
x (list or numpy array) – Known x-values (independent variable).
y (list or numpy array) – Known y-values (dependent variable).
- interpolate(term)[source]
Interpolates a value at the specified term using linear interpolation.
For a given term (input value), this method finds the two adjacent x-values that bound the term and computes the corresponding interpolated y-value.
- Parameters:
term (float) – The x-value at which interpolation is desired.
- Returns:
The interpolated y-value.
- Return type:
float
- Raises:
ValueError – If the term is outside the range of x-values.
tensorquant.numericalhandles.newton module
- tensorquant.numericalhandles.newton.newton(func, x0, tol=1e-08, max_iter=100)[source]
Solves a system of nonlinear equations using Newton’s method.
- Parameters:
func (callable) – A function that returns
(f(x), jacobian), wheref(x)is the vector of residuals andjacobianis the NxN Jacobian matrix evaluated atx.x0 (numpy.ndarray) – Initial guess for the root.
tol (float, optional) – Convergence tolerance. The method stops when both
‖f(x)‖and‖Δx‖are below tol. Defaults to 1e-8.max_iter (int, optional) – Maximum number of iterations. Defaults to 100.
- Returns:
numpy.ndarray: Solution vector
xsatisfyingfunc(x) ≈ 0.numpy.ndarray: Jacobian matrix at the solution.
- Return type:
tuple
- Raises:
ValueError – If the method fails to converge after max_iter iterations.
- tensorquant.numericalhandles.newton.newton_1d(func, i: int, r0: float, tol: float = 1e-08, max_iter: int = 100) float[source]
Scalar Newton’s method for a single pillar in curve bootstrapping.
Finds
rsuch thatfunc(i, r) = 0using the analytic derivative returned byfunc(typically computed via autodiff).- Parameters:
func (callable) – Callable with signature
func(i, r) -> (f, df_dr), wherefis the NPV of instrument i anddf_dris its derivative w.r.t. the rate at pillar i.i (int) – Pillar index being solved.
r0 (float) – Initial guess for the rate at pillar i.
tol (float, optional) – Convergence tolerance on both
|f|and the Newton step|Δr|. Defaults to 1e-8.max_iter (int, optional) – Maximum number of iterations. Defaults to 100.
- Returns:
The bootstrapped rate at pillar i.
- Return type:
float
- Raises:
ValueError – If the derivative is numerically zero or if the method fails to converge within max_iter iterations.