

from numpy import exp
from scipy.stats import rv_continuous
from scipy.special import gamma


class grw_gen(rv_continuous):
    """
    Generalized Reverse Weibull distribution.

    PDF:
        
        a/gamma(g) * x^(a*g-1) * exp(-x^a)

    for x,a,g >= 0

    """

    def _pdf(self,x,a,g):
        return a/gamma(g) * pow(x,a*g-1) * exp(-pow(x,a))

    def _fitstart(self,data):
        return (2.0,1.0,0.0,0.02)

grw = grw_gen(a=0.0, name='grw', shapes='a,g')



import numpy as np
from scipy.stats import gengamma, norm


"""
Set default starting parameters for fitting a generalized gamma distribution.

These parameters are sensible for ATLAS v_n distributions.

Order:  (a, c, loc, scale)  where a,c are shape params.
"""
gengamma._fitstart = lambda data: (1.0, 2.0, 0.0, 0.1)


def fit_file(fname,dist='gengamma',**kwargs):
    """
    Fit a distribution to each column of a data file.

    Arguments
    ---------
    fname -- file name or object containing data columns to fit
    dist -- distribution to fit, either 'gengamma' (default) or 'norm'
    kwargs -- for np.loadtxt, except 'unpack' or 'ndmin' are ignored

    Returns
    -------
    iterable of MLE parameters:

        params_0, ... , params_N

    for each column, where params are tuples of the form

        (*shapes, loc, scale)

    as produced by scipy.stats.rv_continuous.fit

    """

    # remove 'unpack' and 'ndmin' kwargs if set
    for key in ['unpack','ndmin']:
        try:
            del kwargs[key]
        except KeyError:
            pass

    # read file
    cols = np.loadtxt(fname,unpack=True,ndmin=2,**kwargs)

    # set fitting distribution
    try:
        dist = eval(dist)
    except NameError:
        raise ValueError('invalid distribution: ' + dist)

    return (dist.fit(c) for c in cols)

Replace custom GRW dist with scipy gengamma. Implement file fitting function.
Replace custom GRW dist with scipy gengamma.  Implement file fitting function.

  
  
+ import numpy as np
- from numpy import exp
- from scipy.stats import rv_continuous
- from scipy.special import gamma
+ from scipy.stats import gengamma, norm
  
  
- class grw_gen(rv_continuous):
+ """
+ Set default starting parameters for fitting a generalized gamma distribution.
+ 
+ These parameters are sensible for ATLAS v_n distributions.
+ 
+ Order:  (a, c, loc, scale)  where a,c are shape params.
+ """
+ gengamma._fitstart = lambda data: (1.0, 2.0, 0.0, 0.1)
+ 
+ 
+ def fit_file(fname,dist='gengamma',**kwargs):
      """
-     Generalized Reverse Weibull distribution.
+     Fit a distribution to each column of a data file.
  
-     PDF:
-         
-         a/gamma(g) * x^(a*g-1) * exp(-x^a)
+     Arguments
+     ---------
+     fname -- file name or object containing data columns to fit
+     dist -- distribution to fit, either 'gengamma' (default) or 'norm'
+     kwargs -- for np.loadtxt, except 'unpack' or 'ndmin' are ignored
  
-     for x,a,g >= 0
+     Returns
+     -------
+     iterable of MLE parameters:
+ 
+         params_0, ... , params_N
+ 
+     for each column, where params are tuples of the form
+ 
+         (*shapes, loc, scale)
+ 
+     as produced by scipy.stats.rv_continuous.fit
  
      """
  
-     def _pdf(self,x,a,g):
-         return a/gamma(g) * pow(x,a*g-1) * exp(-pow(x,a))
+     # remove 'unpack' and 'ndmin' kwargs if set
+     for key in ['unpack','ndmin']:
+         try:
+             del kwargs[key]
+         except KeyError:
+             pass
  
-     def _fitstart(self,data):
-         return (2.0,1.0,0.0,0.02)
+     # read file
+     cols = np.loadtxt(fname,unpack=True,ndmin=2,**kwargs)
  
- grw = grw_gen(a=0.0, name='grw', shapes='a,g')
+     # set fitting distribution
+     try:
+         dist = eval(dist)
+     except NameError:
+         raise ValueError('invalid distribution: ' + dist)
  
+     return (dist.fit(c) for c in cols)
+ 
Replace custom GRW dist with scipy gengamma. Implement file fitting function.
## Code Before:


from numpy import exp
from scipy.stats import rv_continuous
from scipy.special import gamma


class grw_gen(rv_continuous):
    """
    Generalized Reverse Weibull distribution.

    PDF:
        
        a/gamma(g) * x^(a*g-1) * exp(-x^a)

    for x,a,g >= 0

    """

    def _pdf(self,x,a,g):
        return a/gamma(g) * pow(x,a*g-1) * exp(-pow(x,a))

    def _fitstart(self,data):
        return (2.0,1.0,0.0,0.02)

grw = grw_gen(a=0.0, name='grw', shapes='a,g')

## Instruction:
Replace custom GRW dist with scipy gengamma. Implement file fitting function.
## Code After:


import numpy as np
from scipy.stats import gengamma, norm


"""
Set default starting parameters for fitting a generalized gamma distribution.

These parameters are sensible for ATLAS v_n distributions.

Order:  (a, c, loc, scale)  where a,c are shape params.
"""
gengamma._fitstart = lambda data: (1.0, 2.0, 0.0, 0.1)


def fit_file(fname,dist='gengamma',**kwargs):
    """
    Fit a distribution to each column of a data file.

    Arguments
    ---------
    fname -- file name or object containing data columns to fit
    dist -- distribution to fit, either 'gengamma' (default) or 'norm'
    kwargs -- for np.loadtxt, except 'unpack' or 'ndmin' are ignored

    Returns
    -------
    iterable of MLE parameters:

        params_0, ... , params_N

    for each column, where params are tuples of the form

        (*shapes, loc, scale)

    as produced by scipy.stats.rv_continuous.fit

    """

    # remove 'unpack' and 'ndmin' kwargs if set
    for key in ['unpack','ndmin']:
        try:
            del kwargs[key]
        except KeyError:
            pass

    # read file
    cols = np.loadtxt(fname,unpack=True,ndmin=2,**kwargs)

    # set fitting distribution
    try:
        dist = eval(dist)
    except NameError:
        raise ValueError('invalid distribution: ' + dist)

    return (dist.fit(c) for c in cols)
