# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernel_info:
#     name: python3
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# ## Attrition Rate Curve
#
# An attrition rate curve is given as a period $t_P$ and a list of time point/attrition rate pairs $(t_i, p_i)$ where $p_i$ is the attrition rate per period of length $t_P$ for $t \in [t_i, t_{i+1})$. Note that $t_1 = 0$.

# ### Converting attrition rate curve to a piecewise exponential distribution
#
# To convert the attrition rate curve to a piecewise exponential distribution, we only need to compute the $\lambda$ for each piece of the curve. To do this, denote $T_i \sim \mathrm{Exp}(\lambda_i)$ such that $P(T_i \leqslant t_P) = p_i$. Since the distribution function of the exponential distribution is $F_{T_i}(t) = \exp(- \lambda_i t)$, we can observe that
# $$
# p_i = 1 - F_{T_i}(t_P) = 1 - \exp(- \lambda_i t_P)
# $$
# or, after reworking, that
# $$
# \lambda_i = -\frac{\log(1 - p_i)}{t_P}.
# $$

# ### Computing the distribution function of a piecewise exponential distribution
#
# Assume that $X$ follows a piecewise exponential distribution with anchor points $t_i$ and associated $\lambda_i$. The distribution function for this distribution is $F_{X}$. To compute this function for a given $t$, apply the following steps:
#
# 1. find $J = \max \{i \mid t_i \leqslant t \}$ where we drop the dependency of $J$ on $t$ to keep the notation clear;
# 2. then $P(X \geqslant t) = P(X \geqslant t \mid X \geqslant t_J) P(X \geqslant t_J)$;
# 3. the first factor equals $\exp(- \lambda_j (t - t_J))$ since $X \mid X \geqslant t_J$ follows an exponential distribution with parameter $\lambda_j$, at least for $t \in [t_J, t_J+1]$;
# 4. the second factor can be computed recursively in a similar way: $P(X \geqslant t_{i+1}) = P(X \geqslant t_{i+1} \mid X \geqslant t_i) P(X \geqslant t_i) = \exp(- \lambda_i (t_{i+1} - t_i))$ and $P(X \geqslant t_1) = 1$.
#
# If we denote $\beta_i = P(X \geqslant t_{i+1} \mid X \geqslant t_i) = \exp(- \lambda_i (t_{i+1} - t_i))$ and $\gamma_i = P(X \geqslant t_i) = P(X \geqslant t_1) \prod_{j=1}^{i-1} P(X \geqslant t_{i+1} \mid X \geqslant t_i) = \prod_{j=1}^{i-1} \beta_j$, then we can write
# $$
# P(X \geqslant t) = \gamma_J \exp(- \lambda_J (t - t_J)).
# $$

# ### Computing the conditional distribution function
#
# We will also need the distribution function for $X \mid X \geqslant s$.
#
# 1. determine $J_s = \max \{ i \mid t_i \leqslant s \}$ and $J_t = \max \{ i \mid t_i \leqslant t \}$;
# 2. if $J_s = J_t = J$, we find that
# $$
# P(X \geqslant t \mid X \geqslant s) = \frac{P(X \geqslant t)}{P(X \geqslant s)} = \frac{\gamma_J \exp(- \lambda_J (t - t_J))}{\gamma_J \exp(- \lambda_J (s - t_J))} = \exp(- \lambda_J (t - s) );
# $$
# 3. if $J_s < J_t$ however, things are a bit different. Then
# $$
# \begin{aligned}
# P(X \geqslant t \mid X \geqslant s) &= \frac{P(X \geqslant t)}{P(X \geqslant s)} = \frac{\gamma_{J_t} \exp(- \lambda_{J_t} (t - t_{J_t}))}{\gamma_{J_s} \exp(- \lambda_{J_s} (s - t_{J_s}))} \\
# &= \frac{\gamma_{J_t}}{\gamma_{J_s}} \exp(\lambda_{J_s} (s - t_{J_s}) - \lambda_{J_t} (t - t_{J_t})).
# \end{aligned}
# $$
#

# ### Sampling from the distribution
#
# In theory, sampling from a piecewise exponential distribution is simple, as we already know its distribution function. Hence, $X = F_X^{-1}(U)$ where $U \sim \mathcal{U}[0,1]$. In practice however, it is easier to use ${F'}_X = 1 - F_X$ instead.
#
# Let $u$ be a sample drawn from $U$. Then there exists a $t \geqslant 0$ such that ${F'}_X(t) = u$. To find this $t$, do the following:
#
# 1. find $J = \max \{ i \mid \gamma_i \geqslant u \}$, then $t_J \leqslant t < t_{J+1}$;
# 2. since $P(X \geqslant t) = \gamma_J \exp(- \lambda_J (t - t_J)) = u$, we can rework this to
# $$
# t = t_J - \frac{1}{\lambda_J} \log \frac{u}{\gamma_J}
# $$
# if $\lambda_J > 0$.
#
# Note that $\lambda_J = 0$ cannot happen by the definition of $J$. Assume that $\lambda_J = 0$, then $\gamma_{J+1} = \gamma_J \beta_J=\gamma_J \exp(- \lambda_J (t_{J+1} - t_J)) = \gamma_J$. Since $\gamma_J \geqslant u$, $\gamma_{J+1} \geqslant u$ also holds, which contradicts the definition of $J$. Hence, $\lambda_J > 0$ always holds.

# ### Sampling from the conditional distribution
#
# Sampling from the conditional distribution $X \mid X \geqslant s$ is similar to sampling from $X$ itself.
#
# 1. find $J_s = \max \{ i \mid t_i \leqslant s \}$ and $J_t = \max \{ i \mid \gamma_i \geqslant u P(X \geqslant s) \}$;
# 2. since $P(X \geqslant t \mid X \geqslant s) = \dfrac{\gamma_{J_t}}{\gamma_{J_s}} \exp(\lambda_{J_s} (s - t_{J_s}) - \lambda_{J_t} (t - t_{J_t})) = u$, we can rework this to
# $$
# t = t_{J_t} + \frac{\lambda_{J_s}}{\lambda_{J_t}} (s - t_{J_s}) - \frac{1}{\lambda_{J_t}} \log \left( \frac{\gamma_{J_s}}{\gamma_{J_t}} u \right).
# $$
#

StarcoderdataJupyterScriptsDedupFiltered