Skip to content

Commit

Permalink
Merge branch 'main' into rust
Browse files Browse the repository at this point in the history
# Conflicts:
#	rateslib/dual/dual.py
  • Loading branch information
attack68 committed Mar 4, 2024
2 parents 3a68f2b + b9d56a7 commit 7cf1f85
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
2 changes: 2 additions & 0 deletions docs/source/i_whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ email contact through **[email protected]**.
* - Instruments
- Added argument ``metric`` to :class:`~rateslib.instruments.Value` so that specific *Curve* values derived
as calculated figures (e.g. continuously compounded zero rate, or index value) can be calibrated by *Solvers*.
* - Bug
- `Solver.delta` and `Solver.gamma` now work directly with given ``npv`` when ``fx`` is not provided.


1.0.0 (1st Feb 2024)
Expand Down
4 changes: 2 additions & 2 deletions rateslib/dual/dual.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def _set_order(self, order):
if order == 1:
return Dual(self.real, self.vars, self.dual)
if order == 2:
return Dual2(self.real, self.vars, self.dual, self.dual2)
return self
if order == 0:
return float(self)

Expand Down Expand Up @@ -530,7 +530,7 @@ def __downcast_vars__(self): # pragma: no cover

def _set_order(self, order):
if order == 1:
return Dual(self.real, self.vars, self.dual)
return self
if order == 2:
return Dual2(self.real, self.vars, self.dual, [])
if order == 0:
Expand Down
6 changes: 3 additions & 3 deletions rateslib/solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ def _set_ad_order(self, order):
# Commercial use of this code, and/or copying and redistribution is prohibited.
# Contact rateslib at gmail.com if this code is observed outside its intended sphere.

def delta(self, npv, base: Union[str, NoInput] = NoInput(0), fx=None):
def delta(self, npv, base: Union[str, NoInput] = NoInput(0), fx=NoInput(0)):
"""
Calculate the delta risk sensitivity of an instrument's NPV to the
calibrating instruments of the :class:`~rateslib.solver.Solver`, and to
Expand Down Expand Up @@ -1533,7 +1533,7 @@ def _get_base_and_fx(
base = base.lower()
return base, fx

def gamma(self, npv, base=None, fx=None):
def gamma(self, npv, base=NoInput(0), fx=NoInput(0)):
"""
Calculate the cross-gamma risk sensitivity of an instrument's NPV to the
calibrating instruments of the :class:`~rateslib.solver.Solver`.
Expand Down Expand Up @@ -1758,7 +1758,7 @@ def gamma(self, npv, base=None, fx=None):

return df.astype("float64")

def _pnl_explain(self, npv, ds, dfx=None, base=None, fx=None, order=1):
def _pnl_explain(self, npv, ds, dfx=None, base=NoInput(0), fx=NoInput(0), order=1):
"""
Calculate PnL from market movements over delta and, optionally, gamma.
Expand Down
17 changes: 17 additions & 0 deletions tests/test_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,23 @@ def test_delta_gamma_calculation():
assert -229 < float(eur_swap.gamma(NoInput(0), estr_solver).sum().sum()) < -228


def test_solver_delta_fx_noinput():
estr_curve = Curve(
{dt(2022, 1, 1): 1.0, dt(2032, 1, 1): 1.0, dt(2042, 1, 1): 1.0}, id="estr_curve"
)
estr_instruments = [
(IRS(dt(2022, 1, 1), "10Y", "A"), (estr_curve,), {}),
(IRS(dt(2022, 1, 1), "20Y", "A"), (estr_curve,), {}),
]
estr_solver = Solver(
[estr_curve], estr_instruments, [2.0, 1.5], id="estr", instrument_labels=["10Y", "20Y"]
)
eur_swap = IRS(dt(2032, 1, 1), "10Y", "A", notional=100e6, fixed_rate=2)
npv = eur_swap.npv(curves=estr_curve, solver=estr_solver, local=True)
result = estr_solver.delta(npv)
assert type(result) is DataFrame


def test_solver_pre_solver_dependency_generates_same_gamma():
estr_curve = Curve({dt(2022, 1, 1): 1.0, dt(2032, 1, 1): 1.0, dt(2042, 1, 1): 1.0})
estr_instruments = [
Expand Down

0 comments on commit 7cf1f85

Please sign in to comment.