stefantkeller

doe-intro_functions

Dec 18th, 2022
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.16 KB | Source Code | 0 0
  1. def two_factor_fct_w_random(beta_0, beta_1, beta_2, beta_12):
  2.     def _y(x1, x2):
  3.         return beta_0 + beta_1*x1 + beta_2*x2 + beta_12*x1*x2 + np.random.normal(0,np.sqrt(beta_0))
  4.     return _y
  5.  
  6. # relative error from the factorial estimator
  7. def factorial_estimate(beta_0, beta_A, beta_B, beta_AB, N_comparisons):
  8.     measurement = two_factor_fct_w_random(beta_0, beta_A, beta_B, beta_AB)
  9.    
  10.     error_estimate_A_factorial = []
  11.     error_estimate_B_factorial = []
  12.  
  13.     for _ in range(N_comparisons):
  14.         AmBm = measurement(-1,-1)
  15.         ApBm = measurement(1,-1)
  16.         AmBp = measurement(-1,1)
  17.         ApBp = measurement(1,1)
  18.  
  19.         beta_A_ = 1/4*(ApBp + ApBm - AmBp - AmBm)
  20.         beta_B_ = 1/4*(ApBp + AmBp - ApBm - AmBm)
  21.  
  22.         error_estimate_A_factorial.append(beta_A_-beta_A)
  23.         error_estimate_B_factorial.append(beta_B_-beta_B)
  24.  
  25.     mean_diff_A_fact = np.mean(np.abs(error_estimate_A_factorial))/beta_A
  26.     mean_diff_B_fact = np.mean(np.abs(error_estimate_B_factorial))/beta_B
  27.    
  28.     return mean_diff_A_fact, mean_diff_B_fact
  29.      
  30.    
  31. # one factor at a time (ofat) approach
  32. def ofat_estimate(beta_0, beta_A, beta_B, beta_AB, N_comparisons, benchmark_A, benchmark_B,
  33.                   nonconvergence_safeguard=10):
  34.     measurement = two_factor_fct_w_random(beta_0, beta_A, beta_B, beta_AB)
  35.  
  36.     error_estimate_A_changing_factor = []
  37.     error_estimate_B_changing_factor = []
  38.     mean_diff_A_CF = []
  39.     mean_diff_B_CF = []
  40.  
  41.  
  42.     N_rep = 1
  43.     estimating = True
  44.  
  45.     while estimating and (nonconvergence_safeguard-N_rep>=0):
  46.         error_estimate_A_changing_factor.append([])
  47.         error_estimate_B_changing_factor.append([])
  48.         for _ in range(N_comparisons):
  49.             ApBm = np.mean([measurement(1,-1) for _ in range(N_rep)])
  50.             AmBp = np.mean([measurement(-1,1) for _ in range(N_rep)])
  51.             AmBm = np.mean([measurement(-1,-1) for _ in range(N_rep)])
  52.  
  53.             beta_A_ = (ApBm-AmBm)/2
  54.             beta_B_ = (AmBp-AmBm)/2
  55.  
  56.             error_estimate_A_changing_factor[N_rep-1].append(beta_A_-beta_A)
  57.             error_estimate_B_changing_factor[N_rep-1].append(beta_B_-beta_B)
  58.  
  59.  
  60.         mean_diff_A_CF.append( np.mean(np.abs(error_estimate_A_changing_factor))/beta_A )
  61.         mean_diff_B_CF.append( np.mean(np.abs(error_estimate_B_changing_factor))/beta_B )
  62.  
  63.         # the mean diffs of the factorial approach acts as benchmark
  64.         # we keep on estimating (with increased N_rep) until
  65.         # the one-factor-at-a-time error is lower than the benchmark
  66.         if mean_diff_A_CF[-1]>benchmark_A or mean_diff_B_CF[-1]>benchmark_B:
  67.             N_rep += 1
  68.         else:
  69.             estimating = False
  70.            
  71.     return mean_diff_A_CF, mean_diff_B_CF
  72.    
  73.  
  74. # the horizontal line intersects the line between
  75. # known positions x and y => what is (?) ?
  76. # |:.
  77. # |  x.
  78. # |    :.
  79. # |------?---
  80. # |        y.
  81. # |__________:._        
  82. def interpolate(series, intersect):
  83.     value = 0
  84.     prev = series[0]
  85.     for i,e in enumerate(series):
  86.         if e<intersect and prev>=intersect:
  87.             value = i + (prev-intersect)/(prev-e)
  88.             break
  89.         prev = e
  90.     return value
Advertisement
Add Comment
Please, Sign In to add comment