# Survival Models
## Summary
## The future lifetime random variable
$Def$
*Future Life Time*: time elapsed from now to death

$Def$
*Life Insurance*: A policy that pays benefit upon death of the insured, initiated when bought.

$Def$
*Pure Endowment*: A policy that pays benefit at certain date if the insured is ALIVE on that date.

$\dagger$
现实中Pure Endowment不多

$Def$
*Endowment Insurance* (Endowment): A policy that pays benefit upon death of the insured or at the end of a specified term, whichever occurs first.

$Def$
*Life Annuities*: Anniuty whose payments are contingent on the annuitant being alive on payment date.
***
$Def$
For $x \geq 0$, life aged $x$ is denoted as $(x)$, individual who is insured. And $T_x$ is for that currently aged $x$ life, its *future life time **continuous** **random** variable*, with cumulative distribution function (cdf) $F_x(t)$ and probability density function (pdf) $f_x(t)$.

$\odot$
$T_x \geq 0$, and $T_x$ is a continuous variable.$\square$

$Def$
*Survival Function*: $S_x (t) = 1 - F_x (t)= \mathrm{Pr} [T_x > t]$

$\odot$
$\{T_x\}_{x \geq 0}$ is an infinite collection of $r.v.$.$\square$

$Assumption$
In order to finder the inner relationship between $\{T_x\}_{x \geq 0}$, we want they all are related to $T_0$, which is the future life time for a newborn. Thus:
$$\boxed{ \mathrm{Pr}[ T_x \leq t ] = \mathrm{Pr} [ T_0 \leq x + t \mid T_0 > x ]}$$

$\dagger$
Condition $T_0 > x$ is necessary! Or $T_x$ would not even exist.

$Conclusion$
From the fomula of conditional probability:
$$\mathrm{Pr}[A \mid  B] = \frac{\mathrm{Pr}[A\cup B]} {\mathrm{Pr}[B]},$$
thus:
$$\mathrm{Pr}[T_x \leq t] = \frac{ \mathrm{Pr} [x < T_0 \leq x+t]} {\mathrm{Pr} [T_0 > x]} \Rightarrow F_x(t) = \frac {F_0 (x+t) - F_0 (t)} {1-F_0(x)} \Rightarrow 1 - S_x (t) = \frac {S_0(x) - S_0(x+t)} {S_0(x)} \\ \Rightarrow \boxed { S_x(t) = \frac {S_0(x+t)} {S_0(x)}}$$

$Conclusion$
**Factorization property**:
$$S_x(t+d) = \frac{S_0(x+t+d)} {S_0(x)} = \frac{S_0(x+t)} {S_0(x)} \cdot \frac{S_0(x+t+d)} {S_0(x+t)} = S_x(t) \cdot S_{x+t}(d)$$
$i.e.,$
$$\boxed { S_x(t+d) = S_x(t) \cdot S_{x+t}(d)}$$

$\dagger$
上面的性质就$S_x(t)$有，$F_x(t)$啥的都没有这种性质。

$Conclusion$
Any survival func must satisfy the following:
1. $S_x(0) = 1$, the probability that a life currently aged $x$ survives $0$ years is $1$.
2. $\lim_{t \to \infty} S_x(t) = 0$, all lives eventually die.
3. The survival function must be a *non-increasing* function of $t$.

$\dagger$
可以从Conclusion中得到证明。

$Assumption$
Other assumptions needed:
1. $S_x(t)$ is differentiable for all $t >0$.
2. Sometimes, $\lim_{t \to \infty} tS_x(t) = 0$
3. Sometimes, $\lim_{t \to \infty} t^2S_x(t) = 0$

$\odot$
1. Assumption1 and conclusion 3, we can see that $\frac{\mathrm{d} }{\mathrm{d} t} S_x(t) \leq 0$ for all $t >0$.
2. Assumption2 and 3 is used to make sure that mean and variance exist.$\square$

