<jupyter_start><jupyter_text>Exercise 5     Introduction to Programming and Numerical Analysis     <NAME>   Plan for today1. Welcome2. Interactive plotting3. Solving an exchange model4. Problemset 2**Note: The inuagural project is now live! Deadline 27/3.**  2. Interactive plottingLast time i showed you how to plot static functions. Today, we are going to be plotting interactively. This means that the plots will change dynamically once you change model parameters. This requires that you formulate your plot as a function. Let's try it out:<jupyter_code># Imports
import ipywidgets as widgets
import matplotlib.pyplot as plt
import numpy as np
import OLG_trans as OLG #OLG transition functions
#Center images in notebook (optional)
from IPython.core.display import HTML
HTML("""
<style>
.output_png {
    display: table-cell;
    text-align: center;
    vertical-align: middle;
}
</style>
""")
# Figure to plot
def fig(rho,alpha):
    """
    Returns:
    Plots the transition curve
    
    Args:
    rho(float): Timepreference parameter
    """
    #parameters
    alpha = alpha
    rho = rho #Time preference parameter (value not defined)
    n = 0.2
    
    #transition curve
    k_1, k_2 = OLG.transition_curve(alpha,rho,n,T=1000,k_min=1e-20,k_max=6)
    
    fig = plt.figure(figsize=(9,9))
    ax = fig.add_subplot(1,1,1)
    ax.plot(k_1,k_2, label="Transition curve") #transition curve
    ax.plot(k_1,k_1, '--', color='grey',label="45 degree") #45 degree line
    ax.set_xlabel('$k_t$')
    ax.set_ylabel('$k_t+1$')
    ax.set_title('Transition curve')
    ax.legend()
    ax.set_xlim([0,0.2])
    ax.set_ylim([0,0.2]);
    return
# Interactive plot
import ipywidgets as widgets
widgets.interact(fig, 
    rho = widgets.FloatSlider(description='rho', min=0, max=16, step=0.01, value=0.5),
                 alpha = widgets.FloatSlider
                )<jupyter_output><empty_output><jupyter_text>Widget typesBefore we used the `FloatSlider` argument. However, there are several others you can use. These include:- `IntSlider` discrete version of `FloatSlider`- `Dropdown` creates a dropdown menu of things to choose from- `ToggleButtons` creates buttons you can click on to change the plotRemember: Your plot function needs to be able to accomodate these elements!   3. Solving an exchange modelHere are some tips on how to solve the exchange model in PS2. 1. You may define a dictionary of model parameters or options. I think this is less confusing and makes your functions simpler. Instead of:<jupyter_code>N = 10000
mu = 0.5
sigma = 0.2
mu_low = 0.1
mu_high = 0.9
beta1 = 1.3
beta2 = 2.1
seed = 1986<jupyter_output><empty_output><jupyter_text>Store them in a dictionary and use this a single input to the function:<jupyter_code>mp = {"N":1000,
      "mu":0.5,
      "sigma":0.2,
      "mu_low":0.1,
      "mu_high":0.9,
      "beta1":1.3,
      "beta2":2.1,
      "seed":1986}
# Function example:
def f(x1,x2,mp):
    y=x1*mp["beta1"]+x2*mp["beta2"]
    return y<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10734463276836158, 'fraction_numerical': 0.03456297773346627, 'mean_word_length': 3.7626582278481013, 'pattern_counts': {'":': 8, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '143326', 'license': 'MIT', 'n_tokens_mistral': 1047, 'n_tokens_neox': 990, 'n_words': 323, 'path': 'class 5/exercise 5.ipynb', 'repo_name': 's-honore/NumEcon_slides22', 'size': '3019'}