compute_P1_phase <- function(
    b, S_y, C_y, Wbar, Zmat, t, estWeight, h = 1e-6
) {
  p_tot <- length(b)
  P <- matrix(0, p_tot, p_tot)

  base <- PhaseGradient(
    b, S_y, C_y, cbind(Wbar, Zmat), t, estWeight
  )

  for (j in seq_len(p_tot)) {
    b_up   <- b
    b_down <- b

    b_up[j]   <- b_up[j] + h
    b_down[j] <- b_down[j] - h

    g_up <- PhaseGradient(
      b_up, S_y, C_y, cbind(Wbar, Zmat), t, estWeight
    )

    g_down <- PhaseGradient(
      b_down, S_y, C_y, cbind(Wbar, Zmat), t, estWeight
    )

    P[, j] <- (g_up - g_down) / (2 * h)
  }

  P
}
