KeyError Setting Mixture Proportions for Mixture Model

If you wouldn’t mind, I’d like to ask you another question.

I’ve extended this model to have a triangle_name variable that comes from a mixture of categorical variables. I’ve defined this mixture very similarly to the triangle mixture. To make things simple, I made triangle=0., rather than defining it as a random variable. I expected everything to go well, but I get an error about bad initial energy for that value of triangle. When triangle=1., the sampler works.
Here is some sample code.

pTri_given_on = 1.
pTri_given_not_on = .7
tri_delta_on = pTri_given_on - pTri_given_not_on
tri_name_giv_tri_on_dist = numpy.array([.4, .4, .2])
tri_name_giv_tri_not_on_dist = numpy.array([.2,.3, .5])

n = 1000
NA_ENCODING = -10
with pymc3.Model() as model:
   
   NA = pymc3.Constant.dist(c=NA_ENCODING)

   # On 
   pOn = pymc3.Beta('pOn', alpha=on_count, beta=(schema_count - on_count))
   on = pymc3.Bernoulli('on', p=pOn)

   triangle = 0.
   triangle_name_mixture_weights = [on * triangle, (1. - on) * triangle, on * (1. - triangle) + (1. - on) * (1. - triangle)]
   tri_name_given_tri_and_on = pymc3.Categorical.dist(p=tri_name_giv_tri_on_dist)
   tri_name_given_tri_and_not_on = pymc3.Categorical.dist(p=tri_name_giv_tri_not_on_dist)
   triangle_name = pymc3.Mixture('triangle_name', w=triangle_name_mixture_weights, \
                                 comp_dists=[tri_name_given_tri_and_on, tri_name_given_tri_and_not_on, NA], \
                                 shape=1, testval=0., dtype="int64")
   res=pymc.sample(n)

This code generates the following error:

Multiprocess sampling (2 chains in 2 jobs)
CompoundStep
>NUTS: [pOn]
>BinaryGibbsMetropolis: [on]
>Metropolis: [triangle_name]
Sampling 2 chains:   0%|                            | 0/3000 [00:00<?, ?draws/s]/usr/local/lib/python3.5/dist-packages/numpy/core/fromnumeric.py:3118: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)
/usr/local/lib/python3.5/dist-packages/numpy/core/fromnumeric.py:3118: RuntimeWarning: Mean of empty slice.
  out=out, **kwargs)

Bad initial energy, check any log probabilities that are inf or -inf, nan or very small:
triangle_name   NaN
pymc3.parallel_sampling.RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/pymc3/parallel_sampling.py", line 160, in _start_loop
    point, stats = self._compute_point()
  File "/usr/local/lib/python3.5/dist-packages/pymc3/parallel_sampling.py", line 191, in _compute_point
    point, stats = self._step_method.step(self._point)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/step_methods/compound.py", line 27, in step
    point, state = method.step(point)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/step_methods/arraystep.py", line 247, in step
    apoint, stats = self.astep(array)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/step_methods/hmc/base_hmc.py", line 144, in astep
    raise SamplingError("Bad initial energy")
pymc3.exceptions.SamplingError: Bad initial energy
"""

The above exception was the direct cause of the following exception:

pymc3.exceptions.SamplingError: Bad initial energy

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "test.py", line 109, in <module>
    res = pymc3.sample(n)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/sampling.py", line 432, in sample
    trace = _mp_sample(**sample_args)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/sampling.py", line 965, in _mp_sample
    for draw in sampler:
  File "/usr/local/lib/python3.5/dist-packages/pymc3/parallel_sampling.py", line 393, in __iter__
    draw = ProcessAdapter.recv_draw(self._active)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/parallel_sampling.py", line 297, in recv_draw
    raise error from old_error
pymc3.parallel_sampling.ParallelSamplingError: Bad initial energy

The reason why I ask is because I want to make triangle_name depend on the system’s beliefs about on and triangle. When I make this model explicit:

# Triangle
   triangle_mixture_weights = [on, (1. - on)]
   tri_giv_on = pymc3.Bernoulli.dist(pTri_given_not_on + tri_delta_on)
   tri_giv_not_on = pymc3.Bernoulli.dist(pTri_given_not_on)
   triangle = pymc3.Mixture('triangle', w=triangle_mixture_weights, \
                            comp_dists=[tri_giv_on, tri_giv_not_on], \
                            shape=1, testval=0., dtype="int64")
   
   triangle_name_mixture_weights = [on * triangle, (1. - on) * triangle, on * (1. - triangle) + (1. - on) * (1. - triangle)]
   tri_name_given_tri_and_on = pymc3.Categorical.dist(p=tri_name_giv_tri_on_dist)
   tri_name_given_tri_and_not_on = pymc3.Categorical.dist(p=tri_name_giv_tri_not_on_dist)
   triangle_name = pymc3.Mixture('triangle_name', w=triangle_name_mixture_weights, \
                                 comp_dists=[tri_name_given_tri_and_on, tri_name_given_tri_and_not_on, NA], \
                                 shape=1, testval=0., dtype="int64")

I get the following error.

Traceback (most recent call last):
  File "test.py", line 107, in <module>
    res = pymc3.sample(n)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/sampling.py", line 401, in sample
    step = assign_step_methods(model, step, step_kwargs=kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/sampling.py", line 150, in assign_step_methods
    return instantiate_steppers(model, steps, selected_steps, step_kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/sampling.py", line 71, in instantiate_steppers
    step = step_class(vars=vars, **args)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/step_methods/arraystep.py", line 65, in __new__
    step.__init__([var], *args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/step_methods/metropolis.py", line 136, in __init__
    self.delta_logp = delta_logp(model.logpt, vars, shared)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/step_methods/metropolis.py", line 624, in delta_logp
    [logp0], inarray0 = pm.join_nonshared_inputs([logp], vars, shared)
  File "/usr/local/lib/python3.5/dist-packages/pymc3/theanof.py", line 264, in join_nonshared_inputs
    xs_special = [theano.clone(x, replace, strict=False) for x in xs]
  File "/usr/local/lib/python3.5/dist-packages/pymc3/theanof.py", line 264, in <listcomp>
    xs_special = [theano.clone(x, replace, strict=False) for x in xs]
  File "/usr/local/lib/python3.5/dist-packages/theano/scan_module/scan_utils.py", line 247, in clone
    share_inputs)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 232, in rebuild_collect_shared
    cloned_v = clone_v_get_shared_updates(outputs, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 93, in clone_v_get_shared_updates
    clone_v_get_shared_updates(i, copy_inputs_over)
  File "/usr/local/lib/python3.5/dist-packages/theano/compile/pfunc.py", line 96, in clone_v_get_shared_updates
    [clone_d[i] for i in owner.inputs], strict=rebuild_strict)
  File "/usr/local/lib/python3.5/dist-packages/theano/gof/graph.py", line 246, in clone_with_new_inputs
    new_node = self.op.make_node(*new_inputs)
  File "/usr/local/lib/python3.5/dist-packages/theano/tensor/elemwise.py", line 230, in make_node
    % (self.input_broadcastable, ib)))
TypeError: The broadcastable pattern of the input is incorrect for this op. Expected (True,), got (False,).

I am assuming that this error is related to the first one, but I may be wrong.