>**e.g.**:
>$$F_0(t) = \left\{\begin{matrix}
1-(1-\frac{t}{120})^{\frac{1}{6}} & 0 \leq t \leq 120\\
1 & t>120
\end{matrix}\right.$$
> **(a)** $(0)$ survives beyond age 20
>> (a) $S_0(20)$

> **(b)** $(20)$ dies before age 35
>> (b) $F_{20}(15)= 1-S_{20}(15) = 1-\frac{S_{0}(35)} {S_{0}(20)}$

> **(c)** $(30)$ survives beyond age 50
>> (c) $S_{30}(20)=\frac{S_{0}(50)}{S_{0}(30)}$

$\dagger$
Write the $S_x(t)$ *FIRST* then do the calculation

## Force of Mortality
$Conclusion$
$f_x(t) = \frac{\mathrm{d} }{\mathrm{d} t} F_x(t)$, specifically for a newborn:
$$\begin{align}
f_0(t) &= \lim_{\mathrm{d}t \to 0^+} \frac{F_0(t+\mathrm{d}t) - F_0(t)}{\mathrm{d}t}\\
&=\lim_{\mathrm{d}t \to 0^+} \frac{\mathrm{Pr}[t\leq T_0 \leq t+\mathrm{d}t]}{\mathrm{d}t}
\end{align}$$

$Def$
**Force of Mortality**:
$$\begin{align*}
\mu_{x} &= \lim_{\mathrm{d}t \to 0^{+}} \frac {\mathrm{Pr} [ T_0 \leq x + \mathrm{d}x \mid T_0 > x ]} {\mathrm{d}x} \\
&= \lim_{\mathrm{d}t \to 0^{+}} \frac {\mathrm{Pr} [ T_x \leq \mathrm{d}x ]} {\mathrm{d}x} \\
&= \lim_{\mathrm{d}t \to 0^{+}} \frac {F_{x} (\mathrm{d}x)} {\mathrm{d}x} \\
&= \lim_{\mathrm{d}t \to 0^{+}} \frac {1 - S_{x} (\mathrm{d}x)} {\mathrm{d}x} \\
\end{align*}$$
Since $S_0 (x + \mathrm{d}x) = S_{0} (x) \cdot S_{x} (\mathrm{d}x)$,
$$\begin{align*}
\mu_{x} &= \lim_{\mathrm{d}t \to 0^{+}} \frac {S_{0} (x) - S_0 (x + \mathrm{d}x)} {S_{0} (x) \cdot \mathrm{d}x} \\
&= \frac {-1} {S_{0} (x)} \cdot \frac{\mathrm{d} }{\mathrm{d} x} S_{0} (x) \\
&= \boxed{ - \frac {\frac{\mathrm{d} }{\mathrm{d} x} S_{0} (x)} {S_{0} (x)} = \frac {\frac{\mathrm{d} }{\mathrm{d} x} F_{0}(x)} {S_{0} (x)} = \frac {f_{0} (x)} {S_{0} (x)} }\\
&= \boxed{ - \frac{\mathrm{d} }{\mathrm{d} x} \log S_{0} (x) }
\end{align*}$$

$\dagger$
那里可以把分式提出极限号出来是因为我们是对$\mathrm{d}x$求的极限，不是$x$。
这里的求导也不是正常的求导，而是右导数就好了。

$Conclusion$
Now we fix $x$ at $x^{*}$ and see what's $\mu_{x^{*} + t}$
$$\begin{align*}
\mu_{x^{*} + t} &=  \frac {-1} {S_{0} (x^{*} + t)} \cdot \frac{\mathrm{d} }{\mathrm{d} \left( x^{*} + t \right)} S_{0} (x^{*} + t) \\
&= \frac {-1} {S_{0} (x^{*} + t)} \cdot \frac{\mathrm{d} }{\mathrm{d} t} \left( S_{0} (x^{*}) \cdot S_{x^{*}} (t) \right) \\
&= - \frac{S_{0} (x^{*})} {S_{0} (x^{*} + t)} \cdot \frac{\mathrm{d} }{\mathrm{d} t} S_{x^{*}} (t) \\
&= \frac {-1} {S_{x^{*}} (t)} \cdot \frac{\mathrm{d} }{\mathrm{d} t} S_{x^{*}} (t) \\
&= \boxed{ - \frac {\frac{\mathrm{d} }{\mathrm{d} t} S_{x^{*}} (t)} {S_{x^{*}} (t)} = \frac {\frac{\mathrm{d} }{\mathrm{d} t} F_{x^{*}}(t)} {S_{x^{*}} (t)} = \frac {f_{x^{*}} (t)} {S_{x^{*}} (t)} } \\
&= \boxed{ - \frac{\mathrm{d} }{\mathrm{d} t} \log S_{x^{*}} (t) }
\end{align*}$$

$\dagger$
注意都对什么求的导。$\mu_{x}$那里都是导$x$，而这里的$\mu_{x^{*} + t}$都是导$t$.

$Conclusion$
Now we start from $\mu_{x}$ and see what can we get.

Since $\mu_{x} = - \frac{\mathrm{d} }{\mathrm{d} t} \log S_{0} (x)$,
$$\begin{align*}
\int _{0} ^{y} \mu_{x} \mathrm{d} x &= \int _{0} ^{y} - \frac{\mathrm{d} }{\mathrm{d} t} \log S_{0} (x) \mathrm{d} x \\
&= -\log S_{0} (x) |_{0}^{y} \\
&= -\log S_{0} (y) + \log S_{0} (0)
\end{align*}$$

Remember that $\forall x > 0, S_{x} (0) = 1$ and $\log(1) = 0$, so that:
$$\int _{0} ^{y} \mu_{x} \mathrm{d} x = -\log S_{0} (y) \Rightarrow \boxed {S_{0} (y) = e^{-\int_{0}^y \mu_{x} \mathrm{d} x}}$$

$\dagger$
For the fomula above, those two zeros may not be changed to others. See another conclusion below.

$Conclusion$
$$\begin{align*}
S_{x^{*}} (t) &= \frac{S_{0}(x^{*} + t)} {S_{0}(x^{*})} \\
&= \frac{e^{-\int_{0}^{x^{*} + t} \mu_{x} \mathrm{d} x}} {e^{-\int_{0}^{x^{*}} \mu_{x} \mathrm{d} x}} \\
&= e^{-\int_{x^{*}}^{x^{*} + t} \mu_{x} \mathrm{d} x}
\end{align*}$$
$i.e.,$
$$\boxed{ S_{x^{*}} (t) = \exp \left\{ -\int_{0}^{t} \mu_{x+x^{*}} \mathrm{d} x \right\}, or,S_{x} (t) = \exp \left\{ -\int_{0}^{t} \mu_{x+s} \mathrm{d} s \right\} }$$

>**e.g.**
> Suppose that $F_{0}(x) = 1-(1-x/120)^{1/6}$, for $0 \leq x \leq 120$. Derive an expression for $\mu_x$.
>
>$Answer$
>
>First we see that $S_{0}(x) = (1-x/120)^{1/6}$, then
>$$\frac{\mathrm{d} }{\mathrm{d} x}S_{0}\left( x \right )= \frac{1} {6} (1-x/120)^{-5/6}(-\frac{1}{120})$$
>
>and so
>$$\mu_x=\frac{-1} {S_{0}\left( x \right )} \frac{\mathrm{d} }{\mathrm{d} x}S_{0}\left( x \right ) = \frac{1} {720}(1-x/120)^{-1} = \frac{1} {720-6x}$$

###  Gompertz’ law of mortality.
Let $\mu_x = Bc^{x}$, here $x > 0$, $B$ and $c$ are constants such that $0 < B < 1$ and $c > 1$. Then,
$$\begin{align*}
S_{x}\left( t \right) &= \exp \left\{ - \int_{x}^{x+t} \mu_r \; \mathrm{d}r \right\} \\
&= \exp \left\{ - \int_{x}^{x+t} Bc^{r} \; \mathrm{d}r \right\} \\
&= \exp \left\{ - \frac{B}{\log c} c^{r} \left| \right. _{x}^{x+t}  \right\} \\
&= \boxed{\exp \left\{ - \frac{B}{\log c} c^{x}\left( c^{t} - 1 \right ) \right\}}
\end{align*}$$

>**e.g.**: Calculate the survival function and probability density function for $T_x$ using *Gompertz’ law of mortality*, with $B = 0.0003$ and $c = 1.07$, for $x = 20$, $x = 50$ and $x = 80$. Plot the results and comment on the features of the graphs.
>$Answer$
>For $x = 20$, the force of mortality is $\mu_{20 + t} = Bc^{20 + t}$, and the survival function is:
>
>$$S_{20} \left( t \right) = \exp \left\{ - \frac{B}{\log c} c^{20}\left( c^{t} - 1 \right ) \right\}$$
>Then the probability density function is:
>
>$$\mu_{20 + t} = \frac{f_{20} \left( t \right)} {S_{20} \left( t \right)} \Rightarrow f_{20} \left( t \right) = \mu_{20 + t} S_{20} \left( t \right) = Bc^{20 + t} \exp \left\{ - \frac{B}{\log c} c^{20}\left( c^{t} - 1 \right ) \right\}$$
>
>|$S_{x}(t)$|$f_{x}(t)$|
|:---------:|:--------:|
|||

## Actuarial notation
IAN: International Actuarial Notaion

### $_{t}p_{x}$
Survival Function for $T_{x}$, the Probability of $\left( x \right)$ survives $t$ years.

$$_{t}p_{x}=\mathrm{Pr} \left[ T_{x} > t \right] = S_{x}\left( t \right )$$

Further, we use $p_{x}$ with default $t=1$;

### $_{t}q_{x}$
$\mathrm{cdf}$ for $T_{x}$, the Probability of $\left( x \right)$ dies within $t$ years.

$$_{t}q_{x}=\mathrm{Pr} \left[ T_{x} \leq t \right] = F_{x}\left( t \right )$$

Further, we use $q_{x}$ with default $t=1$;

### ${_{u \mid t}}q_{x}$
A deferred mortality probability, that $\left( x \right)$ survives $t$ years and dies within $u$ years after that.

$$_{u \mid t}q_{x}=\mathrm{Pr} \left[ t < T_{x} \leq u + t \right] = S_{x}\left( u \right ) - S_{x}\left( u + t \right )$$

Further, we use ${_{u \mid}}q_{x}$ with default $t=1$, and we never use $_{u \mid t}p_{x}$;

$Conclusion$
1. $\boxed{_{t}p_{x} + _{t}q_{x} = 1}$
2. $\boxed{{_{u \mid t}}q_{x} = {_{u}}p_{x} - {_{u + t}}p_{x}}$
3. Factorization Formula:
$$\boxed{{_{t + u}}p_{x} = {_{t}}p_{x} \cdot {_{u}}p_{ x + t }}$$
4. Force of Mortality:
$$\boxed{\mu_{x} = - \frac{\frac{\mathrm{d}}{\mathrm{d} x} S_{0}\left( x \right )} {S_{0}\left( x \right )} = - \frac{1}{{_{x}}p_{0}} \cdot \frac{\mathrm{d}}{\mathrm{d} x} \left( {_{x}}p_{0} \right )}$$
$$\boxed{\mu_{x + t} = - \frac{\frac{\mathrm{d}}{\mathrm{d} t} S_{x}\left( t \right )} {S_{x}\left( t \right )} = - \frac{1}{{_{t}}p_{x}} \cdot \frac{\mathrm{d}}{\mathrm{d} t} \left( {_{t}}p_{x} \right )} \Rightarrow \boxed{\frac{\mathrm{d}}{\mathrm{d} t} \left( {_{t}}p_{x} \right ) = - {_{t}}p_{x} \cdot \mu_{x + t}}$$
5. $\mathrm{pdf}$
$$\mu_{x + t} = \frac{ \frac{\mathrm{d}}{\mathrm{d} t} \left( {_{t}}q_{x} \right)} {{_{t}}p_{x}} \Rightarrow \boxed{f_{x} \left( t \right) = {_{t}}p_{x} \cdot \mu_{x + t}}$$
6. Survival Function
$$\boxed{ {_{t}}p_{x} = \exp \left \{ - \int _{0} ^{t} \mu_{x+s} \; \mathrm{d}s \right \}}$$
7. $\mathrm{cdf}$
$$F_{x} \left( t \right) = \int _{0} ^{t} f_{x} \left( s \right) \; \mathrm{d}s \Rightarrow \boxed{ {_{t}}q_{x} = \int _{0} ^{t} {_{s}}p_{x} \mu_{ x + s } \; \mathrm{d}s}$$

An explanation on the formula above:
Consider time $s$, where $0 \leq s < t$. The probability that $\left( x \right)$ is alive at time $s$ is ${_{s}}p_{x}$, and the probability that $\left( x \right)$ dies between ages $x + s$ and $x + s + \mathrm{d}s$, having survived to age $x + s$, is (loosely) $\mu_{x + s} \mathrm{d}s$, provided that $\mathrm{d}s$ is very small. Thus ${_{s}}p_{x} \cdot \mu_{x + s} \mathrm{d}s$ can be interpreted as the probability that $\left( x \right)$ dies between ages $x + s$
and $x + s + \mathrm{d}s$. Now, we can sum over all the possible death intervals $s$ to $s + \mathrm{d}s$, which requires integrating because these are infinitesimal intervals, to obtain the probability of death before age $x + t$, $i.e.$, ${_{t}}q_{x} = F_{x} \left( t \right)$

An special case is when $x =1$, we have:
$${_{t}}q_{x} \!\mid_{t=1} = {_{1}q_{x}} = \boxed{ q_{x} = \int _{0} ^{1} {_{s}}p_{x} \mu_{ x + s } \; \mathrm{d}s}$$

When $q_{x}$ is small, it follows that $p_{x}$ is close to $1$, and hence ${_{s}}p_{x}$ is close to $1$ for $0 \leq s < 1$. Thus
$$\boxed{q_{x}\approx \int_{0}^{1} \mu _{x+s} \mathrm{d}s \approx \mu _{x+\frac{1}{2}}}$$

And another special case is that:
$$\boxed{p_{x} = \frac{S_{0} \left( x + 1 \right)} {S_{0} \left( x \right)}}$$

## Mean and standard deviation of $T_x$
$\overset{\circ}{e}_{x} = \mathbb{E}\left[ \,T_{x} \,\right]$, as the **complete expection of life**. Remember we still have $x \geq 0$.

$$
\begin{align*}
\overset{\circ}{e}_{x} &= \int_{-\infty} ^{\infty} t \cdot f_{x} \left( t \right) \mathrm{d}t \\
&= \int_{0} ^{\infty} t \cdot {_{t}p_{x}} \cdot \mu_{x + t} \mathrm{d}t \\
&= \int_{0} ^{\infty} t \cdot \left( -\frac{\mathrm{d}} {\mathrm{d}t} {_{t}q_{x}} \right) \mathrm{d}t \\
&= -\int_{0} ^{\infty} t \,{\mathrm{d} \left( {_{t}q_{x}} \right)} \\
&= -\left( t \cdot {_{t}p_{x}} \left.\right| _{0} ^{\infty} -  \int_{0} ^{\infty} {_{t}p_{x}} \, \mathrm{d}t \right)
\end{align*}
$$

And since we have assumed that $\lim_{t \to \infty}t \cdot {_{t}p_{x}} = 0$, so finally:

$$\boxed{ \overset{\circ}{e}_{x} = \int_{0} ^{\infty} {_{t}p_{x}} \, \mathrm{d}t}$$

Without the assumption before, $\overset{\circ}{e}_{x}$ won't exist. And with similar method and a stronger assumption, $\lim_{t \to \infty}t^2 \! \cdot \! {_{t}p_{x}} = 0$, we can find that:

$$
\begin{align*}
\mathbb{E}\left[ \,T_{x}^{2} \,\right] &= -\left( t^2 \! \cdot \! {_{t}p_{x}} \left.\right| _{0} ^{\infty} -  \int_{0} ^{\infty} {_{t}p_{x}} \, \mathrm{d}t^2 \right) \\
&= - \left( \lim_{t \to \infty} t^2 \! \cdot \! {_{t}p_{x}}  -  \int_{0} ^{\infty} 2t \, {_{t}p_{x}} \, \mathrm{d}t \right) \\
&= \boxed{ \int_{0} ^{\infty} 2t \, {_{t}p_{x}} \, \mathrm{d}t }
\end{align*}
$$

>**e.g.**
>
>$F_{0}\left( x \right) = 1 - \left( 1 - \frac{x} {120} \right)^{1/6}$
>
>First with survival function, which is: $S_{0}\left( x \right) = 1 - F_{0}\left( x \right) = 1 - \left( 1 - \frac{x} {120} \right)^{1/6}$
>
>Then the $S_{x} \left( t \right)$, $i.e.$, ${_{t}p_{x}} = \frac{S_{0}\left( x + t \right)} {S_{0}\left( x \right)} = \left( 1 - \frac{t} {120 - x} \right)^{1/6}$, for $t \in \left[ \, 0, 120-x \right)$. Then:
>
>$\begin{align*}
\overset{\circ}{e}_{x} &= \int_{0} ^{120 - x} \left( 1 - \frac{t} {120 - x} \right)^{1/6} \mathrm{d}t \\
&= \left( 120 - x \right) \cdot \frac{6} {7} \left( 1 - \frac{t} {120 - x} \right)^{7/6}
\bigg|_{0}^{120 - x} \\
&= \frac{6} {7} \left( 120 - x \right)
\end{align*}$
>
>$\begin{align*}
\mathbb{E}\left[ \,T_{x}^{2} \,\right] &= 2 \int_{0} ^{\infty} t \, {_{t}p_{x}} \, \mathrm{d}t \\
&= 2 \int_{0} ^{120 - x} t \, \left( 1 - \frac{t} {120 - x} \right)^{1/6} \, \mathrm{d}t \\
\end{align*}$
>
>We let $\boxed{ u = 1 - \frac{t} {120 - x} \Rightarrow \mathrm{d}u = - \frac{1} {120 - x} \mathrm{d}t }$, and when $t$ goes from $0$ to $120 - x$, $u$ goes from $1$ to $0$. After this we can write:
>
>$\begin{align*}
\mathbb{E}\left[ \,T_{x}^{2} \,\right] &= 2 \int_{1} ^{0} \left( 1 - u \right) \cdot \left( 120 - x \right) \cdot u^{1/6} \cdot - \left( 120 - x \right) \mathrm{d}u \\
&= 2 \left( 120 - x \right)^2 \int_{0} ^{1} \left( 1 - u \right) \cdot u^{1/6} \, \mathrm{d}u \\
& = 2 \left( 120 - x \right)^2 \left( \frac{6} {7} - \frac{6} {13} \right)
\end{align*}$

## Curtate future lifetime
$Def$
**Curtate future lifetime**: the integer part of future lifetime, denoted as $K_{x} = \left \lfloor T_{x} \right \rfloor$

Then we have:

$$\begin{align*}
\mathrm{Pr} \left[ K_{x} = k \right] &= \mathrm{Pr} \left[ k \leq T_{x} < k+1 \right] \\
&= \boxed{ {_{k \mid 1}}q_{x} = {_{k \mid}}q_{x} } \\
&= {_k}p_{x} - {_{k + 1}}p_{x} \\
&= {_k}p_{x} - {_k}p_{x} \cdot {_1}p_{x + k} \\
&= {_k}p_{x} \left( 1 - p_{x + k} \right) \\
&= {_k}p_{x} \cdot q_{x + k}
\end{align*}$$

$i.e.$,

$$\boxed{\mathrm{Pr} \left[ K_{x} = k \right] = {_k}p_{x} \cdot q_{x + k} = {_k}p_{x} \cdot {_1}q_{x + k}}$$

And its expectation is $e_{x}$, without a halo on the top like $\overset{\circ}{e}_{x}$.

$$\begin{align*}
e_{x} &= \mathbb{E}\left[ \,K_{x} \,\right] \\
&= \sum _{k = 0} ^{\infty} k \cdot \mathrm{Pr} \left[ K_{x} = k \right] \\
&= \sum _{k = 0} ^{\infty} k \cdot \Big( {_k}p_{x} - {_{k + 1}}p_{x} \Big) \\
&= 0 + 1\left( {_1}p_{x} - {_{2}}p_{x} \right) + 2\left( {_2}p_{x} - {_3}p_{x} \right) + 3\left( {_3}p_{x} - {_4}p_{x} \right) + \cdots
\end{align*}$$

$i.e.$,

$$\boxed{ e_{x}= \mathbb{E}\left[ \,K_{x} \,\right] = \sum _{k = \mathbf{1}} ^{\infty} {_k}p_{x}}$$

Similarly we can acquire:

$$\boxed{ \mathbb{E}\left[ \,K_{x}^{2} \,\right] = \sum _{k = \mathbf{1}} ^{\infty} \left( 2k-1 \right) \cdot {_k}p_{x} = \left( \sum _{k = \mathbf{1}} ^{\infty} k \cdot {_k}p_{x} \right) - e_{x}}$$

$Compare$
$$\boxed{\overset{\circ}{e}_{x} = \mathbb{E}\left[ \, T_{x} \,\right]= \int_{\mathbf{0}}^{\infty} {_t}p_{x} \mathrm{d}t \,}, \boxed{ \mathbb{E}\left[ \, T_{x}^{2} \,\right] = \int_{\mathbf{0}}^{\infty} 2t \cdot {_t}p_{x} \mathrm{d}t \,}$$

Finally we will derive the relationship between $\overset{\circ}{e}_{x}$ and ${e}_{x}$

$$\begin{align*}
\overset{\circ}{e}_{x} &= \int_{0}^{\infty} {_t}p_{x} \mathrm{d}t \\
&= \sum _{j=0} ^{\infty} \int_{j}^{j+1} {_t}p_{x} \mathrm{d}t \\
&\approx \sum _{j=0} ^{\infty} \left( 1 \times {_{j+1}}p_{x} + \frac{1} {2} \times 1 \times \left( {_j}p_{x} - {_{j+1}}p_{x} \right) \right) \\
&= \sum _{j=0} ^{\infty} \left( \frac{1} {2} \left( {_j}p_{x} + {_{j+1}}p_{x} \right) \right) \\
&= \frac{1} {2} + \sum _{j=1} ^{\infty} {_j}p_{x}
\end{align*}$$

$i.e.$,

$$\boxed{\overset{\circ}{e}_{x} = \frac{1} {2} + e_{x}}$$
***

Another expectation, about $n\textrm{-year}$ annual term curtate expectation of life, denoted as $e_{{x:\overline {n|}}}$, which is the expected life year of $(x)$ given the

$Def$

$L_{x} = \min\left( K_{x},n \right)$

$$\begin{align*}
e_{x:\overline {n|}} &= \mathbb{E} \left[ \, \min\left( K_{x},n \right) \, \right] \\
&= \sum _{l=0} ^{n} l \cdot \mathrm{Pr} \left[ L_{x} = l \right] \\
&= \left( \sum _{l=0} ^{n - 1} l \cdot \Big( {_l}p_{x} - {_{l + 1}}p_{x} \Big) \right) + n \cdot {_n}p_{x} \\
&= \boxed{ \sum _{\mathbf{l=1}} ^{n} {_l}p_{x}}
\end{align*}$$

And similarly, $ \boxed{ \overset{\circ}{e}_{x:\overline {n|}} = \mathbb{E}\left[ \, \min\left( T_{x},n \right) \,\right]= \int_{\mathbf{0}}^{n} {_t}p_{x} \mathrm{d}t \,}$

# Life tables and selection

## Summary
General polulation, select a part of population, like at specific age, or with same level
Deterministic point of view
## Life Tables
Given a survivol model, with survival probability ${_{t}}p_{x}$, we can construct a **life table** for the model from some initial age $x_0$ to a maximum age $\omega$.

$Def$

$\{l_{x}\}_{x \, \in \,\left[ x_0, \omega \right)}$, a function as follows:  Let $l_{x_{0}}$ be an *arbitrary positive* number, called the **radix**. Then, for $0 \leq t \leq \omega − x_0$, define:

$$l_{x_0 + t} = l_{x_0} \cdot {_{t}}p_{x}$$

Then easily we can see that, for fixed $x^*$, $x_0 \leq x^* \leq x^* + t \leq \omega$

$$\begin{align*}
l_{x^* + t} &= l_{x_0 + \left( x^* + t - x_0 \right)} \\
&= l_{x_0} \cdot {_{x^* + t - x_0}}p_{x_0} \\
&= l_{x_0} \cdot {_{x^* - x_0}}p_{x_0} \cdot {_{t}}p_{x^*} \\
&= l_{x^*} \cdot {_{t}}p_{x^*}
\end{align*}$$

$i.e.$,

$$\boxed{l_{x+t} = l_{x} \cdot {_{t}}p_{x} } \Rightarrow \boxed{{_{t}}p_{x} = \frac{l_{x+t}} {l_{x}}}$$

And actually we can interpret this relationship into another *binomial random variable*, $\mathfrak{L}_t$. That is suppose we have $l_x$ independent lives aged $x$, and each life has a probability ${_{t}}p_{x}$ of surviving to age $x + t$. Then the number of survivors to age $x + t$ is $\mathfrak{L}_t \sim \mathrm{B}\left( l_x, {_{t}}p_{x} \right )$. So that its expectation is:

$$\boxed{\mathbb{E}\left[ \, \mathfrak{L}_t \, \right] = l_x \cdot {_{t}}p_{x} = l_{x+t} }$$

$Conclusion$

$$\boxed{ {_{t}}p_{x} = \frac{l_{x+t}} {l_{x}}, {_{t}}q_{x} = \frac{l_{x} - l_{x+t}} {l_{x}} }$$

$$\begin{align*}
{_{u \mid t}}q_{x} &= {_{u}}p_{x} \cdot {_{t}}q_{x + u} \\
&= \frac{l_{x+u}} {l_{x}} \cdot \left( 1 - {_{t}}p_{x + u} \right) \\
&= \frac{l_{x+u} - l_{x+u+t}} {l_{x}} \\
&\text{or} \\
&= {_{u}}p_{x} - {_{u + t}}p_{x} \\
&= \frac{l_{x + u}} {l_{x}} - \frac{l_{x + u + t}} {l_{x}} \\
&= \frac{l_{x + u} - l_{x + u + t}} {l_{x}}
\end{align*}  \\
\boxed{ {_{u \mid t}}q_{x} = \frac{l_{x + u} - l_{x + u + t}} {l_{x}} }
$$

Then we let $d_{x} = l_{x} - l_{x+1}$, death of life in the next year, and we can get that,

$$\begin{align*}
d_{x} &= l_{x} - l_{x+1} \\
&= l_{x} \cdot \left( 1- \frac{l_{x+1}} {l_{x}} \right) \\
&= \boxed{ l_{x} \cdot \left( 1- p_{x} \right) = l_{x} \cdot q_{x} = l_{x} \cdot {_{1}}q_{x} }
\end{align*} \\
\boxed{ l_{x} = \frac{d_{x}} {{_{1}}q_{x}}, q_{x} = \frac{d_{x}} {l_{x}} }$$

>**e.g.**: Given life table
>
>
| $x$|  $l_x$  |  $d_x$ |
|:--:|:--------:|:-----:|
| 30 | 10000.00 | 34.78 |
| 31 |  9965.22 | 38.10 |
| 32 | 9927.12  | 41.76 |
| 33 | 9885.35  | 45.81 |
| 34 | 9839.55  | 50.26 |
| 35 | 9789.29  | 55.17 |
| 36 | 9734.12  | 60.56 |
| 37 | 9673.56  | 66.49 |
| 38 | 9607.07  | 72.99 |
| 39 | 9534.08  | 80.11 |
>
>$l_{40} = l_{39} - d_{39} = 9453.97$
>
>${_10}p_{30} = \frac{l_{40}} {l_{30}} = 0.94540$
>
>$q_{35} = \frac{d_{35}} {l_{35}} = 0.00564$
>
>${_5}q_{30} = \frac{l_{30} - l_{35}} {l_{30}} = 0.02107$
>
>${_{5\mid}}q_{30} = {_{5\mid1}}q_{30} = \frac{l_{35} - l_{36}} {l_{30}} = \frac{d_{35}} {l_{30}} = 0.00552$

## Fractional age assumptions
Linear interpolation between ages, other skills ...

### Uniform distribution of deaths
$Assumption$
**UDD**, the uniform distribution of deaths, most common one.
1. For integer $x$, and for $0 \leq s < 1$, assume that ${_{s}}q_{x} = s \cdot q_{x} = s \cdot {_{1}}q_{x}$.
  >**e.g.**
  > ${_{1/4}}q_{25} = \frac{1} {4} \cdot q_{25} = \frac{1} {4} \cdot {_{1}}q_{25}$
2. From $K_x = \left \lfloor  T_x \right \rfloor$, we define a new variable by $T_x = K_x + R_x$, and here $R_x$ is the fractional point of $T_x$. And we assume that:  for integer $x$, $R_x \sim \mathrm{U} \left( 0, 1 \right)$ , and $R_x$ is independent of $K_x$.

$Claim$
**UDD1** $\Leftrightarrow$ **UDD2**

$Proof$
($\Rightarrow$) Suppose that for all integer $x \ geq 0, x \in \left[ 0 , 1 \right)$, we have ${_{s}}q_{x} = s \cdot q_{x}$.

Then this means, for $0 \leq s < 1$:

$$\begin{align*}
\mathrm{Pr}\left[ R_x \leq s \right] &= \sum_{k = 0}^{\infty} \mathrm{Pr}\left[ R_x \leq s \mathrm{and} K_x = k \right] \\
&= \sum_{k = 0}^{\infty} \mathrm{Pr}\left[ k \leq T_x \leq k+s \right] \\
&= \sum_{k = 0}^{\infty} {_{k}}p_{x} \cdot {_{s}}q_{x+k} \\
&\stackrel{\textbf{UDD1}}=  \sum_{k = 0}^{\infty} {_{k}}p_{x} \cdot s \cdot q_{x+k} \\
&= s \sum_{k = 0}^{\infty} {_{k}}p_{x} \cdot q_{x+k} \\
&= s \sum_{k = 0}^{\infty} \mathrm{Pr}\left[ K_x = k \right] \\
&= s \cdot 1 = s
\end{align*}$$

Thus it is uniformly distributed, and now the rest is to prove the independency. Note that
$$\begin{align*}
\mathrm{Pr}\left[ R_x \leq s \mathrm{and} K_x = k \right] &= \mathrm{Pr}\left[ k \leq T_x \leq k+s \right]\\
&= {_{k}}p_{x} \cdot {_s}q_{x+k} \\
&\stackrel{\textbf{UDD1}}= {_{k}}p_{x} \cdot s \cdot q_{x+k} \\
&= \mathrm{Pr}\left[ K_x = k \right] \cdot \mathrm{Pr}\left[ R_x =\leq s \right]\\
\end{align*}$$

Half done, let's see the opposite direction.

($\Leftarrow$) Suppose $R_x \sim \mathrm{U} \left( 0, 1 \right)$, $R_x$ and $K_x$ are independent. Let $x$  be an integer and for $0 \leq s < 1$, we have

$$\begin{align*}
s \cdot q_{x+k} &= \mathrm{Pr}\left[ T_x \leq s \right]\\
&= \mathrm{Pr}\left[ K_x = 0 \; \textrm{and}\; R_x \leq s \right] \\
&\stackrel{\textbf{UDD2}}= \mathrm{Pr}\left[ K_x = 0 \right] \cdot \mathrm{Pr}\left[ R_x \leq s \right] \\
&= q_{x} \cdot \int_0^s 1 \mathrm{d}x = s \cdot q_{x}
\end{align*}$$

All done. We normally will use **UDD** as $\boxed{{_{s}}q_{x} = s \cdot q_{x} }$
***
$Conclusion$

$$\begin{align*}
{_{s}}q_{x}   &= 1 - {_{s}}p_{x} \\
s \cdot q_{x} &= 1 - \frac{l_{x+s}} {l_x} = \frac{l_{x+s} - l_x} {l_x} \\
s \cdot \frac{d_x} {l_x} &= \frac{l_{x+s} - l_x} {l_x} \\
\end{align*}$$

$i.e.$,

$$\boxed{l_{x+s} = l_{x} - s \cdot d_{x}}$$

>**e.g.** Given $P_{40} = 0.999473$,
>
>$$\begin{align*}
{_{0.4}}q_{40.2}   &= 1 - {_{0.4}}p_{40.2} \\
&= 1- \frac{l_{40.6}} {l_{40.2}} \\
&= 1- \frac{{_{0.6}}p_{40}} {{_{0.2}}p_{40}} \\
&= 1- \frac{1 - {_{0.6}}q_{40}} {1 - {_{0.2}}q_{40}} \\
&\stackrel{\textbf{UDD}}= 1- \frac{1 - {0.6} q_{40}} {1 - {0.2} q_{40}} \\
&= 1- \frac{1 - 0.6 (0.00527)} {1 - 0.2(0.00527)} \\
& \approx 2.108 \times 10^{-4}
\end{align*}$$

$Conclusion$
We can also differentiate $\boxed{{_{s}}q_{x} = s \cdot q_{x} }$ with $s$ on both side, easily we can get:

$$\frac{ \mathrm{d}} {\mathrm{d}s} {_s}q_x = q_x,\; 0 \leq s \leq 1$$

Left-hand side is the derivative of culmulative density function, so we finally can see that:

$$\boxed{ q_{x} = f_{x} \left( s \right) = {_s}p_x \cdot \mu_{x+s} }, 0 \leq s < 1$$

$\odot$
And this also demonstrate that density function is a constant, $i.e.$, the variable $R_x$ is uniformly distributed.$\square$

|                                                                                Probability                                                                                |             International Actuarial Notation (IAN)            |                   Meaning                  |                                                                 Note                                                                |
|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|:-------------------------------------------------------------:|:------------------------------------------:|:-----------------------------------------------------------------------------------------------------------------------------------:|
|                                                                                $(x)$                                                                                |                             $(x)$                             |               a Life aged $x$              |                                                              $x \geq 0$                                                             |
|                                                                                $T_x(t)$                                                                                |                            $T_x(t)$                           |               Future Lifetime              |                                                          continuous $r.v.$                                                          |
|                                                                                $F_x(t)$                                                                                |                         ${_{t}}q_{x}$                         |      Distribution Function of $T_x(t)$     |                                            the probability of $(x)$ dies within $t$ years                                           |
|                                                                                $S_x(t) = 1 - F_x(t)$                                                                                |                ${_{t}}p_{x} = 1 - {_{t}}q_{x}$                |        Survival Function of $T_x(t)$       |                                             the probability of $(x)$ survives $t$ years                                             |
|                                                                                $\mathrm{Pr} \left[ t < T_{x} \leq u + t \right] = S_{x}(u) - S_{x}(u + t)=F_{x}(u + t) - F_{x}(u)$                                                                                |                     ${_{u \mid t}\;}q_{x}$                    | Deferred Mortality Probability of $T_x(t)$ | the probability of $(x)$ survives $\mathbf{u}$ years and dies within $\mathbf{t}$ years after that; no such ${_{u \mid t}\;}p_{x}$. |
|                                                                                $S_x(t+d) = S_x(t) \cdot S_{x+t}\;(d)$                                                                                | ${_{t + u}\;\,}p_{x} = {_{t}}p_{x} \cdot {_{u}\;}p_{ x + t }$ |            Factorization Formula           |                                                                                |
|                                  $\begin{align}\mu_{x} &= - \frac {\frac{\mathrm{d} }{\mathrm{d} x} S_{0} (x)} {S_{0} (x)} = \frac {\frac{\mathrm{d} }{\mathrm{d} x} F_{0}(x)} {S_{0} (x)} = \frac {f_{0} (x)} {S_{0} (x)} \\ &= - \frac{\mathrm{d} }{\mathrm{d} x} \log S_{0} (x) \\ S_{0} (y) &= \exp \left\{-\int_{0}^y \mu_{x} \;\mathrm{d} x\right\}\end{align}$                                  |                                                               |                                            |                                                                                |
| With fixed $x^{*}$, $\begin{align} \mu_{x^{*} + t} &= - \frac {\frac{\mathrm{d} }{\mathrm{d} t} S_{x^{*}} (t)} {S_{x^{*}} (t)} = \frac {\frac{\mathrm{d} }{\mathrm{d} t} F_{x^{*}}(t)} {S_{x^{*}} (t)} = \frac {f_{x^{*}} (t)} {S_{x^{*}} (t)} \\ &= - \frac{\mathrm{d} }{\mathrm{d} t} \log S_{x^{*}} (t) \\ S_{x} (t) &= \exp \left\{ -\int_{0}^{t} \mu_{x+s} \;\;\mathrm{d} s \right\} \end{align}$ |                                                               |                                            |                                                                                |

```python

```


# PharmSci 175/275 (UCI)
## What is this??
The material below is a supplement to the quantum mechanics (QM) lecture from Drug Discovery Computing Techniques, PharmSci 175/275 at UC Irvine.
Extensive materials for this course, as well as extensive background and related materials, are available on the course GitHub repository: [github.com/mobleylab/drug-computing](https://github.com/mobleylab/drug-computing)

# Using QM in Python

This material adapted (under CC-BY) from a [workshop example](https://github.com/QCMM/workshop2017/blob/master/Theory_electronic_structure_day3/i_inter_es.ipynb) of Stefano Vogt-Giesse (University of Concepcion) from the December, 2017 QCMM workshop in Chile, available under the [workshop repository](https://github.com/QCMM/workshop2017)

### Instructor: David L. Mobley

### Contributors to these materials:
- Stefano Vogt-Giesse
- David L. Mobley

# Exploring molecular interactions using electronic structure methods.

For today's activity we will use the package of program psi4, so we will need to install it first. Assuming you already have anaconda/miniconda installed, you can install as follows (in a new conda environment, `p4env`:

**conda create -n psi4 psi4 psi4-rt jupyter matplotlib -c psi4/label/dev -c psi4**

(Note psi4 seems to be somewhat incompatible with the other software we are using in this course, so a separate environment is needed.)

This will install all the psi4 binaries and a python module which can be imported from the notebook. Then activate the environment via `conda activate psi4`.

You also need to ensure it works in your jupyter notebook, which you can do via (in the terminal, with your `psi4` environment active):
```
conda install ipykernel --name psi4
python -m ipykernel install --user
```

To finish the installation you need to provide a scratch directory in your `~/.bash_profile`, for example (assuming you want your scratch directory in this space):

**export PSI_SCRATCH=/home/user_name/scratch/psi4**

Then type `source ~/.bash_profile`. Now you may open jupyter-notebook and install psi4.

Every time you wish to use psi4 you will need to `conda activate psi4`.

```python
import psi4
import numpy as np
```

# 1. Compute the energy of a diatomic molecule

As a first example we will compute the scf energy of the diatómic molecule hydrogen flouride (HF):

```python
# ==> Basic Psi4 options <==
# Memory
psi4.set_memory(int(5e8))
numpy_memory = 500

# Output
psi4.core.set_output_file('output.dat', False)

# Geometry input
hf_mol = psi4.geometry("""
0 1
H
F 1 0.917
""")

energy_hf_mol , wfn_hf_mol = psi4.energy('mp2/cc-pvtz', return_wfn=True)
print(energy_hf_mol) #Energy in Hartrees
```

    -100.3429063303609

This corresponds to the MP2/cc-pVTZ energy for this system (HF).  Input coordinates are given in [Z matrix format](https://en.wikipedia.org/wiki/Z-matrix_(chemistry)), using internal coordinates (in this case just a bond distance).

### Exercise: Compute the energy of other diatomic molecules using similar methods
You might consider computing the energy of of F$_2$ and N$_2$ using the cc-pVDZ and cc-pVTZ methods.

# 2. Compute the dipole and quadrupole moment of diatomic molecules

Since we are intrested in studying long range  molecular interactions using classical elctrodynamics, it is necessary
to compute the dipole and quadrupole moments. Quantum mechanically the dipole can be computed using the one electron dipole operator:

\begin{equation}
\hat{\mu} = \sum_i q_i r_i
\end{equation}

where $q_i$ is the charge of the particle and $r_i$ is the position vector of the particle.  The dipole moment can be computed using the wavefunction through the expectation value of the operator $\mu$.

\begin{equation}
\mu = <\psi|\hat{\mu}|\psi>
\end{equation}

In psi4 we can obtain the dipole moment from the wafefunction object that was defined above

```python
psi4.oeprop(wfn_hf_mol, 'DIPOLE', 'QUADRUPOLE', title='HF SCF')

mux = psi4.core.get_variable('HF SCF DIPOLE X') # in debye
muy = psi4.core.get_variable('HF SCF DIPOLE Y')
muz = psi4.core.get_variable('HF SCF DIPOLE Z')
quad_zz = psi4.core.get_variable('HF SCF QUADRUPOLE ZZ')
```

```python
print(muz)
print(quad_zz)
mu = (np.sqrt(mux**2 + muy**2 + muz**2))
print(mu)
```

    -1.9411548174366635
    -3.271015995238057
    1.94115481744

# 3.  Compute a potential energy surface of HF dimer.

In order to study the physical interactions between two molecules it is convinient to draw
a potential energy surface along the interaction coordinate. In this section we will
obtain a potential energy profile for the most favorable dipole-dipole interaction, which is the
horizontal orientation with oposing dipole vectors, HF---FH. First we need to define a list containing
the distances between both dimers for which the energy will be obtained.

```python
hf_dimer = psi4.geometry("""
        0 1
        H
        F 1 0.917
        H 2 R 1 180.0
        F 3 0.917 2 180.0 1 0.0
        """)
```

Next, we write a loop and in each step of the loop we compute the energy at the mp4 level of theory.

```python
energy = []
dist   = []

Rval = np.arange(1.5,10.0,0.1)

for d in Rval:
    hf_dimer.R = d
    psi4.set_options({'freeze_core': 'True'})
    en = psi4.energy('scf/cc-pvtz')
    print(en)
    print(d)
    energy.append(en)
    dist.append(d)

```

    -200.1159400762018
    1.5
    -200.1190819646694
    1.6
    -200.12084511069853
    1.7
    -200.1217418230204
    1.8
    -200.12210646265714
    1.9
    -200.12214740628218
    2.0
    -200.12199128622555
    2.1
    -200.12171855348862
    2.2
    -200.1213834825437
    2.3
    -200.12102248398264
    2.4
    -200.1206584434578
    2.5
    -200.12030471830317
    2.6
    -200.11996870223288
    2.7
    -200.11965427034008
    2.8
    -200.11936321363657
    2.9
    -200.11909600295286
    3.0
    -200.1188522860765
    3.1
    -200.11863115701004
    3.2
    -200.11843131717893
    3.3
    -200.11825114501002
    3.4
    -200.11808882589855
    3.5
    -200.11794245507383
    3.6
    -200.11781018000022
    3.7
    -200.11769030206548
    3.8
    -200.11758132579342
    3.9
    -200.11748198075108
    4.0
    -200.11739119292795
    4.1
    -200.11730805328622
    4.2
    -200.11723176731104
    4.3
    -200.11716164900196
    4.4
    -200.11709708263894
    4.5
    -200.1170375095619
    4.6
    -200.1169824408411
    4.7
    -200.11693143092367
    4.8
    -200.11688408496144
    4.9
    -200.11684005298991
    5.0
    -200.1167990260822
    5.1
    -200.1167607297471
    5.2
    -200.11672492543434
    5.3
    -200.11669140001007
    5.4
    -200.1166599601226
    5.5
    -200.11663044555814
    5.6
    -200.11660269950414
    5.7
    -200.11657658816026
    5.8
    -200.1165519929236
    5.9
    -200.1165288021483
    6.0
    -200.11650691710236
    6.1
    -200.11648624307801
    6.2
    -200.11646669629727
    6.3
    -200.11644820451244
    6.4
    -200.11643069564883
    6.5
    -200.11641410467115
    6.6
    -200.1163983745758
    6.7
    -200.11638344606672
    6.8
    -200.11636927063006
    6.9
    -200.116355801285
    7.0
    -200.1163429924834
    7.1
    -200.11633081020645
    7.2
    -200.11631921073243
    7.3
    -200.11630816039016
    7.4
    -200.11629762874782
    7.5
    -200.11628758588446
    7.6
    -200.1162780030017
    7.7
    -200.11626885594384
    7.8
    -200.116260117293
    7.9
    -200.11625176417053
    8.0
    -200.11624377692485
    8.1
    -200.1162361367973
    8.2
    -200.11622882502687
    8.3
    -200.1162218216226
    8.4
    -200.11621511335397
    8.5
    -200.11620868510147
    8.6
    -200.11620251717724
    8.7
    -200.11619660443722
    8.8
    -200.11619092565678
    8.9
    -200.11618547548989
    9.0
    -200.11618024015092
    9.1
    -200.11617521010604
    9.2
    -200.11617037397684
    9.3
    -200.11616572549198
    9.4
    -200.11616124982714
    9.5
    -200.1161569453809
    9.6
    -200.11615279951042
    9.7
    -200.1161488082223
    9.8
    -200.11614496206
    9.9

Now we are ready to plot the potential energy profile. We will use the matplotlib python library for this
purpose. The function `ref_zero_kcal` transforms the energy which is in Hartee to kcal/mol and takes the
energy of the dimer with the farthest separation as the reference energy.

```python
import matplotlib.pyplot as plt
%matplotlib inline
```

```python
def ref_zero_kcal(en_list):
    energy_kcal = []
    for x in range(len(en_list)):
        energy_kcal.append((en_list[x] - en_list[-1])*627.51)
    return energy_kcal

energy_kcal = ref_zero_kcal(energy)
plt.plot(dist,energy_kcal)
plt.xlabel('Distance')
plt.ylabel('Energy (kcal/mol)')
```

There are many more examples/tutorials in the Psi4 GitHub repositories, especially see `Tutorials` under the (psi4numpy repository](https://github.com/psi4/psi4numpy) for many Jupyter notebooks.


```python
from sympy.physics.units import *
from sympy import *

import decimal
from decimal import Decimal as DX
def iso_round(obj, pv, rounding=decimal.ROUND_HALF_EVEN):
    import sympy
    """
    Rounding according to: DIN EN ISO 80000-1:2013-08
    pv: place value = Rundestellenwert
    """
    # pv     :  Rounded digit
    # 1      :  last digit before decimal
    # 0.1    :  1st digit after decimal
    # 0.01   :  2nd digit
    # 0.001  :  3rd digit
    assert pv in set([ # round to:
        100,           #  3rd last digit before decimal
        10,            #  2nd last
        1,             #  last
        0.1,           #  1st digit after decimal
        0.01,          #  2nd
        0.001,         #  3rd
        0.0001,        #  4th
        0.00001,       #  5th
        0.000001,      #  6th
        0.0000001,     #  7th
        0.00000001,    #  8th
        0.000000001,   #  9th
        0.0000000001,  # 10th
        ])
    try:
        tmp = float(obj)
        tmp = DX(str(tmp))
        obj = tmp.quantize(DX(str(pv)), rounding=rounding)
    except:
        for i in range(len(obj)):
            tmp = float(obj[i])
            tmp = DX(str(tmp))
            obj[i] = tmp.quantize(DX(str(pv)), rounding=rounding)
    return obj

# LateX:
kwargs = {"mat_str": "bmatrix",  "mat_delim": ""}

kilo = 1000
mega = 1000*1000
giga = 1000*1000*1000
(mm, cm) = (m/1000, m/100)

deg = pi/180
Newton = kg*m/s**2
Pa = Newton/m**2
MPa = mega*Pa
GPa = giga*Pa
kN = kilo*Newton

###

half = S(1)/2

A = 100 * Matrix([
    [2, 1, 0],
    [1, half, 0],
    [0, 0, 1]
    ]) *MPa

pprint("\nEigenvalues in MPa:")
for ev in A.eigenvals():
    pprint(ev/MPa)

# Eigenvalues in MPa:
# 250
# 100
# 0

```


```python
import sympy as sym
from sympy.utilities.lambdify import lambdify
from functools import partial
from itertools import product
import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
```

```python
%matplotlib inline
sym.init_printing(use_unicode=True)
mpl.style.use('seaborn-talk')

```

```python

```

```python
x, t, z, = sym.symbols('x t z')
pi, psi, phi,theta1, theta2 = sym.symbols('pi, psi, phi theta_1, theta_2', real=True)
c, rho, delta = sym.symbols('c, rho, delta', positive=True, real=True)
```

## Define the relevant variables

```python
a = (rho * x) / (1 + c * x)
a
```

```python
b = delta * sym.log(1 + c * x)
b
```

```python
ratio1 = phi / sym.sqrt(c * ( 1 + phi))
ratio1
```

```python
alpha = x * (ratio1 + (1-phi**2)*(theta2 - sym.Rational(1,2))) - sym.Rational(1/2) * x**2 * (1-phi**2)
alpha
```

```python
arg1 = theta1 + alpha.replace(x, theta2)
arg1
```

```python
beta = x * (a.replace(x, - ratio1 + arg1) - a.replace(x, arg1))
beta
```

```python
gamma = x * (b.replace(x, - ratio1 + arg1) - b.replace(x, arg1))
gamma
```

```python
gamma.replace(c,1).replace(delta, 1).replace(phi, -.2)
```

```python
beta.replace(c,1).replace(delta, 1).replace(rho, .9).replace(phi, -.2)
```

## I now take the derivatives

### a(x)

```python
a.diff(rho,1)
```

```python
a.diff(c, 1)
```

### b(x)

```python
b.diff(delta, 1)
```

```python
b.diff(c, 1)
```

### $\alpha(x)$

```python
theta2val = sym.solveset(alpha.diff(phi, 1), theta2).args[0]
```

```python
theta2val
```

```python
alpha.diff(theta2, 1)
```

```python
alpha.diff(c, 1)
```

### $\beta(x)$

```python
beta.diff(phi, 1).simplify()
```

```python
beta.diff(phi,1)
```

```python
beta.diff(c,1)
```

```python
sym.diff(beta, theta1)
```

```python
sym.diff(beta, theta2)
```

### $\gamma(x)$

```python
sym.diff(gamma, phi)
```

```python
sym.sympify(sym.diff(gamma, theta1).replace(theta2,theta2val).replace(x,1))
```

```python
theta2val
```

```python
sym.diff(gamma, theta2)
```

```python
sym.diff(gamma, delta)
```

## Lack of identification issue

```python
sym.diff(gamma.replace(phi, 0), theta1)
```

```python
sym.diff(beta.replace(phi, 0), theta1)
```

```python
sym.diff(gamma.replace(theta1, 0), theta2)
```

### Parameter Estimates from Renault (Gmm Step)

rho : 0.95
delta : 1.25
phi : -0.30
theta2 : 0.53

sym.diff(gamma, theta1).replace(phi, 0)

```python
gamma_func = partial(lambdify((c, delta, rho, theta2, theta1, x, phi), sym.diff(gamma, theta1)),
                     1, 1.25, .95, .53, -7, .5)
```

```python
gamma_diff_fig, gamma_diff_ax = mpl.pyplot.subplots()
xvals = np.linspace(-.99, .99, 100)
yvals = [gamma_func(val) for val in xvals]
gamma_diff_ax.set_xlabel('$\phi$')
gamma_diff_ax.plot(xvals, yvals)
gamma_diff_ax.axhline(0, color='grey')
gamma_diff_ax.axvline(0, color='grey')
gamma_diff_ax.set_yticklabels([])
gamma_diff_fig.savefig('../doc/figures/gamma_diff_theta2.tmp.pdf', frameon=False, transparent=True,
                       bbox_inches='tight',pad_inches=0)
```

```python
beta_func = partial(lambdify((c, delta, rho, theta2, theta1, x, phi), sym.diff(beta, theta1)),
                     1, 1.25, .95, .53, -7, .5)
```

```python
beta_diff_fig, beta_diff_ax = mpl.pyplot.subplots()
beta_xvals = np.linspace(-.99, .99, 100)
beta_yvals = [beta_func(val) for val in xvals]
beta_diff_ax.set_ylabel('$\phi$')
beta_diff_ax.plot(xvals, yvals)
beta_diff_ax.axhline(0, color='grey')
beta_diff_ax.axvline(0, color='grey')
```

### Solving for $\psi$

```python
phi
```

```python
pi, psi, phi,theta, alpha = sym.symbols('pi, psi, phi theta, alpha', real=True)

```

```python
alpha = psi * x + (1 - phi**2 / 2) * x**2
```

```python
alpha
```

```python
c
```

```python
chi = 1 + c * (pi + alpha.replace(x,theta))
```

```python
chi
```

```python
sym.Rational(3/2)
```

```python
umm = -psi + (phi * chi**sym.Rational(3/2)) / (sym.sqrt(c) * sym.sqrt(chi**2 - rho)) + (1 - phi**2) /2 - ( 1 - phi**2) * theta
```

```python
umm
```

```python
umm.replace(phi, 0)
```

```python
umm.diff(phi)
```

```python
sym.solveset(-psi + phi / sym.sqrt((c / chi) * (3 - rho / chi**2)) + (1 - phi**2) / 2 - ( 1 - phi**2) * theta, psi)
```

```python
psi.simplify()
```

```python

```