Last updated: 2023-03-16.
tf_quant_finance.math.root_search.newton_root#
Finds roots of a scalar function using Newton’s method.
tf_quant_finance.math.root_search.newton_root(
value_and_grad_func, initial_values, max_iterations=20, tolerance=2e-07,
relative_tolerance=None, dtype=None, name='root_finder'
)
This method uses Newton’s algorithm to find values x such that f(x)=0 for
some real-valued differentiable function f. Given an initial value x_0 the
values are iteratively updated as:
x_{n+1} = x_n - f(x_n) / f'(x_n),
for further details on Newton’s method, see [1]. The implementation accepts array-like arguments and assumes that each cell corresponds to an independent scalar model.
Examples#
# Set up the problem of finding the square roots of three numbers.
constants = np.array([4.0, 9.0, 16.0])
initial_values = np.ones(len(constants))
def objective_and_gradient(values):
objective = values**2 - constants
gradient = 2.0 * values
return objective, gradient
# Obtain and evaluate a tensor containing the roots.
roots = tff.math.root_search.newton_root(objective_and_gradient,
initial_values)
print(root_values) # Expected output: [ 2. 3. 4.]
print(converged) # Expected output: [ True True True]
print(failed) # Expected output: [False False False]
References#
[1] Luenberger, D.G., 1984. ‘Linear and Nonlinear Programming’. Reading, MA: Addison-Wesley.
Args:#
value_and_grad_func: A python callable that takes aTensorof the same shape and dtype as theinitial_valuesand which returns a two-tupleofTensors, namely the objective function and the gradient evaluated at the passed parameters.initial_values: A realTensorof any shape. The initial values of the parameters to use (x_0in the notation above).max_iterations: positiveint. The maximum number of iterations of Newton’s method. Default value: 20.tolerance: positive scalarTensor. The absolute tolerance for the root search. Search is judged to have converged if|f(x_n) - f(x_n-1)|< |x_n| *relative_tolerance+tolerance(using the notation above), or ifx_nbecomesnan. When an element is judged to have converged it will no longer be updated. If all elements converge beforemax_iterationsis reached then the root finder will return early. If None, it would be set according to thedtype, which is 4 * np.finfo(dtype.as_numpy_dtype(0)).eps. Default value: 2e-7.relative_tolerance: positivedouble, default 0. See the document fortolerance. Default value: None.dtype: optionaltf.DType. If supplied theinitial_valueswill be coerced to this data type. Default value: None.name:str, to be prefixed to the name of TensorFlow ops created by this function. Default value: ‘root_finder’.
Returns:#
A three tuple of Tensors, each the same shape as initial_values. It
contains the found roots (same dtype as initial_values), a boolean
Tensor indicating whether the corresponding root results in an objective
function value less than the tolerance, and a boolean Tensor which is true
where the corresponding ‘root’ is not finite.