# Financial Economics HW_01

**11510691 程远星$\DeclareMathOperator*{\argmin}{argmin}
\DeclareMathOperator*{\argmax}{argmax}
\newcommand{\using}[1]{\stackrel{\mathrm{#1}}{=}}
\newcommand{\ffrac}{\displaystyle \frac}
\newcommand{\space}{\text{ }}
\newcommand{\bspace}{\;\;\;\;}
\newcommand{\QQQ}{\boxed{?\:}}
\newcommand{\void}{\left.\right.}
\newcommand{\CB}[1]{\left\{ #1 \right\}}
\newcommand{\SB}[1]{\left[ #1 \right]}
\newcommand{\P}[1]{\left( #1 \right)}
\newcommand{\dd}{\mathrm{d}}
\newcommand{\Tran}[1]{{#1}^{\mathrm{T}}}
\newcommand{\d}[1]{\displaystyle{#1}}
\newcommand{\EE}[2][\,\!]{\mathbb{E}_{#1}\left[#2\right]}
\newcommand{\Var}[2][\,\!]{\mathrm{Var}_{#1}\left[#2\right]}
\newcommand{\Cov}[2][\,\!]{\mathrm{Cov}_{#1}\left(#2\right)}
\newcommand{\Corr}[2][\,\!]{\mathrm{Corr}_{#1}\left(#2\right)}
\newcommand{\I}[1]{\mathrm{I}\left( #1 \right)}
\newcommand{\N}[1]{\mathrm{N} \left( #1 \right)}
\newcommand{\ow}{\text{otherwise}}\void^\dagger$**

## Question 2.1

$\bspace$Firstly they share the same domian $c \in \mathbb{R}_+^{N}$, and the range, $\mathbb{R}$. Then we consider two comsumption plans $a,b \in \mathbb{R}_+^{N}$. Suppose that $a \succsim b$, then from the definition of utility function $U$ we have $a \succsim b \Leftrightarrow U\P{a} \geq U\P{b}$, such that $V\P{a} = f\P{U\P{a}} \geq f\P{U\P{b}} = V\P{b}$

## Question 2.2

$\P{1}$ instatiability

$\bspace$Suppose there're two possible consumption plan $c_i = \SB{c_{0,i};c_{1a,i};c_{1b,i}}, i=1,2$ and let $c_1 \geq c_2$ meaning that $c_{0,1} \geq c_{0,2}$ and $c_{1a,1} \geq c_{1a,2}$ and $c_{1b,1} \geq c_{1b,2}$. Then for the first Utility function,

$$U\P{c_1} = \log c_{0,1} + \ffrac{1} {2} \P{\log c_{1a,1} + \log c_{1b,1}} \geq \log c_{0,12} + \ffrac{1} {2} \P{\log c_{1a,2} + \log c_{1b,2}} = U\P{c_2} $$

$\bspace$And for the second one, when $1-\gamma > 0$, $\ffrac{1} {1-\gamma}x^{1-\gamma}$ is an increasing function on $x \geq 0$ and when $1-\gamma < 0$, $\ffrac{1} {1-\gamma}x^{1-\gamma}$ is still an increasing function on $x \geq 0$. Thus

$$U\P{c_1} = \ffrac{1} {1-\gamma}c_{0,1}^{1-\gamma} + \ffrac{1} {2} \P{\ffrac{1} {1-\gamma}c_{1a,1}^{1-\gamma} + \ffrac{1} {1-\gamma}c_{1b,1}^{1-\gamma}} \geq \ffrac{1} {1-\gamma}c_{0,2}^{1-\gamma} + \ffrac{1} {2} \P{\ffrac{1} {1-\gamma}c_{1a,2}^{1-\gamma} + \ffrac{1} {1-\gamma}c_{1b,2}^{1-\gamma}} = U\P{c_2} $$

$\bspace$For the third one, when $\alpha > 0$, $-e^{-\alpha x}$ is an increasing function on $x \geq 0$, however it turns to decreasing when $\alpha < 0$ which fails the instatiability. So one possible solution is to let $U\P{c} = -\alpha e^{-\alpha c_{\void_0}}-\ffrac{\alpha} {2} \P{e^{-\alpha c_{\void_{1a}}} + e^{-\alpha c_{\void_{1b}}}}$. Then we can easily prove that $U\P{c_1} \geq U\P{c_2}$. But to prove the convexity, we'd better assume that $\alpha > 0$.

$\P{2}$ continuity

$\bspace$Consider a series of consumption plan $\CB{c_{i}}, i=1,2,\cdots$ with a limit $\lim\limits_{n \to \infty} c_n= c_{\cdot}$ meaning that $\lim\limits_{n \to \infty} c_{j,n}= c_{j}$ for $j = 0,1a,1b$. For the first utility function, by the property of $\log$ function, we have $\lim\limits_{n \to \infty} \log c_{j,n} = \log \P{ \lim\limits_{n \to \infty} c_{j,n}} = \log c_{j}$ and thus we have their linear combination $\lim\limits_{n \to \infty}U\P{c_n} = U\P{c}$

$\bspace$For the second and third one, since $\ffrac{1} {1-\gamma}x^{1-\gamma}$ and $-e^{-\alpha x}$ are continuous on the domain, we can use the same method to draw the conclusion.

$\P{3}$ convexity

$\bspace$Suppose that $U\P{c_1} \geq U\P{c_2}$, and let $\beta \in \P{0,1}$, then for the first utility function, since $\log$ funciton is convex, we have

$$\begin{align}
U\P{\beta c_1 + \P{1-\beta}c_2} &= \log\P{\beta c_{\void_{0,1}} + \P{1-\beta}c_{\void_{0,2}}\,} +\ffrac{1} {2}\SB{ \log \P{\beta c_{\void_{1a,1}} + \P{1-\beta}c_{\void_{1a,2}}\,} + \log \P{\beta c_{\void_{1b,1}} + \P{1-\beta}c_{\void_{1b,2}}\,}} \\
& \geq \beta \P{\log c_{\void_{0,1}} + \ffrac{1} {2}\log c_{\void_{1a,1}} + \ffrac{1} {2}\log c_{\void_{1b,1}}} + \P{1-\beta} \P{ \log c_{\void_{0,2}} + \ffrac{1} {2}\log c_{\void_{1a,2}} + \ffrac{1} {2}\log c_{\void_{1b,2}}} \\
&= \beta U\P{c_1} + \P{1-\beta}U\P{c_2} \geq U\P{c_2}
\end{align}$$

$\bspace$Similarly, for the next two utility functions, $\P{\ffrac{1} {1-\gamma}x^{1-\gamma}}'' = -\ffrac{1} {\gamma}x^{-\gamma - 1} < 0$ and $\P{-e^{-\alpha x}}'' = -\alpha^2 e^{-\alpha x} <0$, thus all three are convex and we can prove the convexity for the later two utility functions.

## Question 2.4

$\P{1}$

$\bspace$Since $\Omega = 3$ we have the payoff space: $\mathbb{R}^3$.

$\P{2}$

$$X \equiv \SB{X_{\cdot,1},X_{\cdot,2}} = \begin{bmatrix}
1 & 1 \\
1 & 2 \\
1 & 3
\end{bmatrix}$$

$\P{3}$

$\bspace$The payoff vector is $X\begin{bmatrix}
\theta_1 \\
\theta_2
\end{bmatrix} = \begin{bmatrix}
\theta_1 + \theta_2 \\
\theta_1 + 2\theta_2 \\
\theta_1 + 3\theta_2
\end{bmatrix}$. And the price is $\SB{S_1,S_2}\begin{bmatrix}
\theta_1 \\
\theta_2
\end{bmatrix} = S_1\theta_1 + S_2 \theta_2$

$\P{4}$

$\bspace$The market portfolio is $\d{\sum_{k=1}^{K}}\begin{bmatrix}
1 \\
2
\end{bmatrix} =  \begin{bmatrix}
K \\
2K
\end{bmatrix}$. Its payoff vector is $\begin{bmatrix}
K + 2K \\
K + 2\times 2K \\
K + 3\times 2K
\end{bmatrix} = \begin{bmatrix}
3K \\
5K \\
7K
\end{bmatrix}$ and the overall price is $\SB{S_1,S_2}\begin{bmatrix}
K \\
2K
\end{bmatrix} = \P{S_1 + 2S_2}K$.

$\P{5}$

$\bspace$Since there're only two securities thus $N=2$. Denote $\theta = \begin{bmatrix}
\theta_1 \\
\theta_2
\end{bmatrix} \in \mathbb{R}^2$ then we have the marketed payoff $M = \CB{Y =X\theta: \theta \in \mathbb{R}^2}$

$\P{6}$

$\bspace$When shorting is not allowed, the domain of $\theta$ is limited to the first quadrant and thus $M = \CB{Y =X\theta: \theta \in \mathbb{R}_+^2}$

$\P{7}$

$$X = \begin{bmatrix}
1 & 1 & 0 \\
1 & 2 & 0 \\
1 & 3 & 1
\end{bmatrix}, \theta = \begin{bmatrix}
\theta_1 \\
\theta_2 \\
\theta_3
\end{bmatrix} \in \mathbb{R}^3$$

thus the new marketed payoff is $M = \CB{Y =X\theta: \theta \in \mathbb{R}^3}$

## Question 2.7

$\P{1}$

$\bspace$Without trading his consumption plan can only be $\SB{100,1}$ thus $U^a = \log 100 + \rho \log 1 = \log 100$

$\P{2.1}$

$\bspace$His budget set is $B\P{\begin{bmatrix}
100\\
1
\end{bmatrix}, \CB{[1+r_F],[1]}} = \CB{c \geq 0: c_0 = 100 - \theta, c_1 = 1 + \theta\P{1 + r_F}, \theta \in \mathbb{R}}$. His current wealth is (from the formula in the next chapter!) $w = e_0 + \Tran\phi e_1 = 100 + \phi_1 \times 1 = 100 + \ffrac{1} {1+r_F}$.

$\P{2.2}$

$\bspace$The optimization problem now is

$$
\begin{array}{cl}
\d{\max_{\theta}} & U^b\P{c} = \log\P{100 - \theta} + \rho \log\P{1 + \theta\P{1 + r_F}} \\
s.t. & c_0 = 100 - \theta, c_1 = 1 + \theta\P{1 + r_F} \\
& c_0,c_1 \geq 0
\end{array}
$$

$\bspace$We don't need lagrangian multipliers😂😂😂. It's obvious that $\ffrac{\partial^2 U^b\P{c}} {\partial \theta^2} = -\ffrac{1} {\P{\theta - 100}^2} - \ffrac{\rho\P{1+r_F}^2} {\P{1+\theta\P{1+r_F}}^2} < 0$ thus letting its first order partial derivative equal to zero gives $\ffrac{\partial U^b\P{c}} {\partial \theta} = \ffrac{1} {\theta - 100} + \ffrac{\rho \P{1+r_F}} {1 + \theta\P{1+r_F}} = 0$. Solve the equation and we can obtain the optimized saving, which is just the number of the security you're going to hold here:

$$s = \theta = \ffrac{100\rho \P{1+r_F} - 1} {\P{1+\rho}\P{1+r_F}}\\
c_0 = 100 - \ffrac{100\rho \P{1+r_F} - 1} {\P{1+\rho}\P{1+r_F}} = \ffrac{100\P{1+r_F} + 1} {\P{1+\rho}\P{1+r_F}} = \ffrac{1} {1+\rho} w\\
c_1 = 1 + \ffrac{100\rho \P{1+r_F} - 1} {\P{1+\rho}} = \rho\ffrac{100\P{1+r_F}+1}{1+\rho} = \ffrac{\rho\P{1+r_F}} {1+\rho}w \\[0.8em]
U^b\P{c} = \log\P{\ffrac{w} {1+\rho}} + \rho\log\P{\ffrac{\rho\P{1+r_F}} {1+\rho}w}
$$

$\P{2.3}$

$\bspace$Since we already have the expression for $\theta$, we can see that it will rise with the rise of $r_F$. And assuming $\rho >0$, we have the same conclusion that it will rise with the rise of $\rho$.

$\P{3}$

$\bspace$$U^a\P{c}$ is just the case when $\rho \P{1+r_F} = 0.01$ thus since $U^b\P{c}$ is the optimized solution it's obvious $U^b\P{c} \geq U^a\P{c}$.

$\P{4}$

$\bspace U^b\P{w-g} = \log\P{\ffrac{w-g} {1+\rho}} + \rho\log\P{\ffrac{\rho\P{1+r_F}} {1+\rho}\P{w-g}} = \log 100 \Rightarrow g = w - \P{1+\rho}\P{\ffrac{100} {\rho^{\rho}\P{1+r_F}^{\rho}}}^{\ffrac{1} {1+\rho}}$

$\bspace$Obviously it increases with $r_F$. As for $\rho$, well, to find its derivative is way too complicated! So intuitively, we can see that when $\rho$ increases, the agents will intend to spend more in the period of time $1$. Therefore they will have more money in their account and thus gain more in security market, leading to a higher $g$.

## Question 3.1

$\P{1}$

$\bspace$There're only $2$ states in time period $1$ thus there're only two possible securities in the Arrow-Debreu Market. And their payoff vectors are $\SB{1;0}$ and $\SB{0;1}$ respectively.

$\P{2}$

$\bspace$Obviously we have $e = \begin{bmatrix}
2 \\
1
\end{bmatrix} = 2\begin{bmatrix}
1 \\
0
\end{bmatrix} + \begin{bmatrix}
0 \\
1
\end{bmatrix} = 2X_a + X_b$

$\P{3}$

$\bspace$His financial wealth is $w = 0 + [\phi_a,\phi_b]\begin{bmatrix}
2 \\
1
\end{bmatrix} = 2\phi_a + \phi_b$. His budget plan is $B\P{\begin{bmatrix}
0 \\
2 \\
1
\end{bmatrix}, \CB{\begin{bmatrix}
1 & 0 \\
0 & 1
\end{bmatrix},S}}$, which is $\CB{c \geq 0 : \Tran{\hat\phi}c = [1,\phi_a,\phi_b]\begin{bmatrix}
c_0 \\
c_{1a} \\
c_{1b}
\end{bmatrix} =c_0 + \phi_a c_{1a} + \phi_bc_{1b} = w }$

$\P{4}$

$\bspace$His optimization problem can be written as

$$\begin{array}{cl}
\d{\max_{c\in C}} & U\P{c}=-e^{-c_{\void_0}}-\ffrac{1}{2}\P{e^{-c_{\void_{1a}}}+e^{-c_{\void_{1b}}}} \\
s.t. & 2\phi_a + \phi_b = w = c_0 + \phi_a c_{1a} + \phi_bc_{1b}
\end{array}$$

$\bspace$Using the lagrangian multiplier method we have

$$\begin{array}{l}
\partial_iU = \lambda\phi_i + \mu_i \Rightarrow \begin{cases}
e^{-c_{\void_0}} = \lambda \times 1 + \mu_0 \\
0.5 e^{-c_{\void_{1a}}} = \lambda \phi_{a} + \mu_a \\
0.5 e^{-c_{\void_{1b}}} = \lambda \phi_{b} + \mu_b
\end{cases} \\
\Tran{\hat\phi}\P{e-c} = \P{2-c_{1a}}\phi_a + \P{1-c_{1b}}\phi_b - c_0 = 0 \\
\mu_i c_i = 0
\end{array}$$

$\bspace$Notice that $\text DU \gg 0$, we can assume that $\mu_i = 0$. Thus, we can plug in $\begin{cases}
c_0 = -\log \lambda \\
c_{1a} = -\log\P{2\phi_a \lambda} \\
c_{1b} = -\log\P{2\phi_b \lambda}
\end{cases}$

$$\big(2 + \log\P{2\phi_a\lambda}\big)\phi_a + \big(1 + \log\P{x\phi_b\lambda}\big)\phi_b + \log\lambda = 0 \\
\log \lambda = -\ffrac{ 2\phi_a + \phi_b + \phi_a\P{\log 2\phi_a} + \phi_b\P{\log 2\phi_b} } {1 + \phi_a + \phi_b}$$

$\bspace$Thus we can find all optimized solution for $c$:

$$c_0 = \ffrac{ 2\phi_a + \phi_b + \phi_a\P{\log 2\phi_a} + \phi_b\P{\log 2\phi_b} } {1 + \phi_a + \phi_b} \\
c_{1a} =\ffrac{ 2\phi_a + \phi_b + \phi_b\P{\log 2\phi_b} - \phi_b\P{\log 2\phi_a} - \log\P{2\phi_a} } {1 + \phi_a + \phi_b} \\
c_{1b} =\ffrac{ 2\phi_a + \phi_b + \phi_a\P{\log 2\phi_a} - \phi_a\P{\log 2\phi_b} - \log\P{2\phi_b} } {1 + \phi_a + \phi_b}
$$

$\P{5}$

$\bspace$We can draw the $c-\phi$ graph to see how $\phi$ gonna affect the consumption (it's too complicated to find their derivatives). And we can find that

- when $\phi_a$ increase from about $0$
    - $c_0$ first decrease a little bit then increase
    - $c_{1a}$ first decrease then increase
    - $c_{1b}$ keep increasing
- when $\phi_b$ increase from about $0$
    - $c_0$ first decrease a little bit then increase
    - $c_{1a}$ first decrease a little bit then increase
    - $c_{1b}$ first decrease then keep increasing, but just a little bit

$\P{6}$

$\bspace$Suppoes $\phi_a = \phi_b = x$, then $c_0 = \ffrac{3x+2x\log\P{2x}} {1+2x} < 0 \Leftrightarrow \log 2x < -1.5$. Thus we can assign the value $\ffrac{e^{-2}} {2} = x$ and then $c_0 =-\ffrac{1} {2\P{e^{2} + 1}}$

## Question 3.2

$\P{1}$

$\bspace$This is the same with **Question 2.7.2** where we find the answer should be $\theta = s = \ffrac{100\rho \P{1+r_F} - 1} {\P{1+\rho}\P{1+r_F}}$ and therefore

$$c_0 = \ffrac{100\P{1+r_F} + 1} {\P{1+\rho}\P{1+r_F}},
c_1 = \rho\ffrac{100\P{1+r_F}+1}{1+\rho}$$

$\P{2}$

$\bspace$To reach the market clearing, it requires

$$\begin{cases}
100 - \theta = 100 \\
1 + \theta\P{1+r_F} = 1
\end{cases} \Rightarrow \theta = 0$$

$\P{3}$

$\bspace$After $\theta = 0$, we have $0 = \ffrac{100\rho \P{1+r_F} - 1} {\P{1+\rho}\P{1+r_F}} \Rightarrow r_F = \ffrac{1} {100\rho} - 1$

$\P{4}$

$\bspace$The equilibrium interest rate $r_F$ decreases with the increase of $\rho$. It happens because when $\rho$ increases, agents will prefer spend more in the future thus there'll be more savings which leads to a lower $r_F$.

## Question 3.3

$\P{1}$

$$\begin{array}{cl}
\d{\max_{c_{\void_2} \in C}} & U_2 \P{c_2} = \log c_{2,0} + \rho\log c_{2,1} \\
s.t. & c_{2,0} + \phi_a c_{2,1} = 1 + 100\phi_a \\
& \phi_a = \P{1 + r_F}^{-1} \\
& c_{2,0}, c_{2,1} \geq 0
\end{array}$$

solve this we'll obtain $c_{2,0}=\ffrac{1+r_F+100} {\P{1+\rho}\P{1+r_F}}, c_{2,1} = \ffrac{\rho\P{1+r_F+100}} {1+\rho}, s = 1 - c_{2,0} = \ffrac{\rho\P{1+r_F}-100} {\P{1+\rho}\P{1+r_F}}$

$\P{2}$

$\bspace$They are identical in terms of their wealth. $c_0 = \ffrac{w} {1+\rho}, c_1 = \ffrac{w\rho\P{1 + r_F}} {1+\rho}, s = \ffrac{e_0\rho - e_1/\P{1+r_F}} {1+\rho}$.

$\P{3}$

$\bspace$The market equilibrium now is

$$\begin{cases}
\ffrac{101+\ffrac{101} {1+r_F}} {1+\rho} = 101 \\
\ffrac{\rho\P{1+r_F}\P{101 + \ffrac{101} {1+r_F}}} {1+\rho} = 101
\end{cases} \Longrightarrow \rho\P{1+r_F} = 1$$

$\P{4}$

$\bspace$From the last question we can see that $r_F = \ffrac{1} {\rho} - 1$. They are different because of the different $\sum\limits_k e_{0,k}$ and $\sum\limits_k e_{1,k}$

$\P{5}$

$\bspace$We only need to check whether they reach the equilibrium.

$$\ffrac{\partial_1 U_1\P{c_{1}}} {\partial_0 U_1\P{c_{1}}} = \ffrac{\ffrac{\rho} {c_{1,1}}} {\ffrac{1} {c_{1,0}}} = \ffrac{1} {1+r_F} \\
\ffrac{\partial_1 U_2\P{c_{2}}} {\partial_0 U_2\P{c_{2}}} = \ffrac{\ffrac{\rho} {c_{2,1}}} {\ffrac{1} {c_{2,0}}} = \ffrac{1} {1+r_F} = \ffrac{\partial_1 U_1\P{c_{1}}} {\partial_0 U_1\P{c_{1}}}
$$

$\bspace$Thus they reached the equilibrium and thus by $Theorem.4$, this allocation is parato dominant.

## Question 3.4

$\P{1}$

$$\begin{array}{cl}
\d{\max_{c_{\void_{i}}\in C}} & U\P{c_i} = -\ffrac{1} {c_{i,0}} -\ffrac{1} {3}\P{\ffrac{1} {c_{i,1a}} + \ffrac{1} {c_{i,1b}} + \ffrac{1} {c_{i,1c}}}, i = 1,2 \\
s.t. & c_{1,0} + \Tran{\phi}_1\begin{bmatrix}
c_{1,1a}\\
c_{1,1b}\\
c_{1,1c}
\end{bmatrix} = 100, c_{2,0} + \Tran{\phi}_2\begin{bmatrix}
c_{2,1a}\\
c_{2,1b}\\
c_{2,1c}
\end{bmatrix} = 0 + \Tran{\phi}_2\begin{bmatrix}
200\\
100\\
50
\end{bmatrix}\\
& c_{i,\cdot} \geq 0
\end{array}$$

$\bspace$This is a easy one, the solutions are:

$$c_{1,0} = \ffrac{100} {1+\d{\sum_{a,b,c}}\P{\sqrt{\ffrac{\phi_{1,a}} {3}}}}, c_{1,\,j} = \ffrac{100} {\sqrt{3\phi_{1,\,j}}\SB{1+\d{\sum_{a,b,c}}\P{\sqrt{\ffrac{\phi_{1,a}} {3}}}}},j = 1a,1b,1c \\
c_{2,0} = \ffrac{200\phi_{2,1a} + 100\phi_{2,1b} + 50\phi_{2,1c}} {1+\d{\sum_{a,b,c}}\P{\sqrt{\ffrac{\phi_{2,a}} {3}}}}, c_{2,\,j} = \ffrac{200\phi_{2,1a} + 100\phi_{2,1b} + 50\phi_{2,1c}} {\sqrt{3\phi_{2,\,j}}\SB{1+\d{\sum_{a,b,c}}\P{\sqrt{\ffrac{\phi_{2,a}} {3}}}}},j = 1a,1b,1c
$$

$\P{2}$

$\bspace$We have the equation set: $\ffrac{100} {\sqrt{3\phi_{1,\,j}}\SB{1+\d{\sum_{a,b,c}}\P{\sqrt{\ffrac{\phi_{1,a}} {3}}}}} + \ffrac{200\phi_{2,1a} + 100\phi_{2,1b} + 50\phi_{2,1c}} {\sqrt{3\phi_{2,\,j}}\SB{1+\d{\sum_{a,b,c}}\P{\sqrt{\ffrac{\phi_{2,a}} {3}}}}} = e_{2,\,j},j = 1a,1b,1c$

$\bspace$Solve the equation set we have the state price $\phi = \SB{\ffrac{1} {12}; \ffrac{1} {3};\ffrac{4} {3}}$

$\P{3}$

$\bspace$The result is obvious since the endowment is in the position of numerator of the expression for their consumption, like the "$100$" and "$[200;100;50]$". When they increase, the numerator $100$ and $\Tran\phi [200;100;50]$ increase.

$\P{4}$

$\bspace c_{k,1a}:c_{k,1b}:c_{k,1c} = \sqrt{\ffrac{1} {\phi_a}} : \sqrt{\ffrac{1} {\phi_b}} : \sqrt{\ffrac{1} {\phi_c}} = 4:2:1,k = 1,2$, that's it.


# Gravity and Central Forces

**[Morten Hjorth-Jensen](http://mhjgit.github.io/info/doc/web/)**, Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University, USA and Department of Physics, University of Oslo, Norway

 **[Scott Pratt](https://pa.msu.edu/profile/pratts/)**, Department of Physics and Astronomy and National Superconducting Cyclotron Laboratory, Michigan State University, USA

 **[Carl Schmidt](https://pa.msu.edu/profile/schmidt/)**, Department of Physics and Astronomy, Michigan State University, USA

Date: **Mar 18, 2020**

Copyright 1999-2020, [Morten Hjorth-Jensen](http://mhjgit.github.io/info/doc/web/). Released under CC Attribution-NonCommercial 4.0 license

## Introduction

The gravitational potential energy and forces involving two masses $a$ and $b$ are

$$
\begin{eqnarray}
U_{ab}&=&-\frac{Gm_am_b}{|\boldsymbol{r}_a-\boldsymbol{r}_b|},\\
\nonumber
F_{ba}&=&-\frac{Gm_am_b}{|\boldsymbol{r}_a-\boldsymbol{r}_b|^2}\hat{r}_{ab},\\
\nonumber
\hat{r}_{ab}&=&\frac{\boldsymbol{r}_b-\boldsymbol{r}_a}{|\boldsymbol{r}_a-\boldsymbol{r}_b|}.
\end{eqnarray}
$$

Here $G=6.67\times 10^{-11}$ Nm$^2$/kg$^2$, and $F_{ba}$ is the force
on $b$ due to $a$. By inspection, one can see that the force on $b$
due to $a$ and the force on $a$ due to $b$ are equal and opposite. The
net potential energy for a large number of masses would be

$$
\begin{equation}
U=\sum_{a<b}U_{ab}=\frac{1}{2}\sum_{a\ne b}U_{ab}.
\label{_auto1} \tag{1}
\end{equation}
$$

## Relative and Center of Mass Motion

## Deriving Elliptical Orbits

Kepler's laws state that a gravitational orbit should be an ellipse
with the source of the gravitational field at one focus. Deriving this
is surprisingly messy. To do this, we first use angular momentum
conservation to transform the equations of motion so that it is in
terms of $r$ and $\theta$ instead of $r$ and $t$. The overall strategy
is to

1. Find equations of motion for $r$ and $t$ with no angle ($\theta$) mentioned, i.e. $d^2r/dt^2=\cdots$. Angular momentum conservation will be used, and the equation will involve the angular momentum $L$.

2. Use angular momentum conservation to find an expression for $\dot{\theta}$ in terms of $r$.

3. Use the chain rule to convert the equations of motions for $r$, an expression involving $r,\dot{r}$ and $\ddot{r}$, to one involving $r,dr/d\theta$ and $d^2r/d\theta^2$. This is quitecomplicated because the expressions will also involve a substitution $u=1/r$ so that one finds an expression in terms of $u$ and $\theta$.

4. Once $u(\theta)$ is found, you need to show that this can be converted to the familiar form for an ellipse.

The equations of motion give

$$
\begin{eqnarray}
\label{eq:radialeqofmotion} \tag{2}
\frac{d}{dt}r^2&=&\frac{d}{dt}(x^2+y^2)=2x\dot{x}+2y\dot{y}=2r\dot{r},\\
\nonumber
\dot{r}&=&\frac{x}{r}\dot{x}+\frac{y}{r}\dot{y},\\
\nonumber
\ddot{r}&=&\frac{x}{r}\ddot{x}+\frac{y}{r}\ddot{y}
+\frac{\dot{x}^2+\dot{y}^2}{r}
-\frac{\dot{r}^2}{r}.
\end{eqnarray}
$$

Recognizing that the numerator of the third term is the velocity squared, and that it can be written in polar coordinates,

$$
\begin{equation}
v^2=\dot{x}^2+\dot{y}^2=\dot{r}^2+r^2\dot{\theta}^2,
\label{_auto2} \tag{3}
\end{equation}
$$

one can write $\ddot{r}$ as

$$
\begin{eqnarray}
\label{eq:radialeqofmotion2} \tag{4}
\ddot{r}&=&\frac{F_x\cos\theta+F_y\sin\theta}{m}+\frac{\dot{r}^2+r^2\dot{\theta}^2}{r}-\frac{\dot{r}^2}{r}\\
\nonumber
&=&\frac{F}{m}+\frac{r^2\dot{\theta}^2}{r}\\
\nonumber
m\ddot{r}&=&F+\frac{L^2}{mr^3}.
\end{eqnarray}
$$

This derivation used the fact that the force was radial,
$F=F_r=F_x\cos\theta+F_y\sin\theta$, and that angular momentum is
$L=mrv_{\theta}=mr^2\dot{\theta}$. The term $L^2/mr^3=mv^2/r$ behaves
like an additional force. Sometimes this is referred to as a
centrifugal force, but it is not a force. Instead, it is the
consequence of considering the motion in a rotating (and therefore
accelerating) frame.

Now, we switch to the particular case of an attractive inverse square
force, $F=-\alpha/r^2$, and show that the trajectory, $r(\theta)$, is
an ellipse. To do this we transform derivatives w.r.t. time to
derivatives w.r.t. $\theta$ using the chain rule combined with angular
momentum conservation, $\dot{\theta}=L/mr^2$.

$$
\begin{eqnarray}
\label{eq:rtotheta} \tag{5}
\dot{r}&=&\frac{dr}{d\theta}\dot{\theta}=\frac{dr}{d\theta}\frac{L}{mr^2},\\
\nonumber
\ddot{r}&=&\frac{d^2r}{d\theta^2}\dot{\theta}^2
+\frac{dr}{d\theta}\left(\frac{d}{dr}\frac{L}{mr^2}\right)\dot{r}\\
\nonumber
&=&\frac{d^2r}{d\theta^2}\left(\frac{L}{mr^2}\right)^2
-2\frac{dr}{d\theta}\frac{L}{mr^3}\dot{r}\\
\nonumber
&=&\frac{d^2r}{d\theta^2}\left(\frac{L}{mr^2}\right)^2
-\frac{2}{r}\left(\frac{dr}{d\theta}\right)^2\left(\frac{L}{mr^2}\right)^2
\end{eqnarray}
$$

Equating the two expressions for $\ddot{r}$ in Eq.s ([4](#eq:radialeqofmotion2)) and ([5](#eq:rtotheta)) eliminates all the derivatives w.r.t. time, and provides a differential equation with only derivatives w.r.t. $\theta$,

$$
\begin{equation}
\label{eq:rdotdot} \tag{6}
\frac{d^2r}{d\theta^2}\left(\frac{L}{mr^2}\right)^2
-\frac{2}{r}\left(\frac{dr}{d\theta}\right)^2\left(\frac{L}{mr^2}\right)^2
=\frac{F}{m}+\frac{L^2}{m^2r^3},
\end{equation}
$$

that when solved yields the trajectory, i.e. $r(\theta)$. Up to this
point the expressions work for any radial force, not just forces that
fall as $1/r^2$.

The trick to simplifying this differential equation for the inverse
square problems is to make a substitution, $u\equiv 1/r$, and rewrite
the differential equation for $u(\theta)$.

$$
\begin{eqnarray}
r&=&1/u,\\
\nonumber
\frac{dr}{d\theta}&=&-\frac{1}{u^2}\frac{du}{d\theta},\\
\nonumber
\frac{d^2r}{d\theta^2}&=&\frac{2}{u^3}\left(\frac{du}{d\theta}\right)^2-\frac{1}{u^2}\frac{d^2u}{d\theta^2}.
\end{eqnarray}
$$

Plugging these expressions into Eq. ([6](#eq:rdotdot)) gives an
expression in terms of $u$, $du/d\theta$, and $d^2u/d\theta^2$. After
some tedious algebra,

$$
\begin{equation}
\frac{d^2u}{d\theta^2}=-u-\frac{F m}{L^2u^2}.
\label{_auto3} \tag{7}
\end{equation}
$$

For the attractive inverse square law force, $F=-\alpha u^2$,

$$
\begin{equation}
\frac{d^2u}{d\theta^2}=-u+\frac{m\alpha}{L^2}.
\label{_auto4} \tag{8}
\end{equation}
$$

The solution has two arbitrary constants, $A$ and $\theta_0$,

$$
\begin{eqnarray}
\label{eq:Ctrajectory} \tag{9}
u&=&\frac{m\alpha}{L^2}+A\cos(\theta-\theta_0),\\
\nonumber
r&=&\frac{1}{(m\alpha/L^2)+A\cos(\theta-\theta_0)}.
\end{eqnarray}
$$

The radius will be at a minimum when $\theta=\theta_0$ and at a
maximum when $\theta=\theta_0+\pi$. The constant $A$ is related to the
eccentricity of the orbit. When $A=0$ the radius is a constant
$r=L^2/(m\alpha)$, and the motion is circular. If one solved the
expression $mv^2/r=-\alpha/r^2$ for a circular orbit, using the
substitution $v=L/(mr)$, one would reproduce the expression
$r=L^2/(m\alpha)$.

The form describing the elliptical trajectory in
Eq. ([9](#eq:Ctrajectory)) can be identified as an ellipse with one
focus being the center of the ellipse by considering the definition of
an ellipse as being the points such that the sum of the two distances
between the two foci are a constant. Making that distance $2D$, the
distance between the two foci as $2a$, and putting one focus at the
origin,

$$
\begin{eqnarray}
2D&=&r+\sqrt{(r\cos\theta-2a)^2+r^2\sin^2\theta},\\
\nonumber
4D^2+r^2-4Dr&=&r^2+4a^2-4ar\cos\theta,\\
\nonumber
r&=&\frac{D^2-a^2}{D+a\cos\theta}=\frac{1}{D/(D^2-a^2)-a\cos\theta/(D^2-a^2)}.
\end{eqnarray}
$$

By inspection, this is the same form as Eq. ([9](#eq:Ctrajectory)) with $D/(D^2-a^2)=m\alpha/L^2$ and $a/(D^2-a^2)=A$.

## Effective or Centrifugal Potential

The total energy of a particle is

$$
\begin{eqnarray}
E&=&U(r)+\frac{1}{2}mv_\theta^2+\frac{1}{2}m\dot{r}^2\\
\nonumber
&=&U(r)+\frac{1}{2}mr^2\dot{\theta}^2+\frac{1}{2}m\dot{r}^2\\
\nonumber
&=&U(r)+\frac{L^2}{2mr^2}+\frac{1}{2}m\dot{r}^2.
\end{eqnarray}
$$

The second term then contributes to the energy like an additional
repulsive potential. The term is sometimes referred to as the
"centrifugal" potential, even though it is actually the kinetic energy
of the angular motion. Combined with $U(r)$, it is sometimes referred
to as the "effective" potential,

$$
\begin{eqnarray}
U_{\rm eff}(r)&=&U(r)+\frac{L^2}{2mr^2}.
\end{eqnarray}
$$

Note that if one treats the effective potential like a real potential, one would expect to be able to generate an effective force,

$$
\begin{eqnarray}
F_{\rm eff}&=&-\frac{d}{dr}U(r) -\frac{d}{dr}\frac{L^2}{2mr^2}\\
\nonumber
&=&F(r)+\frac{L^2}{mr^3}=F(r)+m\frac{v_\perp^2}{r},
\end{eqnarray}
$$

which is indeed matches the form for $m\ddot{r}$ in Eq. ([4](#eq:radialeqofmotion2)), which included the **centrifugal** force.

The following code plots this effective potential for a simple choice of parameters, with a standard gravitational potential $-\alpha/r$. Here we have chosen $L=m=\alpha=1$.

```python
%matplotlib inline

# Common imports
import numpy as np
from math import *
import matplotlib.pyplot as plt

Deltax = 0.01
#set up arrays
xinitial = 0.3
xfinal = 5.0
alpha = 1.0   # spring constant
m = 1.0   # mass, you can change these
AngMom = 1.0  #  The angular momentum
n = ceil((xfinal-xinitial)/Deltax)
x = np.zeros(n)
for i in range(n):
    x[i] = xinitial+i*Deltax
V = np.zeros(n)
V = -alpha/x+0.5*AngMom*AngMom/(m*x*x)
# Plot potential
fig, ax = plt.subplots()
ax.set_xlabel('r[m]')
ax.set_ylabel('V[J]')
ax.plot(x, V)
fig.tight_layout()
plt.show()
```

### Gravitational force example

Using the above parameters, we can now study the evolution of the system using for example the velocity Verlet method.
This is done in the code here for an initial radius equal to the minimum of the potential well.  We seen then that the radius is always the same and corresponds to a circle (the radius is always constant).

```python
# Common imports
import numpy as np
import pandas as pd
from math import *
import matplotlib.pyplot as plt
import os

# Where to save the figures and data files
PROJECT_ROOT_DIR = "Results"
FIGURE_ID = "Results/FigureFiles"
DATA_ID = "DataFiles/"

if not os.path.exists(PROJECT_ROOT_DIR):
    os.mkdir(PROJECT_ROOT_DIR)

if not os.path.exists(FIGURE_ID):
    os.makedirs(FIGURE_ID)

if not os.path.exists(DATA_ID):
    os.makedirs(DATA_ID)

def image_path(fig_id):
    return os.path.join(FIGURE_ID, fig_id)

def data_path(dat_id):
    return os.path.join(DATA_ID, dat_id)

def save_fig(fig_id):
    plt.savefig(image_path(fig_id) + ".png", format='png')

# Simple Gravitational Force   -alpha/r

DeltaT = 0.01
#set up arrays
tfinal = 100.0
n = ceil(tfinal/DeltaT)
# set up arrays for t, v and r
t = np.zeros(n)
v = np.zeros(n)
r = np.zeros(n)
# Constants of the model, setting all variables to one for simplicity
alpha = 1.0
AngMom = 1.0  #  The angular momentum
m = 1.0  # scale mass to one
c1 = AngMom*AngMom/(m*m)
c2 = AngMom*AngMom/m
rmin = (AngMom*AngMom/m/alpha)
# Initial conditions
r0 = rmin
v0 = 0.0
r[0] = r0
v[0] = v0
# Start integrating using the Velocity-Verlet  method
for i in range(n-1):
    # Set up acceleration
    a = -alpha/(r[i]**2)+c1/(r[i]**3)
    # update velocity, time and position using the Velocity-Verlet method
    r[i+1] = r[i] + DeltaT*v[i]+0.5*(DeltaT**2)*a
    anew = -alpha/(r[i+1]**2)+c1/(r[i+1]**3)
    v[i+1] = v[i] + 0.5*DeltaT*(a+anew)
    t[i+1] = t[i] + DeltaT
    # Plot position as function of time
fig, ax = plt.subplots(2,1)
ax[0].set_xlabel('time')
ax[0].set_ylabel('radius')
ax[0].plot(t,r)
ax[1].set_xlabel('time')
ax[1].set_ylabel('Velocity')
ax[1].plot(t,v)
save_fig("RadialGVV")
plt.show()
```

Changing the value of the initial position to a value where the energy is positive, leads to an increasing radius with time, a so-called unbound orbit. Choosing on the other hand an initial radius that corresponds to a negative energy and different from the minimum value leads to a radius that oscillates back and forth between two values.

### Harmonic Oscillator in two dimensions

Consider a particle of mass $m$ in a 2-dimensional harmonic oscillator with potential

$$
U=\frac{1}{2}kr^2=\frac{1}{2}k(x^2+y^2).
$$

If the orbit has angular momentum $L$, we can find the radius and angular velocity of the circular orbit as well as the b) the angular frequency of small radial perturbations.

We consider the effective potential. The radius of a circular orbit is at the minimum of the potential (where the effective force is zero).
The potential is plotted here with the parameters $k=m=0.1$ and $L=1.0$.

```python
# Common imports
import numpy as np
from math import *
import matplotlib.pyplot as plt

Deltax = 0.01
#set up arrays
xinitial = 1.0
xfinal = 5.0
k = 0.1   # spring constant
m = 0.1   # mass, you can change these
AngMom = 1.0  #  The angular momentum
n = ceil((xfinal-xinitial)/Deltax)
x = np.zeros(n)
for i in range(n):
    x[i] = xinitial+i*Deltax
V = np.zeros(n)
V = 0.5*k*x*x+0.5*AngMom*AngMom/(m*x*x)
# Plot potential
fig, ax = plt.subplots()
ax.set_xlabel('r[m]')
ax.set_ylabel('V[J]')
ax.plot(x, V)
fig.tight_layout()
plt.show()
```

$$
\begin{eqnarray*}
U_{\rm eff}&=&\frac{1}{2}kr^2+\frac{L^2}{2mr^2}
\end{eqnarray*}
$$

The effective potential looks like that of a harmonic oscillator for
large $r$, but for small $r$, the centrifugal potential repels the
particle from the origin. The combination of the two potentials has a
minimum for at some radius $r_{\rm min}$.

$$
\begin{eqnarray*}
0&=&kr_{\rm min}-\frac{L^2}{mr_{\rm min}^3},\\
r_{\rm min}&=&\left(\frac{L^2}{mk}\right)^{1/4},\\
\dot{\theta}&=&\frac{L}{mr_{\rm min}^2}=\sqrt{k/m}.
\end{eqnarray*}
$$

For particles at $r_{\rm min}$ with $\dot{r}=0$, the particle does not
accelerate and $r$ stays constant, i.e. a circular orbit. The radius
of the circular orbit can be adjusted by changing the angular momentum
$L$.

For the above parameters this minimum is at $r_{\rm min}=1$.

 Now consider small vibrations about $r_{\rm min}$. The effective spring constant is the curvature of the effective potential.

$$
\begin{eqnarray*}
k_{\rm eff}&=&\left.\frac{d^2}{dr^2}U_{\rm eff}(r)\right|_{r=r_{\rm min}}=k+\frac{3L^2}{mr_{\rm min}^4}\\
&=&4k,\\
\omega&=&\sqrt{k_{\rm eff}/m}=2\sqrt{k/m}=2\dot{\theta}.
\end{eqnarray*}
$$

Here, the second step used the result of the last step from part
(a). Because the radius oscillates with twice the angular frequency,
the orbit has two places where $r$ reaches a minimum in one
cycle. This differs from the inverse-square force where there is one
minimum in an orbit. One can show that the orbit for the harmonic
oscillator is also elliptical, but in this case the center of the
potential is at the center of the ellipse, not at one of the foci.

The solution is also simple to write down exactly in Cartesian coordinates. The $x$ and $y$ equations of motion separate,

$$
\begin{eqnarray*}
\ddot{x}&=&-kx,\\
\ddot{y}&=&-ky.
\end{eqnarray*}
$$

So the general solution can be expressed as

$$
\begin{eqnarray*}
x&=&A\cos\omega_0 t+B\sin\omega_0 t,\\
y&=&C\cos\omega_0 t+D\sin\omega_0 t.
\end{eqnarray*}
$$

The code here finds the solution for $x$ and $y$ using the code we developed in homework 4.

```python

DeltaT = 0.01
#set up arrays
tfinal = 10.0
n = ceil(tfinal/DeltaT)
# set up arrays
t = np.zeros(n)
v = np.zeros((n,2))
r = np.zeros((n,2))
radius = np.zeros(n)
# Constants of the model
k = 0.1   # spring constant
m = 0.1   # mass, you can change these
omega02 = sqrt(k/m)  # Frequency
AngMom = 1.0  #  The angular momentum
rmin = (AngMom*AngMom/k/m)**0.25
# Initial conditions as compact 2-dimensional arrays
#x0 =rmin*0.5; y0 = sqrt(rmin*rmin-x0*x0)
x0 = 1.0; y0= 1.0
r0 = np.array([x0,y0])
v0 = np.array([0.0,0.0])
r[0] = r0
v[0] = v0
# Start integrating using the Velocity-Verlet  method
for i in range(n-1):
    # Set up the acceleration
    a =  -r[i]*omega02
    # update velocity, time and position using the Velocity-Verlet method
    r[i+1] = r[i] + DeltaT*v[i]+0.5*(DeltaT**2)*a
    anew = -r[i+1]*omega02
    v[i+1] = v[i] + 0.5*DeltaT*(a+anew)
    t[i+1] = t[i] + DeltaT
# Plot position as function of time
radius = np.sqrt(r[:,0]**2+r[:,1]**2)
fig, ax = plt.subplots(3,1)
ax[0].set_xlabel('time')
ax[0].set_ylabel('radius squared')
ax[0].plot(t,r[:,0]**2+r[:,1]**2)
ax[1].set_xlabel('time')
ax[1].set_ylabel('x position')
ax[1].plot(t,r[:,0])
ax[2].set_xlabel('time')
ax[2].set_ylabel('y position')
ax[2].plot(t,r[:,1])

fig.tight_layout()
save_fig("2DimHOVV")
plt.show()
```

With some work using double angle formulas, one can calculate

$$
\begin{eqnarray*}
r^2&=&x^2+y^2\\
\nonumber
&=&(A^2+C^2)\cos^2(\omega_0t)+(B^2+D^2)\sin^2\omega_0t+(AB+CD)\cos(\omega_0t)\sin(\omega_0t)\\
\nonumber
&=&\alpha+\beta\cos 2\omega_0 t+\gamma\sin 2\omega_0 t,\\
\alpha&=&\frac{A^2+B^2+C^2+D^2}{2},~~\beta=\frac{A^2-B^2+C^2-D^2}{2},~~\gamma=AB+CD,\\
r^2&=&\alpha+(\beta^2+\gamma^2)^{1/2}\cos(2\omega_0 t-\delta),~~~\delta=\arctan(\gamma/\beta),
\end{eqnarray*}
$$

and see that radius oscillates with frequency $2\omega_0$. The
factor of two comes because the oscillation $x=A\cos\omega_0t$ has two
maxima for $x^2$, one at $t=0$ and one a half period later.

The following code shows first how we can solve this problem using the radial degrees of freedom only.

```python
DeltaT = 0.01
#set up arrays
tfinal = 10.0
n = ceil(tfinal/DeltaT)
# set up arrays for t, v and r
t = np.zeros(n)
v = np.zeros(n)
r = np.zeros(n)
E = np.zeros(n)
# Constants of the model
AngMom = 1.0  #  The angular momentum
m = 0.1
k = 0.1
omega02 = k/m
c1 = AngMom*AngMom/(m*m)
c2 = AngMom*AngMom/m
rmin = (AngMom*AngMom/k/m)**0.25
# Initial conditions
r0 = rmin
v0 = 0.0
r[0] = r0
v[0] = v0
E[0] = 0.5*m*v0*v0+0.5*k*r0*r0+0.5*c2/(r0*r0)
# Start integrating using the Velocity-Verlet  method
for i in range(n-1):
    # Set up acceleration
    a = -r[i]*omega02+c1/(r[i]**3)
    # update velocity, time and position using the Velocity-Verlet method
    r[i+1] = r[i] + DeltaT*v[i]+0.5*(DeltaT**2)*a
    anew = -r[i+1]*omega02+c1/(r[i+1]**3)
    v[i+1] = v[i] + 0.5*DeltaT*(a+anew)
    t[i+1] = t[i] + DeltaT
    E[i+1] = 0.5*m*v[i+1]*v[i+1]+0.5*k*r[i+1]*r[i+1]+0.5*c2/(r[i+1]*r[i+1])
    # Plot position as function of time
fig, ax = plt.subplots(2,1)
ax[0].set_xlabel('time')
ax[0].set_ylabel('radius')
ax[0].plot(t,r)
ax[1].set_xlabel('time')
ax[1].set_ylabel('Energy')
ax[1].plot(t,E)
save_fig("RadialHOVV")
plt.show()
```

## Stability of Orbits

The effective force can be extracted from the effective potential, $U_{\rm eff}$. Beginning from the equations of motion, Eq. ([2](#eq:radialeqofmotion)), for $r$,

$$
\begin{eqnarray}
m\ddot{r}&=&F+\frac{L^2}{mr^3}\\
\nonumber
&=&F_{\rm eff}\\
\nonumber
&=&-\partial_rU_{\rm eff},\\
\nonumber
F_{\rm eff}&=&-\partial_r\left[U(r)+(L^2/2mr^2)\right].
\end{eqnarray}
$$

For a circular orbit, the radius must be fixed as a function of time,
so one must be at a maximum or a minimum of the effective
potential. However, if one is at a maximum of the effective potential
the radius will be unstable. For the attractive Coulomb force the
effective potential will be dominated by the $-\alpha/r$ term for
large $r$ because the centrifugal part falls off more quickly, $\sim
1/r^2$. At low $r$ the centrifugal piece wins and the effective
potential is repulsive. Thus, the potential must have a minimum
somewhere with negative potential. The circular orbits are then stable
to perturbation.

The effective potential is sketched for two cases, a $1/r$ attractive
potential and a $1/r^3$ attractive potential. The $1/r$ case has a
stable minimum, whereas the circular orbit in the $1/r^3$ case is
unstable.

If one considers a potential that falls as $1/r^3$, the situation is
reversed and the point where $\partial_rU$ disappears will be a local
maximum rather than a local minimum. **Fig to come here with code**

The repulsive centrifugal piece dominates at large $r$ and the attractive
Coulomb piece wins out at small $r$. The circular orbit is then at a
maximum of the effective potential and the orbits are unstable. It is
the clear that for potentials that fall as $r^n$, that one must have
$n>-2$ for the orbits to be stable.

Consider a potential $U(r)=\beta r$. For a particle of mass $m$ with
angular momentum $L$, find the angular frequency of a circular
orbit. Then find the angular frequency for small radial perturbations.

For the circular orbit you search for the position $r_{\rm min}$ where the effective potential is minimized,

$$
\begin{eqnarray*}
\partial_r\left\{\beta r+\frac{L^2}{2mr^2}\right\}&=&0,\\
\beta&=&\frac{L^2}{mr_{\rm min}^3},\\
r_{\rm min}&=&\left(\frac{L^2}{\beta m}\right)^{1/3},\\
\dot{\theta}&=&\frac{L}{mr_{\rm min}^2}=\frac{\beta^{2/3}}{(mL)^{1/3}}
\end{eqnarray*}
$$

Now, we can find the angular frequency of small perturbations about the circular orbit. To do this we find the effective spring constant for the effective potential,

$$
\begin{eqnarray*}
k_{\rm eff}&=&\partial_r^2 \left.U_{\rm eff}\right|_{r_{\rm min}}\\
&=&\frac{3L^2}{mr_{\rm min}^4},\\
\omega&=&\sqrt{\frac{k_{\rm eff}}{m}}\\
&=&\frac{\beta^{2/3}}{(mL)^{1/3}}\sqrt{3}.
\end{eqnarray*}
$$

If the two frequencies, $\dot{\theta}$ and $\omega$, differ by an
integer factor, the orbit's trajectory will repeat itself each time
around. This is the case for the inverse-square force,
$\omega=\dot{\theta}$, and for the harmonic oscillator,
$\omega=2\dot{\theta}$. In this case, $\omega=\sqrt{3}\dot{\theta}$,
and the angles at which the maxima and minima occur change with each
orbit.

## Scattering and Cross Sections

Scattering experiments don't measure entire trajectories. For elastic
collisions, they measure the distribution of final scattering angles
at best. Most experiments use targets thin enough so that the number
of scatterings is typically zero or one. The cross section, $\sigma$,
describes the cross-sectional area for particles to scatter with an
individual target atom or nucleus. Cross section measurements form the
basis for MANY fields of physics. BThe cross section, and the
differential cross section, encapsulates everything measurable for a
collision where all that is measured is the final state, e.g. the
outgoing particle had momentum $\boldsymbol{p}_f$. y studying cross sections,
one can infer information about the potential interaction between the
two particles. Inferring, or constraining, the potential from the
cross section is a classic {\it inverse} problem. Collisions are
either elastic or inelastic. Elastic collisions are those for which
the two bodies are in the same internal state before and after the
collision. If the collision excites one of the participants into a
higher state, or transforms the particles into different species, or
creates additional particles, the collision is inelastic. Here, we
consider only elastic collisions.

For Coulomb forces, the cross section is infinite because the range of
the Coulomb force is infinite, but for interactions such as the strong
interaction in nuclear or particle physics, there is no long-range
force and cross-sections are finite. Even for Coulomb forces, the part
of the cross section that corresponds to a specific scattering angle,
$d\sigma/d\Omega$, which is a function of the scattering angle
$\theta_s$ is still finite.

If a particle travels through a thin target, the chance the particle
scatters is $P_{\rm scatt}=\sigma dN/dA$, where $dN/dA$ is the number
of scattering centers per area the particle encounters. If the density
of the target is $\rho$ particles per volume, and if the thickness of
the target is $t$, the areal density (number of target scatterers per
area) is $dN/dA=\rho t$. Because one wishes to quantify the collisions
independently of the target, experimentalists measure scattering
probabilities, then divide by the areal density to obtain
cross-sections,

$$
\begin{eqnarray}
\sigma=\frac{P_{\rm scatt}}{dN/dA}.
\end{eqnarray}
$$

Instead of merely stating that a particle collided, one can measure
the probability the particle scattered by a given angle. The
scattering angle $\theta_s$ is defined so that at zero the particle is
unscattered and at $\theta_s=\pi$ the particle is scattered directly
backward. Scattering angles are often described in the center-of-mass
frame, but that is a detail we will neglect for this first discussion,
where we will consider the scattering of particles moving classically
under the influence of fixed potentials $U(\boldsymbol{r})$. Because the
distribution of scattering angles can be measured, one expresses the
differential cross section,

$$
\begin{equation}
\frac{d^2\sigma}{d\cos\theta_s~d\phi}.
\label{_auto5} \tag{10}
\end{equation}
$$

Usually, the literature expresses differential cross sections as

$$
\begin{equation}
d\sigma/d\Omega=\frac{d\sigma}{d\cos\theta d\phi}=\frac{1}{2\pi}\frac{d\sigma}{d\cos\theta},
\label{_auto6} \tag{11}
\end{equation}
$$

where the last equivalency is true when the scattering does not depend
on the azimuthal angle $\phi$, as is the case for spherically
symmetric potentials.

The differential solid angle $d\Omega$ can be thought of as the area
subtended by a measurement, $dA_d$, divided by $r^2$, where $r$ is the
distance to the detector,

$$
\begin{eqnarray}
dA_d=r^2 d\Omega.
\end{eqnarray}
$$

With this definition $d\sigma/d\Omega$ is independent of the distance
from which one places the detector, or the size of the detector (as
long as it is small).

Differential scattering cross sections are calculated by assuming a
random distribution of impact parameters $b$. These represent the
distance in the $xy$ plane for particles moving in the $z$ direction
relative to the scattering center. An impact parameter $b=0$ refers to
being aimed directly at the target's center. The impact parameter
describes the transverse distance from the $z=0$ axis for the
trajectory when it is still far away from the scattering center and
has not yet passed it. The differential cross section can be expressed
in terms of the impact parameter,

$$
\begin{equation}
d\sigma=2\pi bdb,
\label{_auto7} \tag{12}
\end{equation}
$$

which is the area of a thin ring of radius $b$ and thickness $db$. In
classical physics, one can calculate the trajectory given the incoming
kinetic energy $E$ and the impact parameter if one knows the mass and
potential. From the trajectory, one then finds the scattering angle
$\theta_s(b)$. The differential cross section is then

$$
\begin{equation}
\frac{d\sigma}{d\Omega}=\frac{1}{2\pi}\frac{d\sigma}{d\cos\theta_s}=b\frac{db}{d\cos\theta_s}=\frac{b}{(d/db)\cos\theta_s(b)}.
\label{_auto8} \tag{13}
\end{equation}
$$

Typically, one would calculate $\cos\theta_s$ and $(d/db)\cos\theta_s$
as functions of $b$. This is sufficient to plot the differential cross
section as a function of $\theta_s$.

The total cross section is

$$
\begin{equation}
\sigma_{\rm tot}=\int d\Omega\frac{d\sigma}{d\Omega}=2\pi\int d\cos\theta_s~\frac{d\sigma}{d\Omega}.
\label{_auto9} \tag{14}
\end{equation}
$$

Even if the total cross section is infinite, e.g. Coulomb forces, one
can still have a finite differential cross section as we will see
later on.

An asteroid of mass $m$ and kinetic energy $E$ approaches a planet of
radius $R$ and mass $M$. What is the cross section for the asteroid to
impact the planet?

### Solution

Calculate the maximum impact parameter, $b_{\rm max}$, for which the asteroid will hit the planet. The total cross  section for impact is $\sigma_{\rm impact}=\pi b_{\rm max}^2$. The maximum cross-section can be found with the help of angular momentum conservation. The asteroid's incoming momentum is $p_0=\sqrt{2mE}$ and the angular momentum is $L=p_0b$. If the asteroid just grazes the planet, it is moving with zero radial kinetic energy at impact. Combining energy and angular momentum conservation and having $p_f$ refer to the momentum of the asteroid at a distance $R$,

$$
\begin{eqnarray*}
\frac{p_f^2}{2m}-\frac{GMm}{R}&=&E,\\
p_fR&=&p_0b_{\rm max},
\end{eqnarray*}
$$

allows one to solve for $b_{\rm max}$,

$$
\begin{eqnarray*}
b_{\rm max}&=&R\frac{p_f}{p_0}\\
&=&R\frac{\sqrt{2m(E+GMm/R)}}{\sqrt{2mE}}\\
\sigma_{\rm impact}&=&\pi R^2\frac{E+GMm/R}{E}.
\end{eqnarray*}
$$

## Center-of-Mass Coordinates

Thus far, we have considered the trajectory as if the force is
centered around a fixed point. For two bodies interacting only with
one another, both masses circulate around the center of mass. One
might think that solutions would become more complex when both
particles move, but we will see here that the problem can be reduced
to one with a single body moving according to a fixed force by
expressing the trajectories for $\boldsymbol{r}_1$ and $\boldsymbol{r}_2$ into the
center-of-mass coordinate $\boldsymbol{R}_{\rm cm}$ and the relative
coordinate $\boldsymbol{r}$,

$$
\begin{eqnarray}
\boldsymbol{R}_{\rm cm}&\equiv&\frac{m_1\boldsymbol{r}_1+m_2\boldsymbol{r}_2}{m_1+m_2},\\
\nonumber
\boldsymbol{r}&\equiv&\boldsymbol{r}_1-\boldsymbol{r_2}.
\end{eqnarray}
$$

Here, we assume the two particles interact only with one another, so
$\boldsymbol{F}_{12}=-\boldsymbol{F}_{21}$ (where $\boldsymbol{F}_{ij}$ is the force on $i$
due to $j$. The equations of motion then become

$$
\begin{eqnarray}
\ddot{\boldsymbol{R}}_{\rm cm}&=&\frac{1}{m_1+m_2}\left\{m_1\ddot{\boldsymbol{r}}_1+m_2\ddot{\boldsymbol{r}}_2\right\}\\
\nonumber
&=&\frac{1}{m_1+m_2}\left\{\boldsymbol{F}_{12}+\boldsymbol{F}_{21}\right\}=0.\\
\ddot{\boldsymbol{r}}&=&\ddot{\boldsymbol{r}}_1-\ddot{\boldsymbol{r}}_2=\left(\frac{\boldsymbol{F}_{12}}{m_1}-\frac{\boldsymbol{F}_{21}}{m_2}\right)\\
\nonumber
&=&\left(\frac{1}{m_1}+\frac{1}{m_2}\right)\boldsymbol{F}_{12}.
\end{eqnarray}
$$

The first expression simply states that the center of mass coordinate
$\boldsymbol{R}_{\rm cm}$ moves at a fixed velocity. The second expression
can be rewritten in terms of the reduced mass $\mu$.

$$
\begin{eqnarray}
\mu \ddot{\boldsymbol{r}}&=&\boldsymbol{F}_{12},\\
\frac{1}{\mu}&=&\frac{1}{m_1}+\frac{1}{m_2},~~~~\mu=\frac{m_1m_2}{m_1+m_2}.
\end{eqnarray}
$$

Thus, one can treat the trajectory as a one-body problem where the
reduced mass is $\mu$, and a second trivial problem for the center of
mass. The reduced mass is especially convenient when one is
considering gravitational problems because then

$$
\begin{eqnarray}
\mu \ddot{r}&=&-\frac{Gm_1m_2}{r^2}\hat{r}\\
\nonumber
&=&-\frac{GM\mu}{r^2}\hat{r},~~~M\equiv m_1+m_2.
\end{eqnarray}
$$

For the gravitational problem, the reduced mass then falls out and the
trajectory depends only on the total mass $M$.

The kinetic energy and momenta also have analogues in center-of-mass
coordinates. The total and relative momenta are

$$
\begin{eqnarray}
\boldsymbol{P}&\equiv&\boldsymbol{p}_1+\boldsymbol{p}_2=M\dot{\boldsymbol{R}}_{\rm cm},\\
\nonumber
\boldsymbol{q}&\equiv&\mu\dot{\boldsymbol{r}}.
\end{eqnarray}
$$

With these definitions, a little algebra shows that the kinetic energy becomes

$$
\begin{eqnarray}
T&=&\frac{1}{2}m_1|\boldsymbol{v}_1|^2+\frac{1}{2}m_2|\boldsymbol{v}_2|^2\\
\nonumber
&=&\frac{1}{2}M|\dot{\boldsymbol{R}}_{\rm cm}|^2
+\frac{1}{2}\mu|\dot{\boldsymbol{r}}|^2\\
\nonumber
&=&\frac{P^2}{2M}+\frac{q^2}{2\mu}.
\end{eqnarray}
$$

The standard strategy is to transform into the center of mass frame,
then treat the problem as one of a single particle of mass $\mu$
undergoing a force $\boldsymbol{F}_{12}$. Scattering angles can also be
expressed in this frame, then transformed into the lab frame. In
practice, one sees examples in the literature where $d\sigma/d\Omega$
expressed in both the "center-of-mass" and in the "laboratory"
frame.

## Rutherford Scattering

This refers to the calculation of $d\sigma/d\Omega$ due to an inverse
square force, $F_{12}=\pm\alpha/r^2$ for repulsive/attractive
interaction. Rutherford compared the scattering of $\alpha$ particles
($^4$He nuclei) off of a nucleus and found the scattering angle at
which the formula began to fail. This corresponded to the impact
parameter for which the trajectories would strike the nucleus. This
provided the first measure of the size of the atomic nucleus. At the
time, the distribution of the positive charge (the protons) was
considered to be just as spread out amongst the atomic volume as the
electrons. After Rutherford's experiment, it was clear that the radius
of the nucleus tended to be roughly 4 orders of magnitude smaller than
that of the atom, which is less than the size of a football relative
to Spartan Stadium.

The incoming and outgoing angles of the trajectory are at
$\pm\theta'$. They are related to the scattering angle by
$2\theta'=\pi+\theta_s$.

In order to calculate differential cross section, we must find how the
impact parameter is related to the scattering angle. This requires
analysis of the trajectory. We consider our previous expression for
the trajectory where we derived the elliptic form for the trajectory,
Eq. ([9](#eq:Ctrajectory)). For that case we considered an attractive
force with the particle's energy being negative, i.e. it was
bound. However, the same form will work for positive energy, and
repulsive forces can be considered by simple flipping the sign of
$\alpha$. For positive energies, the trajectories will be hyperbolas,
rather than ellipses, with the asymptotes of the trajectories
representing the directions of the incoming and outgoing
tracks. Rewriting Eq. ([9](#eq:Ctrajectory)),

$$
\begin{equation}\label{eq:ruthtraj} \tag{15}
r=\frac{1}{\frac{m\alpha}{L^2}+A\cos\theta}.
\end{equation}
$$

Once $A$ is large enough, which will happen when the energy is
positive, the denominator will become negative for a range of
$\theta$. This is because the scattered particle will never reach
certain angles. The asymptotic angles $\theta'$ are those for which
the denominator goes to zero,

$$
\begin{equation}
\cos\theta'=-\frac{m\alpha}{AL^2}.
\label{_auto10} \tag{16}
\end{equation}
$$

The trajectory's point of closest approach is at $\theta=0$ and the
two angles $\theta'$, which have this value of $\cos\theta'$, are the
angles of the incoming and outgoing particles. From
Fig (**to come**), one can see that the scattering angle
$\theta_s$ is given by,

$$
\begin{eqnarray}
\label{eq:sthetover2} \tag{17}
2\theta'-\pi&=&\theta_s,~~~\theta'=\frac{\pi}{2}+\frac{\theta_s}{2},\\
\nonumber
\sin(\theta_s/2)&=&-\cos\theta'\\
\nonumber
&=&\frac{m\alpha}{AL^2}.
\end{eqnarray}
$$

Now that we have $\theta_s$ in terms of $m,\alpha,L$ and $A$, we wish
to re-express $L$ and $A$ in terms of the impact parameter $b$ and the
energy $E$. This will set us up to calculate the differential cross
section, which requires knowing $db/d\theta_s$. It is easy to write
the angular momentum as

$$
\begin{equation}
L^2=p_0^2b^2=2mEb^2.
\label{_auto11} \tag{18}
\end{equation}
$$

Finding $A$ is more complicated. To accomplish this we realize that
the point of closest approach occurs at $\theta=0$, so from
Eq. ([15](#eq:ruthtraj))

$$
\begin{eqnarray}
\label{eq:rminofA} \tag{19}
\frac{1}{r_{\rm min}}&=&\frac{m\alpha}{L^2}+A,\\
\nonumber
A&=&\frac{1}{r_{\rm min}}-\frac{m\alpha}{L^2}.
\end{eqnarray}
$$

Next, $r_{\rm min}$ can be found in terms of the energy because at the
point of closest approach the kinetic energy is due purely to the
motion perpendicular to $\hat{r}$ and

$$
\begin{equation}
E=-\frac{\alpha}{r_{\rm min}}+\frac{L^2}{2mr_{\rm min}^2}.
\label{_auto12} \tag{20}
\end{equation}
$$

One can solve the quadratic equation for $1/r_{\rm min}$,

$$
\begin{equation}
\frac{1}{r_{\rm min}}=\frac{m\alpha}{L^2}+\sqrt{(m\alpha/L^2)^2+2mE/L^2}.
\label{_auto13} \tag{21}
\end{equation}
$$

We can plug the expression for $r_{\rm min}$ into the expression for $A$, Eq. ([19](#eq:rminofA)),

$$
\begin{equation}
A=\sqrt{(m\alpha/L^2)^2+2mE/L^2}=\sqrt{(\alpha^2/(4E^2b^4)+1/b^2}
\label{_auto14} \tag{22}
\end{equation}
$$

Finally, we insert the expression for $A$ into that for the scattering angle, Eq. ([17](#eq:sthetover2)),

$$
\begin{eqnarray}
\label{eq:scattangle} \tag{23}
\sin(\theta_s/2)&=&\frac{m\alpha}{AL^2}\\
\nonumber
&=&\frac{a}{\sqrt{a^2+b^2}}, ~~a\equiv \frac{\alpha}{2E}
\end{eqnarray}
$$

The differential cross section can now be found by differentiating the
expression for $\theta_s$ with $b$,

$$
\begin{eqnarray}
\label{eq:rutherford} \tag{24}
\frac{1}{2}\cos(\theta_s/2)d\theta_s&=&\frac{ab~db}{(a^2+b^2)^{3/2}}=\frac{bdb}{a^2}\sin^3(\theta_s/2),\\
\nonumber
d\sigma&=&2\pi bdb=\frac{\pi a^2}{\sin^3(\theta_s/2)}\cos(\theta_s/2)d\theta_s\\
\nonumber
&=&\frac{\pi a^2}{2\sin^4(\theta_s/2)}\sin\theta_s d\theta_s\\
\nonumber
\frac{d\sigma}{d\cos\theta_s}&=&\frac{\pi a^2}{2\sin^4(\theta_s/2)},\\
\nonumber
\frac{d\sigma}{d\Omega}&=&\frac{a^2}{4\sin^4(\theta_s/2)}.
\end{eqnarray}
$$

where $a= \alpha/2E$. This the Rutherford formula for the differential
cross section. It diverges as $\theta_s\rightarrow 0$ because
scatterings with arbitrarily large impact parameters still scatter to
arbitrarily small scattering angles. The expression for
$d\sigma/d\Omega$ is the same whether the interaction is positive or
negative.

Consider a particle of mass $m$ and charge $z$ with kinetic energy $E$
(Let it be the center-of-mass energy) incident on a heavy nucleus of
mass $M$ and charge $Z$ and radius $R$. Find the angle at which the
Rutherford scattering formula breaks down.

### Solution

Let $\alpha=Zze^2/(4\pi\epsilon_0)$. The scattering angle in Eq. ([23](#eq:scattangle)) is

$$
\sin(\theta_s/2)=\frac{a}{\sqrt{a^2+b^2}}, ~~a\equiv \frac{\alpha}{2E}.
$$

The impact parameter $b$ for which the point of closest approach
equals $R$ can be found by using angular momentum conservation,

$$
\begin{eqnarray*}
p_0b&=&b\sqrt{2mE}=Rp_f=R\sqrt{2m(E-\alpha/R)},\\
b&=&R\frac{\sqrt{2m(E-\alpha/R)}}{\sqrt{2mE}}\\
&=&R\sqrt{1-\frac{\alpha}{ER}}.
\end{eqnarray*}
$$

Putting these together

$$
\theta_s=2\sin^{-1}\left\{
\frac{a}{\sqrt{a^2+R^2(1-\alpha/(RE))}}
\right\},~~~a=\frac{\alpha}{2E}.
$$

It was from this departure of the experimentally measured
$d\sigma/d\Omega$ from the Rutherford formula that allowed Rutherford
to infer the radius of the gold nucleus, $R$.

Just like electrodynamics, one can define "fields", which for a small
additional mass $m$ are the force per mass and the additional
potential energy per mass. The {\it gravitational field} related to
the force has dimensions of force per mass, or acceleration, and can
be labeled $\boldsymbol{g}(\boldsymbol{r})$. The potential energy per mass has
dimensions of energy per mass. This is analogous to the
electromagnetic potential, which is the potential energy per charge,
and the electric field which is the force per charge.

Because the field $\boldsymbol{g}$ obeys the same inverse square law for a
point mass as the electric field does for a point charge, the
gravitational field also satisfies a version of Gauss's law,

$$
\begin{equation}
\label{eq:GravGauss} \tag{25}
\oint d\boldsymbol{A}\cdot\boldsymbol{g}=-4\pi GM_{\rm inside}.
\end{equation}
$$

Here, $M_{\rm inside}$ is the net mass inside a closed area.

Gauss's law can be understood by considering a nozzle that sprays
paint in all directions uniformly from a point source. Let $B$ be the
number of gallons per minute of paint leaving the nozzle. If the
nozzle is at the center of a sphere of radius $r$, the paint per
square meter per minute that is deposited on some part of the sphere
is

$$
\begin{eqnarray}
F(r)&=&\frac{B}{4\pi r^2}.
\end{eqnarray}
$$

Now, let $F$ also be assigned a direction, so that it becomes a vector
pointing along the direction of the flying paint. For any surface that
surrounds the nozzle, not necessarily a sphere, one can state that

$$
\begin{eqnarray}
\label{eq:paint} \tag{26}
\oint \boldsymbol{dA}\cdot\boldsymbol{F}&=&B,
\end{eqnarray}
$$

regardless of the shape of the surface. This follows because the rate
at which paint is deposited on the surface should equal the rate at
which it leaves the nozzle. The dot product ensures that only the
component of $\boldsymbol{F}$ into the surface contributes to the deposition
of paint. Similarly, if $\boldsymbol{F}$ is any radial inverse-square forces,
that falls as $B/(4\pi r^2)$, then one can apply
Eq. ([26](#eq:paint)). For gravitational fields, $B/(4\pi)$ is replaced
by $GM$, and one quickly "derives" Gauss's law for gravity,
Eq. ([25](#eq:GravGauss)).

Consider Earth to have its mass $M$ uniformly distributed in a sphere
of radius $R$. Find the magnitude of the gravitational acceleration as
a function of the radius $r$ in terms of the acceleration of gravity
at the surface $g(R)$. Assume $r<R$, i.e. you are inside the surface.

{\bf Solution}: Take the ratio of Eq. ([25](#eq:GravGauss)) for two radii, $R$ and $r<R$,

$$
\begin{eqnarray*}
\frac{4\pi r^2 g(r)}{4\pi R^2 g(R)}&=&\frac{4\pi GM_{\rm inside~r}}{4\pi GM_{\rm inside~R}}\\
\nonumber
&=&\frac{r^3}{R^3}\\
\nonumber
g(r)&=&g(R)\frac{r}{R}~.
\end{eqnarray*}
$$

The potential energy per mass is similar conceptually to the voltage, or electric potential energy per charge, that was studied in electromagnetism, if $V\equiv U/m$, $\boldsymbol{g}=-\nabla V$.

## Tidal Forces

Consider a spherical planet of radius $r$ a distance $D$ from another
body of mass $M$. The magnitude of the force due to $M$ on an small
object of mass $\delta m$ on surface of the planet can be calculated
by performing a Taylor expansion about the center of the spherical
planet.

$$
\begin{equation}
F=-\frac{GM\delta m}{D^2}+2\frac{GM\delta m}{D^3}\Delta D+\cdots
\label{_auto15} \tag{27}
\end{equation}
$$

If the $z$ direction points toward the large object, $\Delta D$ can be
referred to as $z$. In the accelerating frame of an observer at the
center of the planet,

$$
\begin{equation}
\delta m\frac{d^2 z}{dt^2}=F-\delta ma'+{\rm other~forces~acting~on~} \delta m,
\label{_auto16} \tag{28}
\end{equation}
$$

where $a'$ is the acceleration of the observer. Because $\delta ma'$
equals the gravitational force on $\delta m$ if it were located at the
planet's center, one can write

$$
\begin{equation}
m\frac{d^2z}{dt^2}=2\frac{GM\delta m}{D^3}z+{\rm other~forces~acting~on~}\delta m.
\label{_auto17} \tag{29}
\end{equation}
$$

Here the other forces could represent the forces acting on $\delta m$
from the spherical planet such as the gravitational force or the
contact force with the surface. If $\theta$ is the angle w.r.t. the
$z$ axis, the effective force acting on $\delta m$ is

$$
\begin{equation}
F_{\rm eff}\approx 2\frac{GM\delta m}{D^3}r\cos\theta\hat{z}+{\rm other~forces~acting~on~}\delta m.
\label{_auto18} \tag{30}
\end{equation}
$$

This first force is the "tidal" force. It pulls objects outward from the center of the object. If the object were covered with water, it would distort the objects shape so that the shape would be elliptical, stretched out along the axis pointing toward the large mass $M$. The force is always along (either parallel or antiparallel to) the $\hat{z}$ direction.

Consider the Earth to be a sphere of radius $R$ covered with water,
with the gravitational acceleration at the surface noted by $g$. Now
assume that a distant body provides an additional constant
gravitational acceleration $\boldsymbol{a}$ pointed along the $z$ axis. Find
the distortion of the radius as a function of $\theta$. Ignore
planetary rotation and assume $a<<g$.

{\bf Solution}: Because Earth would then accelerate with $a$, the
field $a$ would seem invisible in the accelerating frame. A tidal
force would only appear if $a$ depended on position, i.e. $\nabla
\boldsymbol{a}\ne 0$.

Now consider that the field is no longer constant, but that instead $a=-kz$ with $|kR|<<g$.

{\bf Solution}: The surface of the planet needs to be at constant
potential (if the planet is not accelerating). The force per mass,
$-kz$ is like a spring, and the potential per mass is
$kz^2/2$. Otherwise water would move to a point of lower
potential. Thus, the potential energy for a sample mass $\delta m$ is

$$
\begin{eqnarray*}
V(R)+\delta m gh(\theta)-\frac{\delta m}{2}kr^2\cos^2\theta={\rm Constant}\\
V(R)+\delta mgh(\theta)-\frac{\delta m}{2}kR^2\cos^2\theta-\delta m kRh(\theta)\cos^2\theta-\frac{\delta m}{2}kh^2(\theta)\cos^2\theta={\rm Constant}.
\end{eqnarray*}
$$

Here, the potential due to the external field is $(1/2)kz^2$ so that $-\nabla U=-kz$. One now needs to solve for $h(\theta)$. Absorbing all the constant terms from both sides of the equation into one constant $C$, and because both $h$ and $kR$ are small, we can through away terms of order $h^2$ or $kRh$. This gives

$$
\begin{eqnarray*}
gh(\theta)-\frac{1}{2}kR^2\cos^2\theta&=&C,\\
h(\theta)&=&\frac{C}{g}+\frac{1}{2g}kR^2\cos^2\theta,\\
h(\theta)&=&\frac{1}{2g}kR^2(\cos^2\theta-1/3).
\end{eqnarray*}
$$

The term with the factor of $1/3$ replaced the constant and was chosen so that the average height of the water would be zero.

The Sun's mass is $27\times 10^6$ the Moon's mass, but the Sun is 390 times further away from Earth as the Sun. What is ratio of the tidal force of the Sun to that of the Moon.

{\bf Solution}: The gravitational force due to an object $M$ a distance $D$ away goes as $M/D^2$, but the tidal force is only the difference of that force over a distance $R$,

$$
F_{\rm tidal}\propto \frac{M}{D^3}R.
$$

Therefore the ratio of force is

$$
\begin{eqnarray*}
\frac{F_{\rm Sun's~tidal~force}}{F_{\rm Moon's~tidal~force}}
&=&\frac{M_{\rm sun}/D_{\rm sun}^3}{M_{\rm moon}/D_{\rm moon}^3}\\
&=&\frac{27\times 10^6}{390^3}=0.46.
\end{eqnarray*}
$$

The Moon more strongly affects tides than the Sun.


```python
import copy
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

from ipywidgets import FloatProgress
from IPython.display import display

%load_ext autoreload
%autoreload 2
```

```python
import torch
import torch.utils.data
```

```python
# DGP related methods
import sys
sys.path.append('../..')

from dsvidgp.kernels import RBF
from dsvidgp.likelihoods import Gaussian
from dsvidgp.layers import SVGPLayer, NNLayer
from dsvidgp.initial_value import calc_median_of_pairwise_distance, calc_centroid
```

```python
RAND_SEED = 811
torch.manual_seed(RAND_SEED)
np.random.seed(RAND_SEED)
```

```python
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
device
```

    device(type='cpu')

```python
DATA_SIZE = 50
MINIBATCH_SIZE = 10

HIDDEN_LAYER_DIM = 3
INPUT_DIM = 1
OUTPUT_DIM = 1

# DNN configurations
DNN_HIDDEN_UNITS = 128
DNN_LEARNING_RATE = 0.001
DNN_TRAINING_NUM_EPOCHS = 1000

# GP configurations
NUM_INDUCINGS = 20
GP_LEARNING_RATE = 0.01
LAYERWISE_GP_NUM_EPOCHS = 100
DGP_NUM_EPOCHS = 1000

```

## Make data

```python

dx = np.random.rand(DATA_SIZE, 1).astype(np.float32) * 2.0 - 1.0
dy = np.where(dx + 0.04 * np.random.randn(*dx.shape) >= 0.0, 1.0, -1.0) \
        + 0.04 * np.random.randn(*dx.shape)
dy = dy.astype(np.float32)

nx = np.linspace(-1.0, 1.0, 200)[:, None]

plt.plot(dx, dy, 'o')
```

```python
class Dataset(torch.utils.data.Dataset):
    def __init__(self, dx, dy):

        self._N = len(dx)
        self._dx = dx
        self._dy = dy

    def __len__(self):
        return self._N

    def __getitem__(self, idx):
        return self._dx[idx], self._dy[idx]

dataset = Dataset(dx, dy)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=MINIBATCH_SIZE, shuffle=True)

```

```python
#Plot function
def plot_mean(pred_mean_list, plot_init=False):
    if plot_init:
        plt.plot(nx, pred_mean_list[0], label='init')
    for j in [10, 100, 1000]:
        if j < len(pred_mean_list):
            plt.plot(nx, pred_mean_list[j], label=str(j))
    plt.plot(dx, dy, 'o')
    plt.legend()
```

## Training method

```python
def train_dgp(optimizer, model, num_epochs):
    lower_bound_list = list()
    pred_mean_list = list()

    tx = torch.from_numpy(nx.astype(np.float32))

    pred_dstr = model.predict(tx.to(device))
    pred_mean_list.append(pred_dstr['mean'].data.cpu().numpy().copy())

    fp = FloatProgress(min=0, max=num_epochs)
    display(fp)

    for i in range(num_epochs):
        fp.value = i
        lower_bound = 0

        model.train()
        for b, (x_B, y_B) in enumerate(dataloader):
            x_B = x_B.to(device)
            y_B = y_B.to(device)

            optimizer.zero_grad()

            f_dstr = model(x_B)
            batch_bound = model.calc_bound(y_B, f_dstr)

            loss = -batch_bound  # maximize
            loss.backward()

            optimizer.step()

            lower_bound += batch_bound.data.cpu().item()

        lower_bound_list.append(lower_bound)

        model.eval()
        pred_dstr = model.predict(tx.to(device))
        pred_mean_list.append(pred_dstr['mean'].data.cpu().numpy().copy())

    return lower_bound_list, pred_mean_list

```

```python
def train_dnn(optimizer, model, criterion, num_epochs):
    loss_list = list()
    pred_list = list()

    tx = torch.from_numpy(nx.astype(np.float32))

    pred = model(tx.to(device))
    pred_list.append(pred.data.cpu().numpy().copy())

    fp = FloatProgress(min=0, max=num_epochs)
    display(fp)

    for i in range(num_epochs):
        fp.value = i
        loss_epoch = 0

        model.train()
        for b, (x_B, y_B) in enumerate(dataloader):
            x_B = x_B.to(device)
            y_B = y_B.to(device)

            optimizer.zero_grad()
            output = model(x_B)
            loss = criterion(output, y_B)
            loss.backward()
            optimizer.step()

            loss_epoch += loss.data.cpu().item()

        loss_list.append(loss_epoch)

        model.eval()
        pred = model(tx.to(device))
        pred_list.append(pred.data.cpu().numpy().copy())

    return loss_list, pred_list

```

```python
def train_layerwise_svgp(optimizer, svgp_train_config, dnn, num_epochs):
    lower_bound_list = list()

    fp = FloatProgress(min=0, max=num_epochs)
    display(fp)

    # Make all SVGP models training mode
    for cfg in svgp_train_config:
        cfg['svgp_model'].train()
    dnn.train() # Use dropout to avoid overfitting

    for i in range(num_epochs):
        fp.value = i
        lower_bound = 0

        for b, (x_B, y_B) in enumerate(dataloader):
            x_B = x_B.to(device)

            optimizer.zero_grad()

            # DNN forward propagation
            with torch.no_grad():
                dnn_hidden_value_dict = dnn.layers_forward(x_B)

            bound_list = list()
            for cfg in svgp_train_config:
                svgp = cfg['svgp_model']

                # Use DNN hidden layer values as input and output of Layerwise GP
                layer_input = dnn_hidden_value_dict[cfg['input_key']]
                layer_output = dnn_hidden_value_dict[cfg['output_key']]

                f_dstr = svgp(layer_input)
                bound = svgp.calc_bound(layer_output, f_dstr)
                bound_list.append(bound)
            bound_all = sum(bound_list)

            loss = -bound_all
            loss.backward()
            optimizer.step()

            lower_bound += bound.data.cpu().item()

        lower_bound_list.append(lower_bound)

    return lower_bound_list

```

## Pretraining 1: DNN training

```python
class DNN3(torch.nn.Module):
    """DNN used for pretraining of 3-layer DGP."""
    def __init__(self):
        super().__init__()
        self.layer1 = NNLayer(input_dim=INPUT_DIM,
                              output_dim=HIDDEN_LAYER_DIM,
                              num_hidden_units=DNN_HIDDEN_UNITS)

        self.layer2 = NNLayer(input_dim=HIDDEN_LAYER_DIM,
                              output_dim=HIDDEN_LAYER_DIM,
                              num_hidden_units=DNN_HIDDEN_UNITS)

        self.layer3 = NNLayer(input_dim=HIDDEN_LAYER_DIM,
                              output_dim=OUTPUT_DIM,
                              num_hidden_units=DNN_HIDDEN_UNITS,
                              batch_norm=False)

    def forward(self, x):
        h_dict = self.layers_forward(x)
        return h_dict['y']

    def layers_forward(self, x):
        """Output all hidden layer values."""
        h1 = self.layer1(x)
        h2 = self.layer2(h1)
        pred = self.layer3(h2)

        h_dict = {'x': x, 'h1': h1, 'h2': h2, 'y': pred}

        return h_dict

dnn_model =  DNN3()
dnn_model
```

    DNN3(
      (layer1): NNLayer(
        (seq): Sequential(
          (0): Linear(in_features=1, out_features=128, bias=True)
          (1): ReLU()
          (2): Dropout(p=0.2)
          (3): Linear(in_features=128, out_features=3, bias=False)
          (4): BatchNorm1d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (layer2): NNLayer(
        (seq): Sequential(
          (0): Linear(in_features=3, out_features=128, bias=True)
          (1): ReLU()
          (2): Dropout(p=0.2)
          (3): Linear(in_features=128, out_features=3, bias=False)
          (4): BatchNorm1d(3, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
        )
      )
      (layer3): NNLayer(
        (seq): Sequential(
          (0): Linear(in_features=3, out_features=128, bias=True)
          (1): ReLU()
          (2): Dropout(p=0.2)
          (3): Linear(in_features=128, out_features=1, bias=False)
        )
      )
    )

```python
optimizer_dnn = torch.optim.Adam(dnn_model.parameters(), lr=DNN_LEARNING_RATE)
loss_dnn_list, pred_mean_dnn_list = train_dnn(
    optimizer=optimizer_dnn,
    model=dnn_model,
    criterion=torch.nn.MSELoss(), num_epochs=DNN_TRAINING_NUM_EPOCHS)
```

    FloatProgress(value=0.0, max=1000.0)

```python
plt.plot(loss_dnn_list)
plt.xlabel('Epoch')
plt.ylabel('Loss')
```

```python
# Plot DNN predition performance
plot_mean(pred_mean_dnn_list)
```

## Pretraining 2: Layerwise GP training

```python
class SVGP(torch.nn.Module):
    """1-layer Stochastic Variational Gaussian process model."""

    def __init__(self, input_dim, output_dim, initial_lengthscale,
                 initial_inducing_input=None,
                 data_size=DATA_SIZE):
        super().__init__()

        kern = RBF(input_dim=input_dim,
                   initial_lengthscale=initial_lengthscale)
        self.layer = SVGPLayer(kernel=kern,
                                input_dim=input_dim,
                                output_dim=output_dim,
                                initial_inducing_input=initial_inducing_input,
                                num_inducings=NUM_INDUCINGS,
                                initial_q_S_value=1.0,
                                fix_inducing=False)
        # output
        self.likelihood = Gaussian(output_dim=output_dim,
                                   initial_variance=1e-3)

        self.data_size = data_size

    def forward(self, x):
        return self.layer.pred_mean_and_var(x)

    def calc_bound(self, y, f_dstr):
        pe = self.likelihood.predictive_expectation(y, f_dstr)

        kld = self.layer.kl_divergence()

        batch_size = y.size(0)
        bound = pe  - batch_size / self.data_size * kld

        return bound

    def predict(self, x):
        f_dstr = self.forward(x)

        y_dstr = self.likelihood.predict(f_dstr)
        return y_dstr

def make_svgp_list():
    lengthscale = calc_median_of_pairwise_distance([dx])
    centroid = calc_centroid(NUM_INDUCINGS, [dx])

    svgp1_model = SVGP(INPUT_DIM, HIDDEN_LAYER_DIM, initial_lengthscale=lengthscale,
                       initial_inducing_input=centroid)
    svgp2_model = SVGP(HIDDEN_LAYER_DIM, HIDDEN_LAYER_DIM, initial_lengthscale=1.0)

    return [svgp1_model, svgp2_model]

svgp_list = make_svgp_list()
svgp_list[0]
```

    SVGP(
      (layer): SVGPLayer(
        input_dim=1, output_dim=3, num_inducings=20
        (kernel): RBF()
      )
      (likelihood): Gaussian()
    )

```python
svgp_train_config = [
    {'input_key': 'x', 'output_key': 'h1', 'svgp_model': svgp_list[0]},
    {'input_key': 'h1', 'output_key': 'h2', 'svgp_model': svgp_list[1]},
]

optimizer_svgp_list = torch.optim.Adam(list(svgp_list[0].parameters()) + \
                                       list(svgp_list[1].parameters()),
                                       lr=0.01)
# optimizer_svgp_list.param_groups
lower_bound_list = train_layerwise_svgp(optimizer_svgp_list,
                                        svgp_train_config, dnn_model,
                                        num_epochs=LAYERWISE_GP_NUM_EPOCHS)
```

    FloatProgress(value=0.0)

```python
plt.plot(lower_bound_list)
plt.xlabel('Epoch')
plt.ylabel('ELBO')
```

## Define DGP model

```python
class DGP3(torch.nn.Module):
    """3-layer deep Gaussian process model."""

    def __init__(self, layer1, layer2, data_size):
        super().__init__()

        # layer 1
        self.layer1 = copy.deepcopy(layer1)
        self.layer1.fix_inducing_(False)

        # layer 2
        self.layer2 = copy.deepcopy(layer2)
        self.layer2.fix_inducing_(True)

        # layer 3
        kern3 = RBF(input_dim=HIDDEN_LAYER_DIM, initial_lengthscale=1.0)
        self.layer3 = SVGPLayer(kernel=kern3,
                                input_dim=HIDDEN_LAYER_DIM,
                                output_dim=OUTPUT_DIM,
                                num_inducings=NUM_INDUCINGS,
                                initial_q_S_value=0.01,
                                fix_inducing=True)

        # output
        self.likelihood = Gaussian(output_dim=OUTPUT_DIM, initial_variance=1e-3)

        self.layers = [self.layer1, self.layer2, self.layer3]
        self.data_size = data_size

    def forward(self, x):
        h1 = self.layer1(x)
        h2 = self.layer2(h1)

        return self.layer3.pred_mean_and_var(h2)

    def calc_bound(self, y, f_dstr):
        pe = self.likelihood.predictive_expectation(y, f_dstr)

        kld = sum(layer.kl_divergence() for layer in self.layers)

        batch_size = y.size(0)
        bound = pe  - batch_size / self.data_size * kld

        return bound

    def predict(self, x):
        f_dstr = self.forward(x)

        y_dstr = self.likelihood.predict(f_dstr)
        return y_dstr

def make_dgp3():
    model = DGP3(layer1=svgp_list[0].layer,
                 layer2=svgp_list[1].layer,
                 data_size=DATA_SIZE)

    return model

dgp_model = make_dgp3()
dgp_model
```

    DGP3(
      (layer1): SVGPLayer(
        input_dim=1, output_dim=3, num_inducings=20
        (kernel): RBF()
      )
      (layer2): SVGPLayer(
        input_dim=3, output_dim=3, num_inducings=20
        (kernel): RBF()
      )
      (layer3): SVGPLayer(
        input_dim=3, output_dim=1, num_inducings=20
        (kernel): RBF()
      )
      (likelihood): Gaussian()
    )

```python
optimizer = torch.optim.Adam(dgp_model.parameters(), lr=GP_LEARNING_RATE)
lower_bound_list, pred_mean_list = train_dgp(optimizer, dgp_model, num_epochs=DGP_NUM_EPOCHS)
```

    FloatProgress(value=0.0, max=1000.0)

## Results

```python
# plotting the change of ELBO
plt.plot(lower_bound_list)
plt.xlabel('Epoch')
plt.ylabel('ELBO')
```

### plot predicted mean for each epoch

```python
plt.plot(nx, pred_mean_list[0], label='init')
for j in [10, 100, 1000]:
    if j < len(pred_mean_list):
        plt.plot(nx, pred_mean_list[j], label=str(j))
plt.plot(dx, dy, 'o', label='data')
plt.legend()
```

## plot layer-wise value

```python
tx = torch.from_numpy(nx.astype(np.float32))
m1 = dgp_model.layer1.pred_mean_and_var(tx)['mean']
plt.plot(nx, m1.data.numpy())
```

```python
m2 = dgp_model.layer2.pred_mean_and_var(m1)['mean']
plt.plot(nx, m2.data.numpy())
```

Plot predictive distribution approximated by
$$
\begin{align}
p(y) &= \int p(y | f) q(f | \bar{h}^{(2)}) df \\
\bar{h}^{(2)} &= \mathbb{E}\left[ q(h^{(2)} | \bar{h}^{(1)}) \right] \\
\bar{h}^{(1)} &= \mathbb{E}\left[ q(h^{(1)} | x) \right].
\end{align}
$$

```python
f_dstr = dgp_model.layer3.pred_mean_and_var(m2)
y_dstr = dgp_model.likelihood.predict(f_dstr)
m_y = y_dstr['mean'].data.numpy()
s_y = np.sqrt(y_dstr['var'].data.numpy())
plt.fill_between(nx.ravel(), (m_y + s_y).ravel(), (m_y - s_y).ravel(), color='#cccccc')
plt.plot(nx, m_y, label='mean')
plt.plot(dx, dy, 'o', label='data')
plt.legend()
```

```python

```


### Intro
This notebook will go over variable importance metrics for uplift models. It will go over:

- Data Generating Process
- Model Building
- Variable Importance Metric

For an introduction on uplift models please see [example on single responses](https://github.com/Ibotta/ibotta_uplift/blob/master/examples/ibotta_uplift_multiple_response_example.ipynb)

### Data Generating Process

Below is the data generating process of the data we have.

\begin{equation}
x_1  \sim runif(0,1)
\end{equation}

\begin{equation}
x_2 \sim runif(0,1)
\end{equation}

\begin{equation}
x_3 \sim runif(0,1)
\end{equation}

\begin{equation}
e_1 \sim rnorm(0,1)
\end{equation}

\begin{equation}
e_2 \sim rnorm(0,1)
\end{equation}

\begin{equation}
t \sim rbinom(.5)
\end{equation}

\begin{equation}
noise \sim rnorm(0,1)
\end{equation}

\begin{equation}
revenue = x_1*t + e_1
\end{equation}

\begin{equation}
costs = x_2*t + e_2
\end{equation}

\begin{equation}
profit = revenue - costs
\end{equation}

```python
import numpy as np
import pandas as pd

from mr_uplift.dataset.data_simulation import get_simple_uplift_data
from mr_uplift.mr_uplift import MRUplift
from ggplot import *

num_obs = 10000
y, x, t = get_simple_uplift_data(num_obs)

y = pd.DataFrame(y)
y.columns = ['revenue','cost', 'noise']
y['profit'] = y['revenue'] - y['cost']

#include noise explanatory variable
x = pd.DataFrame(x)
x.columns = ['x_1', 'x_2']
x['x_3'] = np.random.normal(0, 1, num_obs)

```

    /Users/samweiss/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
      from ._conv import register_converters as _register_converters
    Using TensorFlow backend.
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:458: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint8 = np.dtype([("qint8", np.int8, 1)])
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:459: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:460: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint16 = np.dtype([("qint16", np.int16, 1)])
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:461: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:462: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      _np_qint32 = np.dtype([("qint32", np.int32, 1)])
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:465: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
      np_resource = np.dtype([("resource", np.ubyte, 1)])
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/ggplot/utils.py:81: FutureWarning: pandas.tslib is deprecated and will be removed in a future version.
    You can access Timestamp as pandas.Timestamp
      pd.tslib.Timestamp,
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/ggplot/stats/smoothers.py:4: FutureWarning: The pandas.lib module is deprecated and will be removed in a future version. These are private functions and can be accessed from pandas._libs.lib instead
      from pandas.lib import Timestamp
    /Users/samweiss/anaconda3/lib/python3.6/site-packages/statsmodels/compat/pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
      from pandas.core import datetools

```python
#build model
uplift_model = MRUplift()
param_grid = dict(num_nodes=[8], dropout=[.1,.5], activation=[
                          'relu'], num_layers=[1,2], epochs=[25], batch_size=[30])
uplift_model.fit(x, y, t.reshape(-1,1), param_grid = param_grid, n_jobs = 1)

```

### Variable Importance Uplift

The variable importance metrice described here is a variation on permutation importance; shuffle a column and measure how much the output disagrees with output of the original data.

Continuing the notation from the [multiple response example](https://github.com/Ibotta/ibotta_uplift/blob/master/examples/ibotta_uplift_multiple_response_example.ipynb) we have policy assignment $\pi(x_i, W)$ as a function of weights $W$, explanatory variables $X$, and estimated model $E[]$.

\begin{equation}
    \pi(x_i, W) =argmax \:_{t \in T} \sum_j w_j *E[y_{j,i} | X=x_i, T=t]
\end{equation}

To obtain a variable importance for a particular explanatory variable $p$ a permutation is performed on that column and a new dataset for each user is generated $x_{i,permuted_p}$. The variable importance is a disagreement between original decision for all $n$ observations :

\begin{equation}
   variableimportance_p = 1 -  1/n \sum_{i=1}^{n} I(\pi(x_i, W) = \pi(x_{i,permuted_p}, W))
\end{equation}

Intuitively, if the decisions of the permuted data is the same as the unpermuted data then we can conclude it is not an important variable. Alternatively, if the decisions are very different then we can conclude that variable is very important.

Below is the variable importance from the fitted model. Note that the noise variable $x_3$ has very low importance relative to the other two. This makes sense since $x_3$ does not effect the response variables.

```python
uplift_model.permutation_varimp(weights = np.array([.6,-.4,0,0]).reshape(1,-1))
```

      permuation_varimp_metric
      var_names

      0
      0.375714
      x_1

      1
      0.234571
      x_2

      2
      0.005429
      x_3

```python

```


Just Exploring

```python
from skidl.pyspice import *
#can you say cheeky
import PySpice as pspice
#becouse it's written by a kiwi you know
import lcapy as kiwi

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import sympy as sym

from IPython.display import YouTubeVideo, display

import traceback
import warnings
```

    WARNING: KICAD_SYMBOL_DIR environment variable is missing, so the default KiCad symbol libraries won't be searched.

```python
from DC_1_Codes import get_skidl_spice_ref
from AC_2_Codes import *

sym.init_printing()

#notebook specific loading control statements
%matplotlib inline
#tool to log notebook internals
#https://github.com/jrjohansson/version_information
%load_ext version_information
%version_information skidl, PySpice,lcapy, sympy, numpy, matplotlib, pandas, scipy
```

Software	Version

Python	3.7.6 64bit [GCC 7.3.0]

IPython	7.12.0

OS	Linux 4.19.104 microsoft standard x86_64 with debian bullseye sid

skidl	0.0.31.dev0

PySpice	1.4.3

lcapy	0.75.dev0

sympy	1.6.2

numpy	1.18.1

matplotlib	3.3.0

pandas	1.1.4

scipy	1.4.1

Thu Jan 28 00:41:09 2021 MST

```python
#instatate the rc_lowpass filter to
lowpassF=rlc_parallel_highpass()
lowpassF.lcapy_self()
```

```python
reset()
#create the nets
net_in=Net('In'); net_out=Net('Out');

#create a 1V AC test source and attache to nets
vs=SINEV( ac_magnitude=1@u_V); vs['p', 'n']+=net_in, gnd

#attaceh term_0 to net_in and term_2 to net_out per scikit-rf convention all
#other terminals are grounded
lowpassF.SKiDl(net_in, gnd, net_out, gnd)

circ=generate_netlist()
print(circ)

```

    .title
    V1 In 0 DC 0V AC 1V 0.0rad SIN(0V 1V 50Hz 0s 0Hz)
    L1 Out 0 1H
    C1 In Out 1
    R1 Out 0 1Ohm

    No errors or warnings found during netlist generation.

```python
sim=circ.simulator()
```

```python
res=sim.ac_sensitivity('V(Out)', 'lin', 10, 1@u_Hz, 10@u_MHz)
```

```python
res.elements
```

```python
res=sim.dc_sensitivity('V(Out)')
```

```python
res.branches
```

```python
res.elements
```

    {'v1': WaveForm v1 [0.]@V,
     'r1_scale': WaveForm r1_scale [0.]@V,
     'r1_bv_max': WaveForm r1_bv_max [0.]@V,
     'r1_m': WaveForm r1_m [0.]@V,
     'r1_w': WaveForm r1_w [0.]@V,
     'r1_l': WaveForm r1_l [0.]@V,
     'r1': WaveForm r1 [0.]@V,
     'r1:ef': WaveForm r1:ef [0.]@V,
     'r1:wf': WaveForm r1:wf [0.]@V,
     'r1:lf': WaveForm r1:lf [0.]@V,
     'r1:bv_max': WaveForm r1:bv_max [0.]@V,
     'l1_scale': WaveForm l1_scale [-0.]@V,
     'l1_m': WaveForm l1_m [-0.]@V,
     'c1_scale': WaveForm c1_scale [0.]@V,
     'c1_bv_max': WaveForm c1_bv_max [0.]@V,
     'c1_m': WaveForm c1_m [0.]@V,
     'c1:bv_max': WaveForm c1:bv_max [0.]@V}

```python
res.internal_parameters
```

```python
res.nodes
```

```python
circ.element_names
```

    odict_keys(['V1', 'L1', 'C1', 'R1'])

```python
for e in circ.element_names:
    print(e.lower())

```

    v1
    l1
    c1
    r1

```python

```