Advertisement
korenizla

bootstrap

Nov 4th, 2022
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3.  
  4. samples_A = pd.Series([
  5.      98.24,  97.77,  95.56,  99.49, 101.4 , 105.35,  95.83,  93.02,
  6.     101.37,  95.66,  98.34, 100.75, 104.93,  97.  ,  95.46, 100.03,
  7.     102.34,  98.23,  97.05,  97.76,  98.63,  98.82,  99.51,  99.31,
  8.      98.58,  96.84,  93.71, 101.38, 100.6 , 103.68, 104.78, 101.51,
  9.     100.89, 102.27,  99.87,  94.83,  95.95, 105.2 ,  97.  ,  95.54,
  10.      98.38,  99.81, 103.34, 101.14, 102.19,  94.77,  94.74,  99.56,
  11.     102.  , 100.95, 102.19, 103.75, 103.65,  95.07, 103.53, 100.42,
  12.      98.09,  94.86, 101.47, 103.07, 100.15, 100.32, 100.89, 101.23,
  13.      95.95, 103.69, 100.09,  96.28,  96.11,  97.63,  99.45, 100.81,
  14.     102.18,  94.92,  98.89, 101.48, 101.29,  94.43, 101.55,  95.85,
  15.     100.16,  97.49, 105.17, 104.83, 101.9 , 100.56, 104.91,  94.17,
  16.     103.48, 100.55, 102.66, 100.62,  96.93, 102.67, 101.27,  98.56,
  17.     102.41, 100.69,  99.67, 100.99])
  18.  
  19. samples_B = pd.Series([
  20.     101.67, 102.27,  97.01, 103.46, 100.76, 101.19,  99.11,  97.59,
  21.     101.01, 101.45,  94.8 , 101.55,  96.38,  99.03, 102.83,  97.32,
  22.      98.25,  97.17, 101.1 , 102.57, 104.59, 105.63,  98.93, 103.87,
  23.      98.48, 101.14, 102.24,  98.55, 105.61, 100.06,  99.  , 102.53,
  24.     101.56, 102.68, 103.26,  96.62,  99.48, 107.6 ,  99.87, 103.58,
  25.     105.05, 105.69,  94.52,  99.51,  99.81,  99.44,  97.35, 102.97,
  26.      99.77,  99.59, 102.12, 104.29,  98.31,  98.83,  96.83,  99.2 ,
  27.      97.88, 102.34, 102.04,  99.88,  99.69, 103.43, 100.71,  92.71,
  28.      99.99,  99.39,  99.19,  99.29, 100.34, 101.08, 100.29,  93.83,
  29.     103.63,  98.88, 105.36, 101.82, 100.86, 100.75,  99.4 ,  95.37,
  30.     107.96,  97.69, 102.17,  99.41,  98.97,  97.96,  98.31,  97.09,
  31.     103.92, 100.98, 102.76,  98.24,  97.  ,  98.99, 103.54,  99.72,
  32.     101.62, 100.62, 102.79, 104.19])
  33.  
  34. AB_difference = abs(samples_A.mean() - samples_B.mean())
  35. print("Разность средних чеков:", AB_difference)
  36.  
  37. alpha = 0.05
  38.    
  39. state = np.random.RandomState(12345)
  40.  
  41. bootstrap_samples = 1000
  42. count = 0
  43. for i in range(bootstrap_samples):
  44.     united_samples = pd.concat([samples_A, samples_B])
  45.     subsample = united_samples.sample(frac=1, replace=True, random_state=state)
  46.     subsample_A = subsample[:len(samples_A)]
  47.     subsample_B = subsample[len(samples_A):]
  48.     bootstrap_difference = abs(subsample_A.mean() - subsample_B.mean())
  49.     if bootstrap_difference >= AB_difference:
  50.         count += 1
  51.  
  52. pvalue = 1. * count / bootstrap_samples
  53. print('p-value =', pvalue)
  54.  
  55. if pvalue < alpha:
  56.     print("Отвергаем нулевую гипотезу: скорее всего, средний чек увеличился")
  57. else:
  58.     print("Не получилось отвергнуть нулевую гипотезу: скорее всего, средний чек не увеличился")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement