<jupyter_start><jupyter_text>Exercise 3: Demand function  1. Find the equilibrium distirbution of mileage<jupyter_code># Setup
do_settings = {
    'n': 90
}

model = zurcher(**do_settings)
solver = solve_NFXP()

#Read the data
data = model.read_busdata(bustypes=[1,2,3,4])
samplesize = data.shape[0]
uncond_R_P = sum(data.d)/samplesize

# Estimate
nfxp_model, optim_res, pnames, theta_hat, Avar, converged=estimate.estimate(model, solver,data)


# Update the values with the estimated parameters
do_settings = {
    'n': 90,
    'RC': theta_hat[0],
    'c': theta_hat[1],
    'p': np.array(theta_hat[2:])
}

model_est = zurcher(**do_settings)
ev,pk = solver.poly(model_est.bellman, beta = model_est.beta, output=2)

# Find the probability functions
pp, pp_K, pp_R = model_est.eqb(pk)

fig = plt.figure(figsize=(8,6)) 
ax = fig.add_subplot(1,1,1)

ax.plot(model_est.grid*(model_est.max*1000)/model_est.n,pp_K/sum(pp_K))
ax.plot(model_est.grid*(model_est.max*1000)/model_est.n,pp_R/sum(pp_R))


ax.set_title(f'Equilibrium Distribution: Bus mileage')
ax.set_xlabel("Mileage")
ax.set_ylabel("Conditional Probability Density")
ax.set_xlim(0, 420000)
ax.set_ylim(0, 0.03)

plt.legend((f'$Pr(x, i=Keep)$',f'$Pr(x, i=Replace)$'))

plt.show()<jupyter_output><empty_output><jupyter_text>2. Find the scale of the cost functiona) How does the scale of the cost function relates to the estimate of RC and c?b) What is the scale estimate? Hint: Use the stuctural estimates and tabel III in  Rust (1987), "Optimal Replacement of GMC Bus Engines: An Emperical Model of Harold Zurcher  3. Find the implied Demand function<jupyter_code># Expected Demand, as a function of Replacement cost
import copy
mp0 = copy.copy(model_est.RC)
NRC = 59

RCgrid = np.linspace(1,30,NRC)

Demand = np.empty(NRC)

for i,val in enumerate(RCgrid):
    do_settings = {
        'n': 90,
        'RC': val
    }
    
    model = zurcher(**do_settings)
    _,pk = solver.poly(model.bellman, beta= model.beta, output=2)

    # Find the probability functions
    pp, pp_K, pp_R = model.eqb(pk)
    
    Demand[i] = 12*sum(pp_R)

fig = plt.figure(figsize=(8,6)) 
ax2 = fig.add_subplot(1,1,1)
ax2.plot(RCgrid*scale,Demand)
ax2.set_title(f'Expected Replacement Demand Function')
ax2.set_xlabel("Replacement cost, RC")
ax2.set_ylabel("Expected Annual Engine Replacement")
ax2.set_xlim(0,12000)
ax2.set_ylim(0,1)

plt.show()<jupyter_output><empty_output><jupyter_text>4. Try estimate the demand function for different values of $\beta$. Does the change in the demand function makes intuitively sense?<jupyter_code><jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1332819722650231, 'fraction_numerical': 0.029661016949152543, 'mean_word_length': 5.242788461538462, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '664072', 'license': 'MIT', 'n_tokens_mistral': 1055, 'n_tokens_neox': 1002, 'n_words': 229, 'path': 'codeZurcher/codeZurcher/zurcher_python/1_NFXP/ex-ante/Exercise3_exante.ipynb', 'repo_name': 'dseconf/BergenIO2021', 'size': '2596'}