Mixture of Multivariate Student-T


#1

Hi friends,
I’m trying to estimate the parameters of a mixture of Multivariate Student-T distributions, so I built the follow model, but I’m getting the error bellow, can someone point my mistake? Thank you all!

with pm.Model() as model:
    # Priors    
    position = pm.Beta('position', alpha=2, beta=2, testval=0.5)
    w = pm.Dirichlet('w', a=np.array([1, 1]))
    # Prior Covariance Matrix and transformed parameters 
    packed_L = pm.LKJCholeskyCov('packed_L', n=9, eta=2., sd_dist=pm.HalfCauchy.dist(2.5))
    L = pm.Deterministic("L", var=pm.expand_packed_triangular(9, packed_L, lower=True))

    mu_raw = pm.StudentT('mu_raw', nu=30, sd=1, shape=9)
    mu = pm.Deterministic("mu", tt.dot(L, boltzmann_raw))
    
    # Likelihood Covariance Matrix
    PKL = pm.LKJCholeskyCov('PKL', n=9, eta=2., sd_dist=pm.HalfCauchy.dist(2.5))
    Lo = pm.expand_packed_triangular(9, PKL)

    func_0 = pm.MvStudentT('func_0',nu=30, mu=mu - position, chol=Lo, shape=9)
    func_1 = pm.MvStudentT('func_1',nu=30, mu=mu + position, chol=Lo, shape=9)
    
    # Likelihood
    like = pm.Mixture('like', w=w, comp_dists = [func_0, func_1], observed=data)

AttributeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pymc3/distributions/mixture.py in _comp_means(self)
118 try:
–> 119 return tt.as_tensor_variable(self.comp_dists.mean)
120 except AttributeError:

AttributeError: ‘list’ object has no attribute ‘mean’

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/theano/tensor/type.py in dtype_specs(self)
268 ‘complex64’: (complex, ‘theano_complex64’, ‘NPY_COMPLEX64’)
–> 269 }[self.dtype]
270 except KeyError:

KeyError: ‘object’

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/theano/tensor/basic.py in constant(x, name, ndim, dtype)
245 try:
–> 246 ttype = TensorType(dtype=x_.dtype, broadcastable=bcastable)
247 if not constant.enable:

/usr/local/lib/python3.6/dist-packages/theano/tensor/type.py in init(self, dtype, broadcastable, name, sparse_grad)
50 self.broadcastable = tuple(bool(b) for b in broadcastable)
—> 51 self.dtype_specs() # error checking is done there
52 self.name = name

/usr/local/lib/python3.6/dist-packages/theano/tensor/type.py in dtype_specs(self)
271 raise TypeError(“Unsupported dtype for %s: %s”
–> 272 % (self.class.name, self.dtype))
273

TypeError: Unsupported dtype for TensorType: object

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/theano/tensor/basic.py in as_tensor_variable(x, name, ndim)
193 try:
–> 194 return constant(x, name=name, ndim=ndim)
195 except TypeError:

/usr/local/lib/python3.6/dist-packages/theano/tensor/basic.py in constant(x, name, ndim, dtype)
265 except Exception:
–> 266 raise TypeError(“Could not convert %s to TensorType” % x, type(x))
267

TypeError: (‘Could not convert to TensorType’, <class ‘method’>)

During handling of the above exception, another exception occurred:

AsTensorError Traceback (most recent call last)
in ()
18
19 # Likelihood
—> 20 like = pm.Mixture(‘like’, w=w, comp_dists = [func_0, func_1], observed=data)

/usr/local/lib/python3.6/dist-packages/pymc3/distributions/distribution.py in new(cls, name, *args, **kwargs)
39 raise TypeError(“observed needs to be data but got: {}”.format(type(data)))
40 total_size = kwargs.pop(‘total_size’, None)
—> 41 dist = cls.dist(*args, **kwargs)
42 return model.Var(name, dist, data, total_size)
43 else:

/usr/local/lib/python3.6/dist-packages/pymc3/distributions/distribution.py in dist(cls, *args, **kwargs)
50 def dist(cls, *args, **kwargs):
51 dist = object.new(cls)
—> 52 dist.init(*args, **kwargs)
53 return dist
54

/usr/local/lib/python3.6/dist-packages/pymc3/distributions/mixture.py in init(self, w, comp_dists, *args, **kwargs)
83
84 try:
—> 85 self.mean = (w * self._comp_means()).sum(axis=-1)
86
87 if ‘mean’ not in defaults:

/usr/local/lib/python3.6/dist-packages/pymc3/distributions/mixture.py in _comp_means(self)
121 return tt.squeeze(tt.stack([comp_dist.mean
122 for comp_dist in self.comp_dists],
–> 123 axis=1))
124
125 def _comp_modes(self):

/usr/local/lib/python3.6/dist-packages/theano/tensor/basic.py in stack(tensors, **kwargs)
4707 dtype = scal.upcast(
[i.dtype for i in tensors])
4708 return theano.tensor.opt.MakeVector(dtype)(*tensors)
-> 4709 return join(axis, *[shape_padaxis(t, axis) for t in tensors])
4710
4711

/usr/local/lib/python3.6/dist-packages/theano/tensor/basic.py in (.0)
4707 dtype = scal.upcast(*[i.dtype for i in tensors])
4708 return theano.tensor.opt.MakeVector(dtype)(*tensors)
-> 4709 return join(axis, *[shape_padaxis(t, axis) for t in tensors])
4710
4711

/usr/local/lib/python3.6/dist-packages/theano/tensor/basic.py in shape_padaxis(t, axis)
4594
4595 “”"
-> 4596 _t = as_tensor_variable(t)
4597
4598 ndim = _t.ndim + 1

/usr/local/lib/python3.6/dist-packages/theano/tensor/basic.py in as_tensor_variable(x, name, ndim)
198 except Exception:
199 str_x = repr(x)
–> 200 raise AsTensorError(“Cannot convert %s to TensorType” % str_x, type(x))
201
202 # this has a different name, because _as_tensor_variable is the

AsTensorError: (‘Cannot convert to TensorType’, <class ‘method’>)


#2

This solved : https://github.com/junpenglao/Planet_Sakaar_Data_Science/blob/master/WIP/[WIP]%20Bayesian%20GMM.ipynb
Thank you Junpeng Lao!