Dual#

class rateslib.dual.Dual(real, vars, dual)#

Bases: object

Dual number data type to perform first derivative automatic differentiation.

Parameters:
  • real (float) – The real coefficient of the dual number: its value.

  • vars (tuple/list of str) – The labels of the variables for which to record derivatives. If empty, the dual number represents a constant, equivalent to a float.

  • dual (list of float) – First derivative information contained as coefficient of linear manifold. Defaults to an array of ones the length of vars if empty.

See also

Dual2

Dual number data type to perform second derivative automatic differentiation.

Examples

In [1]: def func(x, y):
   ...:     return 5 * x**2 + 10 * y**3
   ...: 

In [2]: x = Dual(1.0, ["x"], [])

In [3]: y = Dual(1.0, ["y"], [])

In [4]: gradient(func(x,y), ["x", "y"])
Out[4]: array([10., 30.])

Attributes Summary

dual

First derivative information contained as coefficient of linear manifold.

dual2

Not available on Dual.

real

The real coefficient of the dual number - its value.

vars

The string labels of the variables for which to record derivatives.

Methods Summary

grad1(vars)

Return the first derivatives of Self.

grad2(_vars)

Not available for Dual.

ptr_eq(other)

Evaluate if the ARC pointers of two Dual data types are equivalent.

to_dual2()

Convert self into a Dual2 with 2nd order manifold set to zero.

to_json()

Create a JSON string representation of the object.

vars_from(other, real, vars, dual)

Create a Dual object with vars linked with another.

Attributes Documentation

dual#

First derivative information contained as coefficient of linear manifold.

dual2#

Not available on Dual.

real#

The real coefficient of the dual number - its value.

vars#

The string labels of the variables for which to record derivatives.

Methods Documentation

grad1(vars)#

Return the first derivatives of Self.

Parameters:

vars (tuple/list of str) – Name of the variables which to return gradients for.

Return type:

ndarray

grad2(_vars)#

Not available for Dual.

ptr_eq(other)#

Evaluate if the ARC pointers of two Dual data types are equivalent.

Parameters:

other (Dual) – The comparison object.

Return type:

bool

to_dual2()#

Convert self into a Dual2 with 2nd order manifold set to zero.

to_json()#

Create a JSON string representation of the object.

Return type:

str

static vars_from(other, real, vars, dual)#

Create a Dual object with vars linked with another.

Parameters:
  • other (Dual) – The other Dual from which vars are linked.

  • real (float) – The real coefficient of the dual number.

  • vars (list[str]) – The labels of the variables for which to record derivatives. If empty, the dual number represents a constant, equivalent to a float.

  • dual (list[float]) – First derivative information contained as coefficient of linear manifold. Defaults to an array of ones the length of vars if empty.

Return type:

Dual

Notes

Variables are constantly checked when operations are performed between dual numbers. In Rust the variables are stored within an ARC pointer. It is much faster to check the equivalence of two ARC pointers than if the elements within a variables Set, say, are the same and in the same order. This method exists to create dual data types with shared ARC pointers directly.

In [1]: from rateslib import Dual

In [2]: x1 = Dual(1.0, ["x"], [])

In [3]: x2 = Dual(2.0, ["x"], [])

# x1 and x2 have the same variables (["x"]) but it is a different object
In [4]: x1.ptr_eq(x2)
Out[4]: False

In [5]: x3 = Dual.vars_from(x1, 3.0, ["x"], [])

# x3 contains shared object variables with x1
In [6]: x1.ptr_eq(x3)
Out[6]: True