code: def fit_pval_model(
      quantiles: pd.DataFrame | pd.Series,
  ) -> PvalueResult:
      percentiles = quantiles.index.to_numpy()
      lhs = stats.norm.ppf(percentiles)
      data = np.asarray(quantiles)
      avg_test_stats = data.mean(1)
      avg_test_std = data.std(1)
      avg_test_stats = avg_test_stats[:, None]
  
      rhs = avg_test_stats ** np.arange(4)
      rhs_large = rhs
      lhs_large = lhs
      res_large = WLS(lhs_large, rhs, weights=1.0 / avg_test_std).fit()
      large_p = res_large.params.tolist()
  
      # Compute tau_max, by finding the func maximum
      p = res_large.params
      poly_roots = np.roots(np.array([3, 2, 1.0]) * p[:0:-1])
      if np.isreal(poly_roots[0]):
          tau_max = float(np.squeeze(np.real(np.max(poly_roots))))
      else:
          tau_max = np.inf
  
      # Small p regression using only p<=15%
      cutoff = np.where(percentiles <= 0.150)[0]
      avg_test_stats = avg_test_stats[cutoff]
      avg_test_std = avg_test_std[cutoff]
      lhs_small = lhs[cutoff]
      rhs = avg_test_stats ** np.arange(3)
      res_small = WLS(lhs_small, rhs, weights=1.0 / avg_test_std).fit()
      small_p = res_small.params.tolist()
  
      # Compute tau star
      err_large = lhs_large - rhs_large.dot(res_large.params)
      # Missing 1 parameter here, replace with 0
      params = np.append(res_small.params, 0.0)
      err_small = lhs_large - rhs_large.dot(params)
      # Find the location that minimizes the total absolute error
      m = lhs_large.shape[0]
      abs_err = np.zeros((m, 1))
      for j in range(m):
          abs_err[j] = (
              np.abs(err_large[:j]).sum() + np.abs(err_small[j:]).sum()
          )
      loc = np.argmin(abs_err)
      tau_star = rhs_large[loc, 1]
      # Compute tau min
      tau_min = -params[1] / (2 * params[2])
      large_p = [round(val, 5) for val in large_p]
      small_p = [round(val, 5) for val in small_p]
      tau_max = round(tau_max, 5)
      tau_star = round(tau_star, 5)
      tau_min = round(tau_min, 5)
      return PvalueResult(large_p, small_p, tau_max, tau_star, tau_min)
  
imports: from __future__ import annotations
  from collections import defaultdict
  import glob
  from itertools import product
  import os
  from typing import NamedTuple, cast
  from black import FileMode, TargetVersion, format_file_contents
  import matplotlib.backends.backend_pdf
  import matplotlib.pyplot
  import numpy
  import pandas
  from phillips_ouliaris import FILE_TYPES, ROOT, TRENDS
  from scipy import stats
  import seaborn
  from shared import format_dict
  from statsmodels.regression.linear_model import OLS, WLS
label_desc: Use image node_id 3 for calling a global function with example usage: fit_pval_model(quantiles) and returns: PvalueResult