original_docstring: Estimate bootstrap CIs for a statfunction that operates along the rows of
      a pandas.DataFrame and return a dict or pd.Series of results. Returning
      a dict is typically faster.
  
      This is about 10x slower than using scikits.bootstrap.ci for a statistic
      doesn't require resampling the whole DataFrame. However, if the statistic
      requires the whole DataFrame or you are computing many statistics on the
      same DataFrame that all require CIs, then this function may be efficient.
  
      Parameters
      ----------
      df : pd.DataFrame
          Data that will be passed to statfunction as a single parameter.
      statfunction : function
          Function that should operate along the rows of df and return a dict
      alpha : float [0, 1]
          Specify CI: [alpha/2, 1-alpha/2]
      n_samples : int
          Number of bootstrap samples.
      method : str
          Specify bias-corrected and accelerated ("bca") or percentile ("pi")
          bootstrap.
  
      Returns
      -------
      cis : pd.Series [est, lcl, ucl]
          Point-estimate and CI of statfunction of df
docstring: Estimate bootstrap CIs for a statfunction that operates along the rows of
  a pandas.DataFrame and return a dict or pd.Series of results. Returning
  a dict is typically faster.
  
  This is about 10x slower than using scikits.bootstrap.ci for a statistic
  doesn't require resampling the whole DataFrame. However, if the statistic
  requires the whole DataFrame or you are computing many statistics on the
  same DataFrame that all require CIs, then this function may be efficient.
  
  Parameters
  
  df : pd.DataFrame
  Data that will be passed to statfunction as a single parameter.
  statfunction : function
  Function that should operate along the rows of df and return a dict
  alpha : float [0, 1]
  Specify CI: [alpha/2, 1-alpha/2]
  n_samples : int
  Number of bootstrap samples.
  method : str
  Specify bias-corrected and accelerated ("bca") or percentile ("pi")
  bootstrap.
  
  Returns
  
  cis : pd.Series [est, lcl, ucl]
  Point-estimate and CI of statfunction of df
docstring_tokens: Estimate bootstrap CIs for a statfunction that operates along the rows of a pandas . DataFrame and return a dict or pd . Series of results . Returning a dict is typically faster . This is about 10x slower than using scikits . bootstrap . ci for a statistic doesn ' t require resampling the whole DataFrame . However if the statistic requires the whole DataFrame or you are computing many statistics on the same DataFrame that all require CIs then this function may be efficient . Parameters df : pd . DataFrame Data that will be passed to statfunction as a single parameter . statfunction : function Function that should operate along the rows of df and return a dict alpha : float [ 0 1 ] Specify CI : [ alpha / 2 1 - alpha / 2 ] n_samples : int Number of bootstrap samples . method : str Specify bias - corrected and accelerated ( " bca " ) or percentile ( " pi " ) bootstrap . Returns cis : pd . Series [ est lcl ucl ] Point - estimate and CI of statfunction of df
code: def bootci_pd(df, statfunction, alpha=0.05, n_samples=10000, method='bca'):
      """Estimate bootstrap CIs for a statfunction that operates along the rows of
      a pandas.DataFrame and return a dict or pd.Series of results. Returning
      a dict is typically faster.
  
      This is about 10x slower than using scikits.bootstrap.ci for a statistic
      doesn't require resampling the whole DataFrame. However, if the statistic
      requires the whole DataFrame or you are computing many statistics on the
      same DataFrame that all require CIs, then this function may be efficient.
  
      Parameters
      ----------
      df : pd.DataFrame
          Data that will be passed to statfunction as a single parameter.
      statfunction : function
          Function that should operate along the rows of df and return a dict
      alpha : float [0, 1]
          Specify CI: [alpha/2, 1-alpha/2]
      n_samples : int
          Number of bootstrap samples.
      method : str
          Specify bias-corrected and accelerated ("bca") or percentile ("pi")
          bootstrap.
  
      Returns
      -------
      cis : pd.Series [est, lcl, ucl]
          Point-estimate and CI of statfunction of df"""
      alphas = np.array([alpha/2, 1-alpha/2])
  
      # The value of the statistic function applied just to the actual data.
      res = pd.Series(statfunction(df))
      
      #st = time.time()
      boot_res = []
      for i in range(n_samples):
          # rind = np.random.randint(df.shape[0], size=df.shape[0])
          # boot_res.append(statfunction(df.iloc[rind]))
          boot_res.append(statfunction(df.sample(frac=1, replace=True)))
      boot_res = pd.DataFrame(boot_res)
      #print(time.time() - st)
  
      # Percentile Interval Method
      if method == 'pi':
          avals = np.tile(alphas, (boot_res.shape[1], 1)).T
      # Bias-Corrected Accelerated Method
      elif method == 'bca':
          ind = np.ones(df.shape[0], dtype=bool)
          jack_res = []
          for i in range(df.shape[0]):
              ind[i] = False
              jack_res.append(statfunction(df.loc[ind]))
              ind[i] = True
  
          jack_res = pd.DataFrame(jack_res)
          jmean = np.nanmean(jack_res, keepdims=True, axis=0)
          bca_accel = np.nansum((jmean - jack_res.values)**3, axis=0) / (6.0 * np.nansum((jmean - jack_res.values)**2, axis=0)**1.5)
  
          """The bias correction value"""
          z0 = stats.distributions.norm.ppf( (np.sum(boot_res.values < res.values[None, :], axis=0)) / np.sum(~np.isnan(boot_res.values), axis=0) )
          zs = z0 + stats.distributions.norm.ppf(alphas).reshape(alphas.shape + (1,) * z0.ndim)
          avals = stats.distributions.norm.cdf(z0 + zs / (1 - bca_accel * zs))
  
      non_nan_ind = ~np.isnan(boot_res)
      nvals = np.round((np.sum(non_nan_ind.values, axis=0) - 1) * avals).astype(int)
      nvals = pd.DataFrame(nvals.T, columns=['%1.3f' % a for a in alphas], index=boot_res.columns)
  
      if np.any(np.isnan(nvals)):
          print('Nan values for some stats suggest there is no bootstrap variation.')
          print(res.head(10))
      
      cis = pd.DataFrame(np.zeros((len(boot_res.columns), len(avals) + 1)), index=boot_res.columns, columns=['est'] + ['%1.3f' % a for a in alphas])
      for i,col in enumerate(boot_res.columns):
          boot_res.values[:, i].sort()
          cis.loc[col, 'est'] = res[col]
          cis.loc[col, ['%1.3f' % a for a in alphas]] = boot_res[col].values[nvals.loc[col]]
  
      if np.any(nvals < 10) or np.any(nvals > n_samples-10):
          print('Extreme samples used: results unstable')
          print(nvals)
  
      return cis
