Replace mutables used as defaults in functions with placeholder defaults

Bug #562874 reported by Mike Crowe
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
statsmodels
Fix Committed
Undecided
Unassigned

Bug Description

There are places where we have got class constructor calls in the
function defaults,

e.g.

    def __init__(self, endog, exog, family=family.Gaussian()):

The Gaussian constructor will be called when the module is imported, not when the function is called, therefore all calls to __init__ that use the default will be sharing the same instance of the default object.

If we want the default evaluated when it called, then you need
something like:

    def __init__(self, endog, exog, family=None):
        if family is None:
             family = family.Gaussian()

or

    def __init__(self, endog, exog, family=family.Default()):
        if family = family.Default():
              family = family.Gaussian()

Revision history for this message
Skipper Seabold (jsseabold) wrote :

What about something like this, I don't know if it seems more or less intuitive

class Gaussian(object):
    def __init__(self, **kwargs):
        self.link = kwargs.get('link', 'default')
        self.alpha = kwargs.get('alpha', 'default')

class GLM(object):
    def __init__(self, family=Gaussian, family_kwargs={}):
        self.family = family(**family_kwargs)

A = GLM(family_kwargs=dict(link = 10, alpha = 25))
print A.family.link
A.family.link = 50
print A.family.link
print A.family.alpha
B = GLM()
print B.family.link
print B.family.alpha

Changed in statsmodels:
status: New → Fix Committed
Revision history for this message
Skipper Seabold (jsseabold) wrote :

It doesn't look like replacing with None fixes the problem.

Changed in statsmodels:
status: Fix Committed → In Progress
Revision history for this message
Skipper Seabold (jsseabold) wrote :

Fix in my branch. I changed all of the aliased link functions to subclasses, so the names are the same. Now the families have

self.link = link()

instead of

self.link = link

and all seems to be well.

Changed in statsmodels:
status: In Progress → Fix Committed
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.