Compute Wald z-statistics and p-values for logistic regression coefficients
Role: Data Scientist
Category: Machine Learning
Difficulty: easy
Interview Round: Other
Implement `wald_z_and_pvalues(X, y)`. Fit a logistic regression with an added intercept, then compute the Wald z-statistic and two-sided p-value for every coefficient by hand, using the standard normal as the reference distribution (not a t distribution). The results must match `res.tvalues` and `res.pvalues` from statsmodels.
The function must:
- Fit `sm.Logit(y, sm.add_constant(X)).fit(method='newton', maxiter=200, tol=1e-10, disp=0)`.
- Compute `z = res.params / res.bse`.
- Compute `p = 2 * scipy.stats.norm.sf(np.abs(z))` itself. Do not read `res.pvalues`.
- Return a 2-tuple `(z, p)` of `np.float64` arrays, each of shape `(d + 1,)`, with the intercept first.
```hint Tail precision
The two-sided p-value only needs the upper tail of the normal at `|z|`. Think about which scipy function gives that tail without subtracting a number close to 1 from 1.
```
### Constraints & Assumptions
- `X` is a 2-D float array of shape `(n, d)` with no intercept column, and `y` is a 1-D array of 0/1 integers.
- The grader uses one fixed, non-separable dataset with n = 60 and d = 2. It checks both outputs against `res.tvalues` and `res.pvalues` with `assert_allclose(rtol=1e-6)` and checks that each has shape `(3,)`.
### What a Strong Answer Covers
- Adding the intercept as the first column and fitting with the given Newton settings, so the coefficients and standard errors are the ones the grader compares against.
- The elementwise Wald statistic for all d + 1 coefficients and the two-sided p-value computed from it.
- Why a logistic regression MLE uses a normal reference, and what a t reference would change.
- A numerically safe way to compute small tail probabilities.
- Where the standard errors come from and when the Wald test stops being reliable.
### Follow-up Questions
- When would a Wald test mislead you, and what alternative would you use?
- Where does `res.bse` come from mathematically?
Overview: Implement a function that fits a logistic regression with statsmodels and computes the Wald z-statistic and two-sided p-value for each coefficient by hand. It tests whether you know where the standard errors come from and which reference distribution applies to a logistic MLE.