Skip to content

ENH: Generalized Additive Models and splines (Gam 2744 rebased4) - #5481

Merged
josef-pkt merged 68 commits into
statsmodels:masterfrom
josef-pkt:gam_2744_rebased4
Jan 29, 2019
Merged

josef-pkt merged 68 commits into
statsmodels:masterfrom
josef-pkt:gam_2744_rebased4

Conversation

@josef-pkt

Copy link
Copy Markdown
Member

the last rebase PR

rebased version of #5370 with renaming of one module (will break github blame, but renaming is needed)

previous PRs:
closes #5370 closes #5296 closes #4575 closes #2744 closes #2435

There is an old issue #846 from before GSOC, but I don't see directly a GSOC GAM issue
There are several follow-up or details issues that I didn't check for closing.

DonBeo and others added 30 commits January 29, 2019 10:42
refactor get_prediction, improve unit test
@josef-pkt josef-pkt added this to the 0.10 milestone Jan 29, 2019
@josef-pkt

Copy link
Copy Markdown
Member Author

MAINT compatibility warnings
taken from https://ci.appveyor.com/project/josef-pkt/statsmodels/builds/21975155/job/i6bsmi7x8kre4ab4?fullLog=true

statsmodels\gam\smooth_basis.py:751
  C:\projects\statsmodels\statsmodels\gam\smooth_basis.py:751: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
    for smoother in self.smoothers)

as_matrix is also used in several other places

statsmodels\gam\tests\test_gam.py:378
  C:\projects\statsmodels\statsmodels\gam\tests\test_gam.py:378: FutureWarning: Method .as_matrix will be removed in a future version. Use .values instead.
    x = data_from_r.x.as_matrix()

some noise (but pytest is a pain and doesn't show which unit tests issues the warning)

statsmodels\gam\smooth_basis.py:384
  C:\projects\statsmodels\statsmodels\gam\smooth_basis.py:384: RuntimeWarning: divide by zero encountered in reciprocal
    der2_basis[:, i] = dg * (dg - 1) * self.x ** (dg - 2)
  C:\projects\statsmodels\statsmodels\gam\smooth_basis.py:384: RuntimeWarning: invalid value encountered in multiply
    der2_basis[:, i] = dg * (dg - 1) * self.x ** (dg - 2)
statsmodels\gam\gam_penalties.py:187
  C:\projects\statsmodels\statsmodels\gam\gam_penalties.py:187: UserWarning: weights is currently ignored
    warnings.warn('weights is currently ignored')

no idea which test or folder produces this warning

statsmodels\genmod\families\family.py:81
  C:\projects\statsmodels\statsmodels\genmod\families\family.py:81: DeprecationWarning: Calling Family(..) with a link class as argument is deprecated.
  Use an instance of a link class instead.
    warnings.warn(warnmssg, category=DeprecationWarning)

@josef-pkt

Copy link
Copy Markdown
Member Author

The runtime and user warnings are in test_gam

using
>pytest -v --pdb -W error::RuntimeWarning M:...\statsmodels\gam\tests\test_gam.py

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    def test_train_test_smoothers():
        n = 6
        x = np.zeros(shape=(n, 2))
        x[:, 0] = range(6)
        x[:, 1] = range(6, 12)
>       poly = PolynomialSmoother(x, degrees=[3, 3])

..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\tests\test_gam.py:
430:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\smooth_basis.py:81
0: in __init__
    variable_names=variable_names)
..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\smooth_basis.py:74
9: in __init__
    self.smoothers = self._make_smoothers_list()
..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\smooth_basis.py:81
7: in _make_smoothers_list
    variable_name=self.variable_names[v])
..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\smooth_basis.py:36
8: in __init__
    variable_name=variable_name)
..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\smooth_basis.py:31
0: in __init__
    base4 = self._smooth_basis_for_single_variable()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <statsmodels.gam.smooth_basis.UnivariatePolynomialSmoother object at 0x00
00000008D7B198>

    def _smooth_basis_for_single_variable(self):
        # TODO: unclear description
        """
            given a vector x returns poly=(1, x, x^2, ..., x^degree)
            and its first and second derivative
            """

        basis = np.zeros(shape=(self.nobs, self.degree))
        der_basis = np.zeros(shape=(self.nobs, self.degree))
        der2_basis = np.zeros(shape=(self.nobs, self.degree))
        for i in range(self.degree):
            dg = i + 1
            basis[:, i] = self.x ** dg
            der_basis[:, i] = dg * self.x ** (dg - 1)
>           der2_basis[:, i] = dg * (dg - 1) * self.x ** (dg - 2)
E           RuntimeWarning: divide by zero encountered in reciprocal

..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\smooth_basis.py:38
4: RuntimeWarning
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\smooth
_basis.py(384)_smooth_basis_for_single_variable()
-> der2_basis[:, i] = dg * (dg - 1) * self.x ** (dg - 2)
(Pdb) dg
1
(Pdb) self.x.min()
0.0

and for UserWarning, I get

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> traceback >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    def test_multivariate_penalty():
        alphas = [1, 2]
        weights = [1, 1]
        np.random.seed(1)
        x, y, pol = multivariate_sample_data()

        univ_pol1 = UnivariatePolynomialSmoother(x[:, 0], degree=pol.degrees[0])

        univ_pol2 = UnivariatePolynomialSmoother(x[:, 1], degree=pol.degrees[1])


        gp1 = UnivariateGamPenalty(alpha=alphas[0], univariate_smoother=univ_pol
1)
        gp2 = UnivariateGamPenalty(alpha=alphas[1], univariate_smoother=univ_pol
2)
        mgp = MultivariateGamPenalty(multivariate_smoother=pol, alpha=alphas,
>                                    weights=weights)

..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\tests\test_gam.py:
266:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <statsmodels.gam.gam_penalties.MultivariateGamPenalty object at 0x0000000
009886A20>
multivariate_smoother = <statsmodels.gam.smooth_basis.PolynomialSmoother object
at 0x0000000009886438>
alpha = [1, 2], weights = [1, 1], start_idx = 0

    def __init__(self, multivariate_smoother, alpha, weights=None,
                 start_idx=0):

        if len(multivariate_smoother.smoothers) != len(alpha):
            msg = ('all the input values should be of the same length.'
                   ' len(smoothers)=%d, len(alphas)=%d') % (
                   len(multivariate_smoother.smoothers), len(alpha))
            raise ValueError(msg)

        self.multivariate_smoother = multivariate_smoother
        self.dim_basis = self.multivariate_smoother.dim_basis
        self.k_variables = self.multivariate_smoother.k_variables
        self.nobs = self.multivariate_smoother.nobs
        self.alpha = alpha
        self.start_idx = start_idx
        self.k_params = start_idx + self.dim_basis

        # TODO: Review this,
        if weights is None:
            # weights should have total length as params
            # but it can also be scalar in individual component
            self.weights = [1. for _ in range(self.k_variables)]
        else:
            import warnings
>           warnings.warn('weights is currently ignored')
E           UserWarning: weights is currently ignored

..\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\gam_penalties.py:1
87: UserWarning
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> entering PDB >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
> m:\josef_new\eclipse_ws\statsmodels\statsmodels_py34_pr\statsmodels\gam\gam_pe
nalties.py(187)__init__()
-> warnings.warn('weights is currently ignored')

@josef-pkt

josef-pkt commented Jan 29, 2019 •

Copy link
Copy Markdown
Member Author

I fixed the zero division in polynomial smoother, but will leave the userwarning that weights are ignored.

The usage of weights in penalization still needs to be fixed. AFAICS or AFAIR it is only partially implemented. but not in this PR.

@josef-pkt

Copy link
Copy Markdown
Member Author

as green as it gets
merging

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants