Skip to content

Ch05 lab bug produces incorrect bootstrap estimate SE(B_0) #79

Description

@johnlevidy

"def boot_SE(func,\n",

This function as written has a bug. Non positional indexing on data frames will result in behavior different than positional sampling. When I run the notebook as is, I get

intercept 0.717654 horsepower 0.006041

To fix this, we can just write:

def boot_SE_fixed(func, D, n=None, B=1000, seed=0):
    rng = np.random.default_rng(seed)
    n = len(D) if n is None else n

    first_, second_ = 0, 0

    for _ in range(B):
        positions = rng.choice(
            len(D),
            size=n,
            replace=True,
        )
        value = func(D, positions)
        first_ += value
        second_ += value**2

    return np.sqrt(second_ / B - (first_ / B)**2)

( here we rng.choice on the length of D )

And

def boot_OLS_fixed(model_matrix, response, D, idx):
    D_ = D.iloc[idx]
    Y_ = D_[response]
    X_ = clone(model_matrix).fit_transform(D_)
    return sm.OLS(Y_, X_).fit().params

here we use iloc instead of loc.

hp_func_fixed = partial(boot_OLS_fixed, MS(['horsepower']), 'mpg')

Finally, we correctly wrap it and:

hp_se = boot_SE_fixed(hp_func_fixed,
                Auto,
                B=1000,
                seed=0)
hp_se

produces the desired results, making it consistent with the markdown below.

Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions