def two_factor_fct_w_random(beta_0, beta_1, beta_2, beta_12):
def _y(x1, x2):
return beta_0 + beta_1*x1 + beta_2*x2 + beta_12*x1*x2 + np.random.normal(0,np.sqrt(beta_0))
return _y
# relative error from the factorial estimator
def factorial_estimate(beta_0, beta_A, beta_B, beta_AB, N_comparisons):
measurement = two_factor_fct_w_random(beta_0, beta_A, beta_B, beta_AB)
error_estimate_A_factorial = []
error_estimate_B_factorial = []
for _ in range(N_comparisons):
AmBm = measurement(-1,-1)
ApBm = measurement(1,-1)
AmBp = measurement(-1,1)
ApBp = measurement(1,1)
beta_A_ = 1/4*(ApBp + ApBm - AmBp - AmBm)
beta_B_ = 1/4*(ApBp + AmBp - ApBm - AmBm)
error_estimate_A_factorial.append(beta_A_-beta_A)
error_estimate_B_factorial.append(beta_B_-beta_B)
mean_diff_A_fact = np.mean(np.abs(error_estimate_A_factorial))/beta_A
mean_diff_B_fact = np.mean(np.abs(error_estimate_B_factorial))/beta_B
return mean_diff_A_fact, mean_diff_B_fact
# one factor at a time (ofat) approach
def ofat_estimate(beta_0, beta_A, beta_B, beta_AB, N_comparisons, benchmark_A, benchmark_B,
nonconvergence_safeguard=10):
measurement = two_factor_fct_w_random(beta_0, beta_A, beta_B, beta_AB)
error_estimate_A_changing_factor = []
error_estimate_B_changing_factor = []
mean_diff_A_CF = []
mean_diff_B_CF = []
N_rep = 1
estimating = True
while estimating and (nonconvergence_safeguard-N_rep>=0):
error_estimate_A_changing_factor.append([])
error_estimate_B_changing_factor.append([])
for _ in range(N_comparisons):
ApBm = np.mean([measurement(1,-1) for _ in range(N_rep)])
AmBp = np.mean([measurement(-1,1) for _ in range(N_rep)])
AmBm = np.mean([measurement(-1,-1) for _ in range(N_rep)])
beta_A_ = (ApBm-AmBm)/2
beta_B_ = (AmBp-AmBm)/2
error_estimate_A_changing_factor[N_rep-1].append(beta_A_-beta_A)
error_estimate_B_changing_factor[N_rep-1].append(beta_B_-beta_B)
mean_diff_A_CF.append( np.mean(np.abs(error_estimate_A_changing_factor))/beta_A )
mean_diff_B_CF.append( np.mean(np.abs(error_estimate_B_changing_factor))/beta_B )
# the mean diffs of the factorial approach acts as benchmark
# we keep on estimating (with increased N_rep) until
# the one-factor-at-a-time error is lower than the benchmark
if mean_diff_A_CF[-1]>benchmark_A or mean_diff_B_CF[-1]>benchmark_B:
N_rep += 1
else:
estimating = False
return mean_diff_A_CF, mean_diff_B_CF
# the horizontal line intersects the line between
# known positions x and y => what is (?) ?
# |:.
# | x.
# | :.
# |------?---
# | y.
# |__________:._
def interpolate(series, intersect):
value = 0
prev = series[0]
for i,e in enumerate(series):
if e<intersect and prev>=intersect:
value = i + (prev-intersect)/(prev-e)
break
prev = e
return value