Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TYP: solver.py #645

Merged
merged 11 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ exclude = [
# "/instruments/fx_volatility/strategies.py",
# "/instruments/generics.py",
# "/instruments/rates/inflation.py",
"solver.py",
# "solver.py",
]
strict = true

Expand Down
4 changes: 2 additions & 2 deletions python/rateslib/dual/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from rateslib.rs import ADOrder, Dual, Dual2, _dsolve1, _dsolve2, _fdsolve1, _fdsolve2

if TYPE_CHECKING:
from rateslib.typing import Arr1dF64, Arr1dObj, Arr2dF64, Arr2dObj, DualTypes, Number
from rateslib.typing import Arr1dF64, Arr1dObj, Arr2dF64, Arr2dObj, DualTypes, Number, Sequence

Dual.__doc__ = "Dual number data type to perform first derivative automatic differentiation."
Dual2.__doc__ = "Dual number data type to perform second derivative automatic differentiation."
Expand Down Expand Up @@ -131,7 +131,7 @@ def set_order_convert(

def gradient(
dual: Dual | Dual2 | Variable,
vars: list[str] | None = None, # noqa: A002
vars: Sequence[str] | None = None, # noqa: A002
order: int = 1,
keep_manifold: bool = False,
) -> Arr1dF64 | Arr2dF64:
Expand Down
6 changes: 3 additions & 3 deletions python/rateslib/instruments/bonds/futures.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,15 +474,15 @@ def cms(
metric = "clean_price"
solver = Solver(
curves=[bcurve],
instruments=[(_, (), {"curves": bcurve, "metric": metric}) for _ in self.basket],
s=prices, # type: ignore[arg-type]
instruments=[(_, (), {"curves": bcurve, "metric": metric}) for _ in self.basket], # type: ignore[misc]
s=prices,
)
if solver.result["status"] != "SUCCESS":
raise ValueError(
"A bond curve could not be solved for analysis. "
"See 'Cookbook: Bond Future CTD Multi-Security Analysis'.",
)
bcurve._set_ad_order(order=0) # turn of AD for efficiency
bcurve._set_ad_order(order=0) # turn off AD for efficiency

data: dict[str | float, Any] = {
"Bond": [
Expand Down
15 changes: 10 additions & 5 deletions python/rateslib/instruments/sensitivities.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from rateslib.solver import Solver

if TYPE_CHECKING:
from rateslib.typing import FX_, NPV, Curves_
from rateslib.typing import FX_, NPV, Curves_, Dual2, DualTypes
P = ParamSpec("P")


Expand Down Expand Up @@ -72,7 +72,7 @@ def delta(
"""
if isinstance(solver, NoInput):
raise ValueError("`solver` is required for delta/gamma methods.")
npv = self.npv(curves, solver, fx, base, local=True, **kwargs)
npv: dict[str, DualTypes] = self.npv(curves, solver, fx, base, local=True, **kwargs) # type: ignore[assignment]
_, fx_, base_ = _get_curves_fx_and_base_maybe_from_solver(
NoInput(0),
solver,
Expand All @@ -83,7 +83,7 @@ def delta(
)
if local:
base_ = NoInput(0)
return solver.delta(npv, base_, fx_)
return solver.delta(npv, base_, fx_) # type: ignore[arg-type]

def exo_delta(
self,
Expand Down Expand Up @@ -154,7 +154,12 @@ def exo_delta(
if local:
base_ = NoInput(0)
return solver.exo_delta(
npv=npv, vars=vars, base=base_, fx=fx_, vars_scalar=vars_scalar, vars_labels=vars_labels
npv=npv, # type: ignore[arg-type]
vars=vars,
base=base_,
fx=fx_,
vars_scalar=vars_scalar,
vars_labels=vars_labels,
)

def gamma(
Expand Down Expand Up @@ -222,7 +227,7 @@ def gamma(
_ad1 = solver._ad
solver._set_ad_order(2)

npv = self.npv(curves, solver, fx_, base_, local=True, **kwargs)
npv: dict[str, Dual2] = self.npv(curves, solver, fx_, base_, local=True, **kwargs) # type: ignore[assignment]
grad_s_sT_P: DataFrame = solver.gamma(npv, base_, fx_)

# reset original order
Expand Down
4 changes: 2 additions & 2 deletions python/rateslib/rs.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ class _DualOps:
def to_json(self) -> str: ...
def ptr_eq(self, other: Self) -> bool: ...
def __repr__(self) -> str: ...
def grad1(self, vars: list[str]) -> Arr1dF64: ... # noqa: A002
def grad2(self, vars: list[str]) -> Arr2dF64: ... # noqa: A002
def grad1(self, vars: Sequence[str]) -> Arr1dF64: ... # noqa: A002
def grad2(self, vars: Sequence[str]) -> Arr2dF64: ... # noqa: A002

class Dual(_DualOps):
def __init__(self, real: float, vars: Sequence[str], dual: Sequence[float] | Arr1dF64): ... # noqa: A002
Expand Down
Loading
Loading