code_tokens: def bootci_pd ( df , statfunction , alpha = 0.05 , n_samples = 10000 , method = 'bca' ) : alphas = np . array ( [ alpha / 2 , 1 - alpha / 2 ] ) res = pd . Series ( statfunction ( df ) ) boot_res = [ ] for i in range ( n_samples ) : boot_res . append ( statfunction ( df . sample ( frac = 1 , replace = True ) ) ) boot_res = pd . DataFrame ( boot_res ) if method == 'pi' : avals = np . tile ( alphas , ( boot_res . shape [ 1 ] , 1 ) ) . T elif method == 'bca' : ind = np . ones ( df . shape [ 0 ] , dtype = bool ) jack_res = [ ] for i in range ( df . shape [ 0 ] ) : ind [ i ] = False jack_res . append ( statfunction ( df . loc [ ind ] ) ) ind [ i ] = True jack_res = pd . DataFrame ( jack_res ) jmean = np . nanmean ( jack_res , keepdims = True , axis = 0 ) bca_accel = np . nansum ( ( jmean - jack_res . values ) ** 3 , axis = 0 ) / ( 6.0 * np . nansum ( ( jmean - jack_res . values ) ** 2 , axis = 0 ) ** 1.5 ) """The bias correction value""" z0 = stats . distributions . norm . ppf ( ( np . sum ( boot_res . values < res . values [ None , : ] , axis = 0 ) ) / np . sum ( ~ np . isnan ( boot_res . values ) , axis = 0 ) ) zs = z0 + stats . distributions . norm . ppf ( alphas ) . reshape ( alphas . shape + ( 1 , ) * z0 . ndim ) avals = stats . distributions . norm . cdf ( z0 + zs / ( 1 - bca_accel * zs ) ) non_nan_ind = ~ np . isnan ( boot_res ) nvals = np . round ( ( np . sum ( non_nan_ind . values , axis = 0 ) - 1 ) * avals ) . astype ( int ) nvals = pd . DataFrame ( nvals . T , columns = [ '%1.3f' % a for a in alphas ] , index = boot_res . columns ) if np . any ( np . isnan ( nvals ) ) : print ( 'Nan values for some stats suggest there is no bootstrap variation.' ) print ( res . head ( 10 ) ) cis = pd . DataFrame ( np . zeros ( ( len ( boot_res . columns ) , len ( avals ) + 1 ) ) , index = boot_res . columns , columns = [ 'est' ] + [ '%1.3f' % a for a in alphas ] ) for i , col in enumerate ( boot_res . columns ) : boot_res . values [ : , i ] . sort ( ) cis . loc [ col , 'est' ] = res [ col ] cis . loc [ col , [ '%1.3f' % a for a in alphas ] ] = boot_res [ col ] . values [ nvals . loc [ col ] ] if np . any ( nvals < 10 ) or np . any ( nvals > n_samples - 10 ) : print ( 'Extreme samples used: results unstable' ) print ( nvals ) return cis
short_docstring: Estimate bootstrap CIs for a statfunction that operates along the rows of
  a pandas.DataFrame and return a dict or pd.Series of results.
short_docstring_tokens: Estimate bootstrap CIs for a statfunction that operates along the rows of a pandas . DataFrame and return a dict or pd . Series of results .
comment: """Estimate bootstrap CIs for a statfunction that operates along the rows of
      a pandas.DataFrame and return a dict or pd.Series of results. Returning
      a dict is typically faster.
  
      This is about 10x slower than using scikits.bootstrap.ci for a statistic
      doesn't require resampling the whole DataFrame. However, if the statistic
      requires the whole DataFrame or you are computing many statistics on the
      same DataFrame that all require CIs, then this function may be efficient.
  
      Parameters
      ----------
      df : pd.DataFrame
          Data that will be passed to statfunction as a single parameter.
      statfunction : function
          Function that should operate along the rows of df and return a dict
      alpha : float [0, 1]
          Specify CI: [alpha/2, 1-alpha/2]
      n_samples : int
          Number of bootstrap samples.
      method : str
          Specify bias-corrected and accelerated ("bca") or percentile ("pi")
          bootstrap.
  
      Returns
      -------
      cis : pd.Series [est, lcl, ucl]
          Point-estimate and CI of statfunction of df"""$SEP$# The value of the statistic function applied just to the actual data.$SEP$#st = time.time()$SEP$# rind = np.random.randint(df.shape[0], size=df.shape[0])$SEP$# boot_res.append(statfunction(df.iloc[rind]))$SEP$#print(time.time() - st)$SEP$# Percentile Interval Method$SEP$# Bias-Corrected Accelerated Method$SEP$"""The bias correction value"""