-
Notifications
You must be signed in to change notification settings - Fork 740
/
Copy pathmonte_carlo_honestforest.py
51 lines (43 loc) · 1.71 KB
/
monte_carlo_honestforest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import numpy as np
import matplotlib.pyplot as plt
import os
import time
import argparse
import warnings
import joblib
from econml.grf import RegressionForest
def monte_carlo():
n = 5000
d = 5
x_grid = np.linspace(-1, 1, 1000)
X_test = np.hstack([x_grid.reshape(-1, 1), np.random.normal(size=(1000, d - 1))])
coverage = []
exp_dict = {'point': [], 'low': [], 'up': []}
for it in range(100):
print(it)
X = np.random.normal(0, 1, size=(n, d))
y = X[:, 0] + np.random.normal(size=(n,))
est = RegressionForest(n_estimators=1000, verbose=1)
est.fit(X, y)
point = est.predict(X_test)
low, up = est.predict_interval(X_test, alpha=0.05)
coverage.append((low <= x_grid) & (x_grid <= up))
exp_dict['point'].append(point)
exp_dict['low'].append(low)
exp_dict['up'].append(up)
if not os.path.exists('figures'):
os.makedirs('figures')
if not os.path.exists(os.path.join("figures", 'honestforest')):
os.makedirs(os.path.join("figures", 'honestforest'))
plt.figure()
plt.plot(x_grid, np.mean(coverage, axis=0))
plt.savefig('figures/honestforest/coverage.png')
plt.figure()
plt.plot(x_grid, np.sqrt(np.mean((np.array(exp_dict['point']) - x_grid)**2, axis=0)), label='RMSE')
plt.savefig('figures/honestforest/rmse.png')
plt.figure()
plt.plot(x_grid, np.mean(np.array(exp_dict['up']) - np.array(exp_dict['low']), axis=0), label='length')
plt.savefig('figures/honestforest/length.png')
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Monte Carlo Coverage Tests for the RegressionForest')
monte_carlo()