code: def _nc2_quantile_numeric(p, df, noncentrality, tol):
      """Numerically inverts the CDF of the NC2 distribution.
  
      Uses the noncentral Wilson-Hilferty approximation from [1] (in the paper
      referred to as 'first approx') to guess the initial location for the
      root search.
  
      References
      [1] Abdel-Aty, S. H. (1954). Approximate formulae for the percentage points
          and the probability integral of the non-central χ2 distribution.
          Biometrika, 41(3/4), 538-540.
  
      Args:
        p: `tf.Tensor` of locations at which to invert the CDF.
        df: `tf.Tensor` of the degrees of freedom of the NC2 variate.
        noncentrality: `tf.Tensor` of the noncentrality of the NC2 variate.
        tol: `float` tolerance for the root search algorithm
  
      Returns:
      `tf.Tensor` of quantiles corresponding to the CDF locations.
      """
      # Ensure that pathological values don't affect the numerical root search
  
      numpy_dtype = dtype_util.as_numpy_dtype(p.dtype)
  
      p_zero_mask = tf.math.equal(p, 0.0)
      p_one_mask = tf.math.equal(p, 1.0)
      p_invalid_mask = tf.math.less(p, 0.0) & tf.math.greater(p, 1.0)
  
      safe_p = tf.where(
          p_zero_mask | p_one_mask | p_invalid_mask,
          0.5 * tf.ones_like(p),
          p,
      )
  
      df_plus_nc = df + noncentrality
      wilson_hilferty_coef = 2.0 / 9.0 * (df_plus_nc + noncentrality)
      wilson_hilferty_coef = wilson_hilferty_coef / tf.math.square(
          df_plus_nc
      )
  
      quant_approx = tf.math.ndtri(safe_p) * tf.math.sqrt(
          wilson_hilferty_coef
      )
      quant_approx = quant_approx + (1.0 - wilson_hilferty_coef)
      quant_approx = df_plus_nc * quant_approx**3.0
      quant_approx = (
          tf.nn.relu(quant_approx) + 1e-2
      )  # Ensure approx is positive
  
      def objective_fn(ux):
          return (
              _nc2_cdf_naive(tf.nn.softplus(ux), df, noncentrality)
              - safe_p
          )
  
      unconstrained_numeric_root_search_results = (
          root_search.find_root_secant(
              objective_fn=objective_fn,
              initial_position=generic.softplus_inverse(quant_approx),
              position_tolerance=tol,
              value_tolerance=tol,
          )
      )
  
      numeric_root = tf.nn.softplus(
          unconstrained_numeric_root_search_results.estimated_root
      )
  
      numeric_root = tf.where(
          p_zero_mask, tf.zeros_like(numeric_root), numeric_root
      )
      numeric_root = tf.where(
          p_one_mask, numpy_dtype(np.inf), numeric_root
      )
      numeric_root = tf.where(
          p_invalid_mask, numpy_dtype(np.nan), numeric_root
      )
  
      return numeric_root
  
imports: import numpy
  import tensorflow.compat.v2
  from tensorflow_probability.python.bijectors import softplus
  from tensorflow_probability.python.distributions import chi2
  from tensorflow_probability.python.distributions import distribution
  from tensorflow_probability.python.distributions import gamma
  from tensorflow_probability.python.distributions import poisson
  from tensorflow_probability.python.internal import assert_util
  from tensorflow_probability.python.internal import custom_gradient
  from tensorflow_probability.python.internal import distribution_util
  from tensorflow_probability.python.internal import dtype_util
  from tensorflow_probability.python.internal import parameter_properties
  from tensorflow_probability.python.internal import prefer_static
  from tensorflow_probability.python.internal import reparameterization
  from tensorflow_probability.python.internal import samplers
  from tensorflow_probability.python.internal import special_math
  from tensorflow_probability.python.internal import tensor_util
  from tensorflow_probability.python.math import bessel
  from tensorflow_probability.python.math import generic
  from tensorflow_probability.python.math import root_search
  import tensorflow_probability.python.random
label_desc: Use image node_id 8 for calling a global function with example usage: _nc2_quantile_numeric(p, df, noncentrality, tol) and returns: numeric_root, unknown