<jupyter_start><jupyter_text>Flow Control and Loops<jupyter_code>from numpy import zeros, ones, absolute
from numpy.random import randn
import scipy.stats as stats<jupyter_output><empty_output><jupyter_text>Exercise 1<jupyter_code>r1 = randn(1)
r2 = randn(1)
if r1<0 and r2<0:
    pass
elif (r1>=0 and r2<0) or (r1<0 and r2>=0):
    pass
else:
    pass<jupyter_output><empty_output><jupyter_text>Exercise 2<jupyter_code>T = 1000
e = randn(2*T)
phi1 = 1.4
phi2 = -.8
theta1 = 0.4
theta2 = 0.8
y = zeros(e.shape)
for t in range(2,2*T):
    y[t] = phi1*y[t-1] + phi2*y[t-2] + theta1*e[t-1] + theta2*e[t-2] + e[t]
y = y[1000:]<jupyter_output><empty_output><jupyter_text>Exercise 3<jupyter_code>omega = 0.05 
alpha = 0.05
beta  = 0.9
y = zeros(2*T)
sigma2 = ones(2*T)
for i in range(1,2*T):
    sigma2[t] = omega + alpha*(y[t-1]**2) + beta*sigma2[t-1]
    y[t] = sigma2[t]*e[t]
sigma2 = sigma2[1000:]
y = y[1000:]<jupyter_output><empty_output><jupyter_text>Exercise 4<jupyter_code>omega = 0.05 
alpha = 0.02
gamma = 0.07
beta  = 0.9
y = zeros(2*T)
sigma2 = ones(2*T)
for i in range(1,2*T):
    sigma2[t] = omega + alpha*(y[t-1]**2) + gamma*((y[t-1]<0)*y[t-1]**2) + beta*sigma2[t-1]
    y[t] = sigma2[t]*e[t]
sigma2 = sigma2[1000:]
y = y[1000:]<jupyter_output><empty_output><jupyter_text>Exercise 6<jupyter_code>M = 5
x = zeros((M,M))
for i in range(M):
    for j in range(M):
        x[i,j] = i * j
y = zeros((M,M))
for i,row in enumerate(y):
    for j,col in enumerate(row):
        y[i,j] = i * j<jupyter_output><empty_output><jupyter_text>Exercise 7<jupyter_code>def invert_normal_cdf(prob):
    UB = 10.0
    LB = -10.0
    cdf = stats.norm(loc=0,scale=1).cdf 
    distance = 1
    while distance > 1e-8:
        MP = (UB+LB)/2
        MPc = cdf(MP)
        if prob>MPc:
            LB = MP
        else:
            UB = MP
        distance = absolute(MPc-prob)
    return MP<jupyter_output><empty_output><jupyter_text>Exercise 8<jupyter_code>icdf = stats.norm(loc=0,scale=1).ppf

print("invert_normal_cdf(.01):")
invert_normal_cdf(.01)
print("icdf(.01):")
icdf(.01)
print("invert_normal_cdf(.5):")
invert_normal_cdf(.5)
print("icdf(.5):")
icdf(.5)
print("invert_normal_cdf(.975):")
invert_normal_cdf(.975)
print("icdf(.975):")
icdf(.975)<jupyter_output><empty_output><jupyter_text>Exercise 9<jupyter_code>r = randn(20)
y = [val for val in r if val<0]
y

print("r[r<0]:")
print(r[r<0])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1826086956521739, 'fraction_numerical': 0.06749482401656315, 'mean_word_length': 4.3215859030837, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 41, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '362625', 'license': 'CC0-1.0', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1127, 'n_words': 197, 'path': 'solutions/chapter12/flow_control_and_loops_solutions.ipynb', 'repo_name': 'ColdTeapot273K/python-for-econometrics-statistics-data-analysis', 'size': '2415'}