<commit_before># Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python2, python3
"""Methods to construct a discount curve from bonds.

Building discount curves is a core problem in mathematical finance. Discount
curves are built using the available market data in liquidly traded rates
products. These include bonds, swaps, forward rate agreements (FRAs) or
eurodollar futures contracts. This module contains methods to build rate curve
from (potentially coupon bearing) bonds data.

A discount curve is a function of time which gives the interest rate that
applies to a unit of currency deposited today for a period of  time `t`.
The traded price of bonds implicitly contains the market view on the discount
rates. The purpose of discount curve construction is to extract this
information.

The algorithm implemented here is based on the Monotone Convex Interpolation
method described by Hagan and West in Ref [1, 2].

### References:

[1]: <NAME> & <NAME>. Interpolation Methods for Curve Construction.
  Applied Mathematical Finance. Vol 13, No. 2, pp 89-129. June 2006.
  https://www.researchgate.net/publication/24071726_Interpolation_Methods_for_Curve_Construction
[2]: <NAME> & <NAME>. Methods for Constructing a Yield Curve.
  Wilmott Magazine, pp. 70-81. May 2008.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import google_type_annotations
from __future__ import print_function

import collections
import tensorflow as tf

from tf_quant_finance.rates import cashflows
from tf_quant_finance.rates.hagan_west import monotone_convex

CurveBuilderResult = collections.namedtuple(
    'CurveBuilderResult',
    [
        # Rank 1 real `Tensor`. Times for the computed discount rates.
        'times',
        # Rank 1 `Tensor` of the same dtype as `times`.
        # The inferred discount rates.
        'discount_rates',
        # Rank 1 `Tensor` of the same dtype as `times`.
        # The inferred discount factors.
        'discount_factors',
        # Rank 1 `Tensor` of the same dtype as `times`. The
        # initial guess for the discount rates.
        'initial_discount_rates',
        # Scalar boolean `Tensor`. Whether the procedure converged.
        'converged',
        # Scalar boolean `Tensor`. Whether the procedure failed.
        'failed',
        # Scalar int32 `Tensor`. Number of iterations performed.
        'iterations'
    ])

<commit_msg>Remove unnecessary "from __future__ import".<commit_after># Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Lint as: python2, python3
"""Methods to construct a discount curve from bonds.

Building discount curves is a core problem in mathematical finance. Discount
curves are built using the available market data in liquidly traded rates
products. These include bonds, swaps, forward rate agreements (FRAs) or
eurodollar futures contracts. This module contains methods to build rate curve
from (potentially coupon bearing) bonds data.

A discount curve is a function of time which gives the interest rate that
applies to a unit of currency deposited today for a period of  time `t`.
The traded price of bonds implicitly contains the market view on the discount
rates. The purpose of discount curve construction is to extract this
information.

The algorithm implemented here is based on the Monotone Convex Interpolation
method described by Hagan and West in Ref [1, 2].

### References:

[1]: <NAME> & <NAME>. Interpolation Methods for Curve Construction.
  Applied Mathematical Finance. Vol 13, No. 2, pp 89-129. June 2006.
  https://www.researchgate.net/publication/24071726_Interpolation_Methods_for_Curve_Construction
[2]: <NAME> & <NAME>. Methods for Constructing a Yield Curve.
  Wilmott Magazine, pp. 70-81. May 2008.
"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
import tensorflow as tf

from tf_quant_finance.rates import cashflows
from tf_quant_finance.rates.hagan_west import monotone_convex

CurveBuilderResult = collections.namedtuple(
    'CurveBuilderResult',
    [
        # Rank 1 real `Tensor`. Times for the computed discount rates.
        'times',
        # Rank 1 `Tensor` of the same dtype as `times`.
        # The inferred discount rates.
        'discount_rates',
        # Rank 1 `Tensor` of the same dtype as `times`.
        # The inferred discount factors.
        'discount_factors',
        # Rank 1 `Tensor` of the same dtype as `times`. The
        # initial guess for the discount rates.
        'initial_discount_rates',
        # Scalar boolean `Tensor`. Whether the procedure converged.
        'converged',
        # Scalar boolean `Tensor`. Whether the procedure failed.
        'failed',
        # Scalar int32 `Tensor`. Number of iterations performed.
        'iterations'
    ])


{'fraction_non_alphanumeric': 0.064280868385346, 'fraction_numerical': 0.016282225237449117, 'mean_word_length': 3.7100638977635785, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 4, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2228354', 'n_tokens_mistral': 1602, 'n_tokens_neox': 1492, 'n_words': 736}