MCPcopy
hub / github.com/google/tf-quant-finance / random_dates

Function random_dates

tf_quant_finance/datetime/date_tensor.py:694–759  ·  view source on GitHub ↗

Generates random dates between the supplied start and end dates. Generates specified number of random dates between the given start and end dates. The start and end dates are supplied as `DateTensor` objects. The dates uniformly distributed between the start date (inclusive) and end date (e

(*, start_date, end_date, size=1, seed=None, name=None)

Source from the content-addressed store, hash-verified

692
693
694def random_dates(*, start_date, end_date, size=1, seed=None, name=None):
695 """Generates random dates between the supplied start and end dates.
696
697 Generates specified number of random dates between the given start and end
698 dates. The start and end dates are supplied as `DateTensor` objects. The dates
699 uniformly distributed between the start date (inclusive) and end date
700 (exclusive). Note that the dates are uniformly distributed over the calendar
701 range, i.e. no holiday calendar is taken into account.
702
703 Args:
704 start_date: DateTensor of arbitrary shape. The start dates of the range from
705 which to sample. The start dates are themselves included in the range.
706 end_date: DateTensor of shape compatible with the `start_date`. The end date
707 of the range from which to sample. The end dates are excluded from the
708 range.
709 size: Positive scalar int32 Tensor. The number of dates to draw between the
710 start and end date.
711 Default value: 1.
712 seed: Optional seed for the random generation.
713 name: Optional str. The name to give to the ops created by this function.
714 Default value: 'random_dates'.
715
716 Returns:
717 A DateTensor of shape [size] + dates_shape where dates_shape is the common
718 broadcast shape for (start_date, end_date).
719
720 #### Example
721
722 ```python
723 # Note that the start and end dates need to be of broadcastable shape (though
724 # not necessarily the same shape).
725 # In this example, the start dates are of shape [2] and the end dates are
726 # of a compatible but non-identical shape [1].
727 start_dates = tff.datetime.dates_from_tuples([
728 (2020, 5, 16),
729 (2020, 6, 13)
730 ])
731 end_dates = tff.datetime.dates_from_tuples([(2021, 5, 21)])
732 size = 3 # Generate 3 dates for each pair of (start, end date).
733 sample = tff.datetime.random_dates(start_date=start_dates, end_date=end_dates,
734 size=size)
735 # sample is a DateTensor of shape [3, 2]. The [3] is from the size and [2] is
736 # the common broadcast shape of start and end date.
737 ```
738 """
739 with tf.name_scope(name or "random_dates"):
740 size = tf.reshape(
741 tf.convert_to_tensor(size, dtype=tf.int32, name="size"), [-1])
742 start_date = convert_to_date_tensor(start_date)
743 end_date = convert_to_date_tensor(end_date)
744 # Note that tf.random.uniform cannot deal with non scalar max value with
745 # int dtypes. So we do this in float64 space and then floor. This incurs
746 # some non-uniformity of the distribution but for practical purposes this
747 # will be negligible.
748 ordinal_range = tf.cast(
749 end_date.ordinal() - start_date.ordinal(), dtype=tf.float64)
750 sample_shape = tf.concat((size, tf.shape(ordinal_range)), axis=0)
751 ordinal_sample = tf.cast(

Callers

nothing calls this directly

Calls 6

convert_to_date_tensorFunction · 0.85
from_ordinalsFunction · 0.85
reshapeMethod · 0.80
ordinalMethod · 0.80
concatMethod · 0.80
shapeMethod · 0.45

Tested by

no test coverage detected