活跃农民
- 积分
- 610
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-12-12
- 最后登录
- 1970-1-1
|
这是我上udacity上的课的一段代码
这个函数不改参数的情况下返回的是你在半中间看一下如果满足p<5%就停止实验前后两个block的p value 以及 整个实验总体的p value
def peeking_sim(alpha = .05, p = .5, n_trials = 1000, n_blocks = 2, n_sims = 10000): """. 1point3acres.com
This function simulates the rate of Type I errors made if an early
stopping decision is made based on a significant result when peeking ahead.
Input parameters:
alpha: Supposed Type I error rate
p: Probability of individual trial success. Waral dи,
n_trials: Number of trials in a full experiment
n_blocks: Number of times data is looked at (including end)
n_sims: Number of simulated experiments run
Return:
p_sig_any: Proportion of simulations significant at any check point,
p_sig_each: Proportion of simulations significant at each check point
"""
trials_per_block = np.ceil(n_trials / n_blocks).astype(int)
data = np.random.binomial(trials_per_block, p, n_sims * n_blocks).reshape(n_sims, n_blocks)
. 1point 3 acres
# standardize data
data_cumsum = data.cumsum(axis = 1)
block_sizes = trials_per_block * np.arange(1, n_blocks+1, 1)
block_means = block_sizes * p
block_sds = np.sqrt(block_sizes * p * (1-p))
data_zscores =(data_cumsum - block_means)/block_sds
. 1point 3acres
# test outcomes
z_crit = stats.norm.ppf(1-alpha/2)
sig_flags = abs(data_zscores) > z_crit
p_sig_any = (sig_flags.sum(axis = 1)>=1).mean()
p_sig_each = sig_flags.mean(axis = 0)
. Χ
return (p_sig_any, p_sig_each)
这段代码是求 corrected alpha:
def peeking_correction(alpha = .05, p = .5, n_trials = 1000, n_blocks = 2, n_sims = 10000):. Waral dи,
"""-baidu 1point3acres
This function uses simulations to estimate the individual error rate necessary
to limit the Type I error rate, if an early stopping decision is made based on
a significant result when peeking ahead.
. 1point3acres.com
Input parameters:
alpha: Desired overall Type I error rate
p: Probability of individual trial success
n_trials: Number of trials in a full experiment.--
n_blocks: Number of times data is looked at (including end)
n_sims: Number of simulated experiments run
.1point3acres
Return:
alpha_ind: Individual error rate required to achieve overall error rate
"""
# generate data
trials_per_block = np.ceil(n_trials / n_blocks).astype(int)
data = np.random.binomial(trials_per_block, p, [n_sims, n_blocks])
# standardize data
data_cumsum = np.cumsum(data, axis = 1)
block_sizes = trials_per_block * np.arange(1, n_blocks+1, 1)
block_means = block_sizes * p.--
block_sds = np.sqrt(block_sizes * p * (1-p))
data_zscores = (data_cumsum - block_means) / block_sds.--
# find necessary individual error rate
max_zscores = np.abs(data_zscores).max(axis = 1)
z_crit_ind = np.percentile(max_zscores, 100 * (1 - alpha))
alpha_ind = 2 * (1 - stats.norm.cdf(z_crit_ind))
. Waral dи,
return alpha_ind
|
|