Weights

draw_weights(t, full_enumeration, N_G_bootcluster, boot_iter, rng)

draw bootstrap weights

Parameters:

Name Type Description Default
t str | callable

the type of the weights distribution. Either 'rademacher', 'mammen', 'norm' or 'webb'

required
full_enumeration bool

should deterministic full enumeration be employed

required
N_G_bootcluster int

the number of bootstrap clusters

required
boot_iter int

the number of bootstrap iterations

required

Returns:

Type Description
Tuple[np.ndarray, int]

Tuple[np.ndarray, int]: a matrix of dimension N_G_bootcluster x (boot_iter + 1) and the number of iterations

Source code in wildboottest\weights.py
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def draw_weights(t : Union[str, Callable], full_enumeration: bool, 
                 N_G_bootcluster: int, boot_iter: int,
                 rng: np.random.Generator) -> Tuple[np.ndarray, int]:
    """draw bootstrap weights
    Args:
        t (str|callable): the type of the weights distribution. Either 'rademacher', 'mammen', 'norm' or 'webb'
        If `t` is a callable, must be a function of one variable, `n`, and return a vector of size `n`
        full_enumeration (bool): should deterministic full enumeration be employed
        N_G_bootcluster (int): the number of bootstrap clusters
        boot_iter (int): the number of bootstrap iterations
    Returns:
        Tuple[np.ndarray, int]: a matrix of dimension N_G_bootcluster x (boot_iter + 1) and the number of iterations
    """    

    #TODO: we can use the `case` feature in python, but that's only available in 3.10+ will do a 3.7 version for now
    # Will take out this and make separate functions for readability

    if isinstance(t, str):
        wild_draw_fun = wild_draw_fun_dict.get(t)
        if wild_draw_fun is None:
            raise WildDrawFunctionException("Function type specified is not supported or there is a typo.")
    elif callable(t):
        wild_draw_fun = t
    elif t is None:
        raise WildDrawFunctionException("`t` must be specified")
    else:
        raise ValueError(f"t can be string or callable, but got {type(t)}")
    # do full enumeration for rademacher weights if bootstrap iterations
    # B exceed number of possible permutations else random sampling

    # full_enumeration only for rademacher weights (set earlier)
    if full_enumeration: 
        t = 0 # what is this needed for? 
        # with N_G_bootcluster draws, get all combinations of [-1,1] WITH 
        # replacement, in matrix form
        v0 = np.transpose(np.array(list(product([-1,1], repeat=N_G_bootcluster))))
    else:
        # else: just draw with replacement - by chance, some permutations
        # might occur more than once
        v0 = wild_draw_fun(n = N_G_bootcluster * boot_iter, rng=rng)
        v0 = v0.reshape(N_G_bootcluster, boot_iter) # weights matrix

    # update boot_iter (B) - only relevant in enumeration case
    boot_iter = v0.shape[1] 
    #v = np.insert(v0, 0, 1,axis = 1)

    return v0, boot_iter