Skip to content

Commit 12dd4f0

Browse files
committed
More fixes to initialization and link functions.
1 parent 2226ccd commit 12dd4f0

File tree

4 files changed

+18
-26
lines changed

4 files changed

+18
-26
lines changed

pymc3/examples/glm_linear.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
1-
from __future__ import print_function
2-
3-
import numpy as np
41
import sys
52

6-
try:
7-
import statsmodels.api as sm
8-
except ImportError:
9-
print("Example requires statsmodels")
10-
sys.exit(0)
3+
import numpy as np
4+
import scipy.optimize as opt
115

126
from pymc3 import *
137

@@ -31,13 +25,12 @@ def run(n=2000):
3125
import matplotlib.pyplot as plt
3226

3327
with model:
34-
trace = sample(n, Slice())
28+
start = find_MAP(fmin=opt.fmin_powell)
29+
trace = sample(n, Slice(), start=start)
3530

3631
plt.plot(x, y, 'x')
3732
glm.plot_posterior_predictive(trace)
3833
# plt.show()
3934

4035
if __name__ == '__main__':
4136
run()
42-
43-

pymc3/examples/glm_robust.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import print_function
2-
31
import numpy as np
42
import sys
53

@@ -20,7 +18,7 @@
2018
data_outlier = dict(x=x, y=y)
2119

2220
with Model() as model:
23-
family = glm.families.T(link=glm.families.identity,
21+
family = glm.families.T(#link=glm.families.identity,
2422
priors={'nu': 1.5,
2523
'lam': Uniform.dist(0, 20)})
2624
glm.glm('y ~ x', data_outlier, family=family)

pymc3/glm/families.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
__all__ = ['Normal', 'T', 'Binomial', 'Poisson']
99

1010
# Define link functions
11-
@staticmethod
12-
def identity(x):
13-
return x
1411

12+
# Hack as assigning a function in the class definition automatically binds it as a method.
13+
class Identity():
14+
def __call__(self, x):
15+
return x
16+
17+
identity = Identity()
1518
logit = theano.tensor.nnet.sigmoid
1619
inverse = theano.tensor.inv
1720
log = theano.tensor.log
@@ -20,7 +23,7 @@ class Family(object):
2023
"""Base class for Family of likelihood distribution and link functions.
2124
"""
2225
priors = {}
23-
link = identity
26+
link = None
2427

2528
def __init__(self, **kwargs):
2629
# Overwrite defaults
@@ -70,20 +73,18 @@ def __repr__(self):
7073
Link function: {link}.""".format(klass=self.__class__, likelihood=self.likelihood.__name__, parent=self.parent, priors=self.priors, link=self.link)
7174

7275

73-
class Normal(Family):
74-
link = identity
75-
likelihood = pm_dists.Normal
76-
parent = 'mu'
77-
priors = {'sd': pm_dists.HalfCauchy.dist(beta=10)}
78-
79-
8076
class T(Family):
8177
link = identity
8278
likelihood = pm_dists.T
8379
parent = 'mu'
8480
priors = {'lam': pm_dists.HalfCauchy.dist(beta=10),
8581
'nu': 1}
8682

83+
class Normal(Family):
84+
link = identity
85+
likelihood = pm_dists.Normal
86+
parent = 'mu'
87+
priors = {'sd': pm_dists.HalfCauchy.dist(beta=10)}
8788

8889
class Binomial(Family):
8990
link = logit

pymc3/glm/glm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def glm(*args, **kwargs):
124124
# Logistic regression
125125
vars = glm('male ~ height + weight',
126126
data,
127-
family=glm.families.Binomial(link=glm.links.Logit))
127+
family=glm.families.Binomial(link=glm.families.logit))
128128
"""
129129

130130
model = modelcontext(kwargs.get('model'))

0 commit comments

Comments
 (0)