

# <span style="color:#2c061f"> Inaugural project: Getting an Insurance</span>  

<br>

## <span style="color:#374045"> Introduction to Programming and Numerical Analysis </span>
*Oluf Kelkjær*

### **Today's Plan**  
1. Objectives
2. Remember to...
3. Important formal requirement
4. Question hints


## Objectives
* Apply simple numerical solution and simulation methods
    * Solve the problem using SciPy and numpy
* Structure a code project
    * Notebook, .py-files, readme etc.
* Document code
    * Document and explain your code using what you learned in **lecture 5**
* Present results in text form and in figures
    * Create nice tables and figures + create nice markdown cells

## Remember (general hint) (1/4)
Create a python-file for your functions  
**Remember** autoreload


```python
import pythonfile as py

%load_ext autoreload
%autoreload 2



```

## Remember (general hint) (2/4)
Document your code - at the exam you might have forgotten what you did and why


```python
import numpy as np

# 1. Initiliaze vectors of goods and container for utilities
psis = np.linspace(-10, -5, 5)
utility = np.empty((psis.shape)) # empty array with same shape as psi's

# 2. Calculate utility for all psis (relative risk aversion parameter
for i, psi in enumerate(psis):
    # i. Calculate utility using functions module
    u_psi = py.utility(1,psi)
    
    # ii. store the utility at the respective place (i)
    utility[i] = u_psi
    
    # iii. print result
    print(f'u(1,psi={psi:.2f})={u_psi:.2f}')
    
    
```

    u(1,psi=-10.00)=-0.11
    u(1,psi=-8.75)=-0.13
    u(1,psi=-7.50)=-0.15
    u(1,psi=-6.25)=-0.19
    u(1,psi=-5.00)=-0.25


## Remember (general hint) (3/4)  

Incoporate markdown cells in your project that describes the model, intuition, etc.  
Remember you do can do equation in markdown using either LaTeX syntax, `$$ math $$`:
$$ y_i = \beta_0 + \beta_1 x_i + \epsilon_i $$  
or `\begin{equation} math \end{equation}`  
\begin{align} y_i = \beta_0 + \beta_1 x_i + \epsilon_i \end{align}  

Other formatting?
* An indent followed by `*` or `1.` creates list
    1. list item 1
    2. list item 2
    * list item 3
* `** text **` produces **text**
* `*text*` produces *text*
* `#` creates a large header, `##` smaller header and so on
* Google it!

## Remember (general hint) (4/4)
**Always** add docstrings to your functions.
The function name in itself isn't always as informal as you'd want


```python
def utility(z,psi):
    """ Calculates utility of given some assets, z, and relative risk aversion, psi.

    Args:
    z (float): the agents assets
    psi (float): the agents relative risk aversion

    Returns:
    (float): utility of assets
    """
    return (z**(1+psi))/(1+psi)


```

## Important requirement!

**ALLWAYS** restart your *kernel* and *run all cells* **before** handing in! This goes for the mandatory assignments along with the exam. It is crucial!

If you have crucual old variables in memory that makes your notebook run, then i cannot run your notebook!!  

**Red flag** if the notebook can't be run from start to end!!

## Getting an insurance

Uninsured agents expected utility:
<p style="text-align: center;">$V_0 = pu(y-x)+(1-p)u(y)$</p>
Insured agent expected utility:
<p style="text-align: center;">$V(q;\pi)=pu(y-x+q-\pi(p,q))+(1-p)u(y-\pi(p,q))$</p>
Insurance company requires premium (in order to not go broke):
<p style="text-align: center;">$\pi(p,q) = pq$</p>
Agents utility given by:
<p style="text-align: center;">$u(z) = \frac{z^{1+\psi}}{1+\psi}$</p>
Parameters are:
<p style="text-align: center;">$y=1, p = 0.2, \psi = -2$</p>

## Hints for question 1
Once the *utility function*, *insurance premium* and the *insured agents expected utility* has been defined, we're ready to solve **question 1**. 

Find optimal coverage, $q^*$, for $x$ in range $[0.01,0.9]$:
<p style="text-align: center;">$q^\ast = \underset{q\in[0,x]}{\operatorname{argmax}} V(q;\pi)$</p>


1.  create `x` array in range `[0.01,0.9]` and an empty array for optimal `q's` to be stored.  
2.  Loop over `x's` where you define an ojective function for each `x`.   
      * You can solve the objective function using the `minimize_scalar` funtion and store $q^*(x)$
      * Remember that $q\in[0,x]$ which means your solver should be **bounded**  
      * Remember remember that we're maximizing (-minimize)
    

Plot the `x` and `q` arrays and comment!


## Hints for question 2:
Find $\tilde{\pi}$ such that $V(q;\tilde{\pi})=V_0$ at each point in $q$-array. 
1. Construct $q$ and (empty) $\pi$-array
2. Create new objective function $V(q;\tilde{\pi})=V_0\rightarrow V(q;\tilde{\pi})-V_0=0$  
3. Loop through the $q$'s 
    * Can use `optimize.root` (solves objective function = 0) **seen in lecture 3**
    * store results in $\pi$-array
4. plot $q$ and $\pi$-array and comment!

## Hints for question 3:
Create a function for Monte Carlo integration - **seen in lecture 4**.  
You can advantageously use two different `SimpleNamespace()`  
One for model parameters and the other for different policy parameters

## Hints for question 4:  
Define new objective function (MC under no coverage - MC under coverage)  
Solve the objective function using `optimize.root` with `method='broyden1'`

0bd92678d1fb03f6b2efe2c3af3070da0c470834
ipynb
Jupyter Notebook
Slides/exc_6/6.class.ipynb
OlufKelk/IPNA
27abbcd0de9d2da33169f1caf9604ebb58682c61
MIT
Slides/exc_6/6.class.ipynb
OlufKelk/IPNA
27abbcd0de9d2da33169f1caf9604ebb58682c61
MIT
Slides/exc_6/6.class.ipynb
OlufKelk/IPNA
27abbcd0de9d2da33169f1caf9604ebb58682c61
MIT
Qwen/Qwen-72B
1. YES
2. YES