Calibration
Contents
Calibration#
Calibration consists on finding the parameters that will match the momments of the simulated data with the true values of the parameters.
Packages#
import time
import numpy as np
import pandas as pd
import seaborn as sns
import dhr_functions as dhr
from matplotlib import pyplot as plt
from scipy.optimize import minimize
Data moments#
Simulate data for treatment and control to get the momments (means) using the estimated parameters (beta = 0.03 and mu = 4)
data = dhr.dataframe_simulation()
data_t = data['treatment']
data_c = data['control']
d_mean_t0 = data_t.loc[data_t['t'] == 30]['d+1'].mean()
d_mean_c0 = data_c.loc[data_c['t'] == 30]['d+1'].mean()
The means are
print(d_mean_c0) # control
print(d_mean_t0) # treatment
5.2
20.68
Let’s check a different value. With these values, the roots are very different.
theta = [0.03,0]
dhr.root_calibration(theta)
[[30. 29.52]]
675.7348
Optimizer#
Now we will use the optmizer again to find the combination of parameters value that match the momments of our data using the estimated parameters. Again, we neet to define the function to use the minimizer.
def root_calibration(theta):
d_mean_c0 = 5.2
d_mean_t0 = 20.82
means, _, _, _, _, _, _, = dhr.simulate_data(theta, M = 10, I = 50, gs = 1)
print(means)
f = ( means[0,1] - d_mean_c0)**2 + (means[0,0] - d_mean_t0)**2
return f
Define a initial guess and this function will find the combination that generates the same momments see before
start = time.perf_counter()
theta = [0.01,4]
resu = minimize(root_calibration,theta, method='Nelder-Mead')
end = time.perf_counter()
[[5.29 5.2 ]]
[[5.29 5.2 ]]
[[3.99 3.76]]
[[8.18 6.69]]
[[13.53 8.41]]
[[13.01 8.41]]
[[19.19 12.78]]
[[23.41 17.14]]
[[19.46 12.78]]
[[21.79 14.9 ]]
[[23.58 17.14]]
[[17.02 10.44]]
[[17.35 10.44]]
[[16.14 9.41]]
[[13.67 8.41]]
[[18.41 11.54]]
[[15.61 9.41]]
[[16.47 9.9 ]]
[[16.79 9.9 ]]
[[16.7 9.65]]
[[17.54 10.18]]
[[17.98 10.32]]
[[17.44 9.56]]
[[17.49 9.1 ]]
[[18.69 9.75]]
[[19.53 9.77]]
[[19.11 8.57]]
[[19.59 7.74]]
[[21.65 8.35]]
[[21.68 6.57]]
[[22.66 5.29]]
[[19.64 6.11]]
[[18.1 5.11]]
[[21.76 5.05]]
[[22.76 3.82]]
[[19.54 4.59]]
[[21.86 3.64]]
[[20.26 5.43]]
[[22.16 5.91]]
[[20.36 4.91]]
[[18.71 5.29]]
[[21. 5.15]]
[[21.17 4.6 ]]
[[20.46 5.24]]
[[21.1 5.41]]
[[21.7 5.32]]
[[20.71 5.26]]
[[20.66 4.93]]
[[20.74 5.06]]
[[20.5 5.22]]
[[20.91 5.18]]
[[20.87 5.32]]
[[20.86 5.26]]
[[21. 5.19]]
[[20.77 5.24]]
[[20.73 5.33]]
[[20.87 5.22]]
[[20.78 5.2 ]]
[[20.78 5.14]]
[[20.88 5.15]]
[[20.78 5.22]]
[[20.75 5.2 ]]
[[20.86 5.22]]
[[20.77 5.21]]
[[20.79 5.22]]
[[20.79 5.17]]
[[20.79 5.2 ]]
[[20.85 5.22]]
[[20.79 5.21]]
[[20.79 5.18]]
[[20.79 5.21]]
[[20.79 5.2 ]]
[[20.79 5.17]]
[[20.79 5.2 ]]
[[20.79 5.18]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.19]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.19]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
[[20.79 5.2 ]]
Let’s seet the roots and the momments generated
print('Optimun beta:' + str(round(resu.x[0],2)) + "\n" + 'Optimun mu:' + str(round(resu.x[1],2)))
print("Elapsed (with compilation) = {}s".format((end - start)))
Optimun beta:0.03
Optimun mu:4.0
Elapsed (with compilation) = 18.297212792s
This matches exaclty the moments that we see in the data, the parameters are exaclty the true value, 0.03 and 4.