class_docstr: Creates a piecewise constant function.
docstr: Initializes jumps of the piecewise constant function.
  
  Sets jump locations and values for a piecewise constant function.
  `jump_locations` split real line into intervals
  `[-inf, jump_locations[..., 0]], ..,
  [jump_locations[..., i], jump_locations[..., i + 1]], ...,
  [jump_locations[..., -1], inf]`
  so that the piecewise constant function takes corresponding `values` on the
  intervals, i.e.,
  ```
  f(x) = \sum_i values[..., i]
         * I_{x \in [jump_locations[..., i -1], jump_locations[..., i])}
  ```
  
  #### Example. Simple scalar-valued piecewise constant function.
  ```python
  dtype = np.float64
  jump_locations = [0.1, 10]
  values = [3, 4, 5]
  piecewise_func = piecewise.PiecewiseConstantFunc(jump_locations, values,
                                                   dtype=dtype)
  # Locations at which to evaluate the function assuming it is
  # left-continuous.
  x = np.array([0., 0.1, 2., 11.])
  value = piecewise_func(x)
  # Expected values: [3, 3, 4, 5]
  integral = piecewise_func.integrate(x, x + 1)
  # Expected integrals: [3.9, 4, 4, 5]
  ```
  
  #### Example. Matrix-valued piecewise constant function.
  ```python
  dtype = np.float64
  jump_locations = [0.1, 10]
  # The function takes values [[1, 2], [3, 4]] on (-inf, 0.1),
  # [[5, 6], [7, 8]] on (0.1, 0.5), and [[9, 10], [11, 12]] on (0.5, +inf).
  values = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]
  piecewise_func = piecewise.PiecewiseConstantFunc(
      jump_locations, values, dtype=dtype)
  # Locations at which to evaluate the function assuming it is
  # left-continuous.
  x = np.array([0., 0.1, 2, 11])
  value = piecewise_func(x)
  # Expected values: [[[1, 2], [3, 4]], [[1, 2], [3, 4]],
  #                   [[5, 6], [7, 8]], [[9, 10], [11, 12]]]
  integral = piecewise_func.integrate(x, x + 1)
  # Expected integrals: [[[4.6, 5.6], [6.6, 7.6]],
  #                      [[5, 6], [7, 8]],
  #                      [[5, 6], [7, 8]],
  #                      [[9, 10], [11, 12]]]
  ```
  
  Args:
    jump_locations: A real `Tensor` of shape
      `batch_shape + [num_jump_points]`. The locations where the function
      changes its values. Note that the values are expected to be ordered
      along the last dimension. Repeated values are allowed but it is
      up to the user to ensure that the corresponding `values` are also
      repeated.
    values: A `Tensor` of the same `dtype` as `jump_locations` and shape
      `batch_shape + [num_jump_points + 1] + event_shape`. Defines
      `values[batch_rank * slice(None), i]` on intervals
      `(jump_locations[..., i - 1], jump_locations[..., i])`. Here
      `event_shape` allows for array-valued piecewise constant functions
      and `batch_rank = len(batch_shape)`.
    dtype:  Optional dtype for `jump_locations` and `values`.
      Default value: `None` which maps to the default dtype inferred from
      `jump_locations`.
    name: Python `str` name prefixed to ops created by this class.
      Default value: `None` which is mapped to the default name
      `PiecewiseConstantFunc`.
  
  Raises:
    ValueError:
      If `jump_locations` and `values` have different batch shapes or,
      in case of static shapes, if the event shape of `values` is different
      from `num_jump_points + 1`.
