code: def perform_null_firth_fit(
      y: NDArray[(Any,), Float],
      C: NDArray[(Any, Any), Float],
      mask: NDArray[(Any,), bool],
      offset: Optional[
          Any
      ],  # Typeguard doesn't work with optional NDArrays
      includes_intercept: bool,
  ) -> NDArray[(Any,), Float]:
      """
      Performs the null fit for approximate Firth in order to calculate the covariate effects to be
      used as an offset during the SNP fits.
  
      Args:
          y : Dependent variable (phenotype)
          C : Covariate matrix
          mask : Missingness array; false if the sample is missing a phenotype value, true otherwise
          offset : Phenotype offset
          includes_intercept : True if the first column of the covariate matrix C represents an intercept term
  
      Returns:
          None if the Firth fit did not converge.
          Otherwise, offset vector with per-sample covariate effects for SNP fits.
      """
  
      firth_offset = np.zeros(y.shape)
      if offset is None:
          offset = np.zeros(y.shape)
  
      masked_y = y[mask]
      masked_C = C[mask, :]
      masked_offset = offset[mask]
  
      b0_null_fit = np.zeros(C.shape[1])
      if includes_intercept:
          b0_null_fit[0] = (0.5 + masked_y.sum()) / (mask.sum() + 1)
          b0_null_fit[0] = (
              np.log(b0_null_fit[0] / (1 - b0_null_fit[0]))
              - masked_offset.mean()
          )
      firth_fit_result = _fit_firth(
          b0_null_fit, masked_C, masked_y, masked_offset
      )
      if firth_fit_result is None:
          raise ValueError("Null fit failed!")
      firth_offset[mask] = (
          masked_offset + masked_C @ firth_fit_result.beta
      )
  
      return firth_offset
  
file_path: glow/python/glow/gwas/approx_firth.py
imports: from typing import Any, Optional
  import numpy
  from dataclasses import dataclass
  import scipy
  from typeguard import typechecked
  from nptyping import Float, NDArray
  from scipy import stats
label_desc: Use image node_id 3 for calling a global function with example usage: perform_null_firth_fit(y, C, mask, offset, includes_intercept) and returns: firth_offset