docstr_tok: ["Initializes","jumps","of","the","piecewise","constant","function",".","Sets","jump","locations","and","values","for","a","piecewise","constant","function",".","`","jump_locations","`","split","real","line","into","intervals","`","[","-inf",",","jump_locations","[","...",",","0","]","]",",","..",",","[","jump_locations","[","...",",","i","]",",","jump_locations","[","...",",","i","+","1","]","]",",","...",",","[","jump_locations","[","...",",","-1","]",",","inf","]","`","so","that","the","piecewise","constant","function","takes","corresponding","`","values","`","on","the","intervals",",","i.e.",",","``","`","f","(","x",")","=","\\sum_i","values","[","...",",","i","]","*","I_","{","x","\\in","[","jump_locations","[","...",",","i","-1","]",",","jump_locations","[","...",",","i","]",")","}","``","`","#","#","#","#","Example",".","Simple","scalar-valued","piecewise","constant","function",".","``","`","python","dtype","=","np.float64","jump_locations","=","[","0.1",",","10","]","values","=","[","3",",","4",",","5","]","piecewise_func","=","piecewise.PiecewiseConstantFunc","(","jump_locations",",","values",",","dtype=dtype",")","#","Locations","at","which","to","evaluate","the","function","assuming","it","is","#","left-continuous",".","x","=","np.array","(","[","0.",",","0.1",",","2.",",","11",".","]",")","value","=","piecewise_func","(","x",")","#","Expected","values",":","[","3",",","3",",","4",",","5","]","integral","=","piecewise_func.integrate","(","x",",","x","+","1",")","#","Expected","integrals",":","[","3.9",",","4",",","4",",","5","]","``","`","#","#","#","#","Example",".","Matrix-valued","piecewise","constant","function",".","``","`","python","dtype","=","np.float64","jump_locations","=","[","0.1",",","10","]","#","The","function","takes","values","[","[","1",",","2","]",",","[","3",",","4","]","]","on","(","-inf",",","0.1",")",",","#","[","[","5",",","6","]",",","[","7",",","8","]","]","on","(","0.1",",","0.5",")",",","and","[","[","9",",","10","]",",","[","11",",","12","]","]","on","(","0.5",",","+inf",")",".","values","=","[","[","[","1",",","2","]",",","[","3",",","4","]","]",",","[","[","5",",","6","]",",","[","7",",","8","]","]",",","[","[","9",",","10","]",",","[","11",",","12","]","]","]","piecewise_func","=","piecewise.PiecewiseConstantFunc","(","jump_locations",",","values",",","dtype=dtype",")","#","Locations","at","which","to","evaluate","the","function","assuming","it","is","#","left-continuous",".","x","=","np.array","(","[","0.",",","0.1",",","2",",","11","]",")","value","=","piecewise_func","(","x",")","#","Expected","values",":","[","[","[","1",",","2","]",",","[","3",",","4","]","]",",","[","[","1",",","2","]",",","[","3",",","4","]","]",",","#","[","[","5",",","6","]",",","[","7",",","8","]","]",",","[","[","9",",","10","]",",","[","11",",","12","]","]","]","integral","=","piecewise_func.integrate","(","x",",","x","+","1",")","#","Expected","integrals",":","[","[","[","4.6",",","5.6","]",",","[","6.6",",","7.6","]","]",",","#","[","[","5",",","6","]",",","[","7",",","8","]","]",",","#","[","[","5",",","6","]",",","[","7",",","8","]","]",",","#","[","[","9",",","10","]",",","[","11",",","12","]","]","]","``","`","Args",":","jump_locations",":","A","real","`","Tensor","`","of","shape","`","batch_shape","+","[","num_jump_points","]","`",".","The","locations","where","the","function","changes","its","values",".","Note","that","the","values","are","expected","to","be","ordered","along","the","last","dimension",".","Repeated","values","are","allowed","but","it","is","up","to","the","user","to","ensure","that","the","corresponding","`","values","`","are","also","repeated",".","values",":","A","`","Tensor","`","of","the","same","`","dtype","`","as","`","jump_locations","`","and","shape","`","batch_shape","+","[","num_jump_points","+","1","]","+","event_shape","`",".","Defines","`","values","[","batch_rank","*","slice","(","None",")",",","i","]","`","on","intervals","`","(","jump_locations","[","...",",","i","-","1","]",",","jump_locations","[","...",",","i","]",")","`",".","Here","`","event_shape","`","allows","for","array-valued","piecewise","constant","functions","and","`","batch_rank","=","len","(","batch_shape",")","`",".","dtype",":","Optional","dtype","for","`","jump_locations","`","and","`","values","`",".","Default","value",":","`","None","`","which","maps","to","the","default","dtype","inferred","from","`","jump_locations","`",".","name",":","Python","`","str","`","name","prefixed","to","ops","created","by","this","class",".","Default","value",":","`","None","`","which","is","mapped","to","the","default","name","`","PiecewiseConstantFunc","`",".","Raises",":","ValueError",":","If","`","jump_locations","`","and","`","values","`","have","different","batch","shapes","or",",","in","case","of","static","shapes",",","if","the","event","shape","of","`","values","`","is","different","from","`","num_jump_points","+","1","`","."]
code: def __init__(self, jump_locations, values, dtype=None, name=None):
      r"""Initializes jumps of the piecewise constant function.
  
      Sets jump locations and values for a piecewise constant function.
      `jump_locations` split real line into intervals
      `[-inf, jump_locations[..., 0]], ..,
      [jump_locations[..., i], jump_locations[..., i + 1]], ...,
      [jump_locations[..., -1], inf]`
      so that the piecewise constant function takes corresponding `values` on the
      intervals, i.e.,
      ```
      f(x) = \sum_i values[..., i]
             * I_{x \in [jump_locations[..., i -1], jump_locations[..., i])}
      ```
  
      #### Example. Simple scalar-valued piecewise constant function.
      ```python
      dtype = np.float64
      jump_locations = [0.1, 10]
      values = [3, 4, 5]
      piecewise_func = piecewise.PiecewiseConstantFunc(jump_locations, values,
                                                       dtype=dtype)
      # Locations at which to evaluate the function assuming it is
      # left-continuous.
      x = np.array([0., 0.1, 2., 11.])
      value = piecewise_func(x)
      # Expected values: [3, 3, 4, 5]
      integral = piecewise_func.integrate(x, x + 1)
      # Expected integrals: [3.9, 4, 4, 5]
      ```
  
      #### Example. Matrix-valued piecewise constant function.
      ```python
      dtype = np.float64
      jump_locations = [0.1, 10]
      # The function takes values [[1, 2], [3, 4]] on (-inf, 0.1),
      # [[5, 6], [7, 8]] on (0.1, 0.5), and [[9, 10], [11, 12]] on (0.5, +inf).
      values = [[[1, 2], [3, 4]], [[5, 6], [7, 8]], [[9, 10], [11, 12]]]
      piecewise_func = piecewise.PiecewiseConstantFunc(
          jump_locations, values, dtype=dtype)
      # Locations at which to evaluate the function assuming it is
      # left-continuous.
      x = np.array([0., 0.1, 2, 11])
      value = piecewise_func(x)
      # Expected values: [[[1, 2], [3, 4]], [[1, 2], [3, 4]],
      #                   [[5, 6], [7, 8]], [[9, 10], [11, 12]]]
      integral = piecewise_func.integrate(x, x + 1)
      # Expected integrals: [[[4.6, 5.6], [6.6, 7.6]],
      #                      [[5, 6], [7, 8]],
      #                      [[5, 6], [7, 8]],
      #                      [[9, 10], [11, 12]]]
      ```
  
      Args:
        jump_locations: A real `Tensor` of shape
          `batch_shape + [num_jump_points]`. The locations where the function
          changes its values. Note that the values are expected to be ordered
          along the last dimension. Repeated values are allowed but it is
          up to the user to ensure that the corresponding `values` are also
          repeated.
        values: A `Tensor` of the same `dtype` as `jump_locations` and shape
          `batch_shape + [num_jump_points + 1] + event_shape`. Defines
          `values[batch_rank * slice(None), i]` on intervals
          `(jump_locations[..., i - 1], jump_locations[..., i])`. Here
          `event_shape` allows for array-valued piecewise constant functions
          and `batch_rank = len(batch_shape)`.
        dtype:  Optional dtype for `jump_locations` and `values`.
          Default value: `None` which maps to the default dtype inferred from
          `jump_locations`.
        name: Python `str` name prefixed to ops created by this class.
          Default value: `None` which is mapped to the default name
          `PiecewiseConstantFunc`.
  
      Raises:
        ValueError:
          If `jump_locations` and `values` have different batch shapes or,
          in case of static shapes, if the event shape of `values` is different
          from `num_jump_points + 1`.
      """
      self._name = name or "PiecewiseConstantFunc"
      # Add a property that indicates that the class instance is a
      # piecewise constant function
      self.is_piecewise_constant = True
      with tf.name_scope(self._name):
          self._jump_locations = tf.convert_to_tensor(
              jump_locations, dtype=dtype, name="jump_locations"
          )
          self._dtype = dtype or self._jump_locations.dtype
          self._values = tf.convert_to_tensor(
              values, dtype=self._dtype, name="values"
          )
          shape_values = self._values.shape.as_list()
  
          shape_jump_locations = self._jump_locations.shape.as_list()
          batch_rank = self._jump_locations.shape.rank - 1
          self._batch_rank = batch_rank
          if (
              None not in shape_values
              and None not in shape_jump_locations
          ):
              if shape_values[:batch_rank] != shape_jump_locations[:-1]:
                  raise ValueError(
                      "Batch shapes of `values` and `jump_locations` should "
                      "be the same but are {0} and {1}".format(
                          shape_values[:-1], shape_jump_locations[:-1]
                      )
                  )
              if (
                  shape_values[batch_rank] - 1
                  != shape_jump_locations[-1]
              ):
                  raise ValueError(
                      "Event shape of `values` should have one more "
                      "element than the event shape of `jump_locations` "
                      "but are {0} and {1}".format(
                          shape_values[-1], shape_jump_locations[-1]
                      )
                  )
  
file_path: tf-quant-finance/tf_quant_finance/math/piecewise.py
imports: import tensorflow.compat.v2
label_desc: Use image node_id 1 to create a new PiecewiseConstantFunc object from inherited base classes: object with example: obj = PiecewiseConstantFunc(jump_locations, values, dtype, name)