# Custom Generation of a Discrete Distribution based on Random Variables • ### Question

• Hello,

I have a graphical model where there is a node, in a plate, that has a discrete distribution defined by a custom generation procedure. Specifically, the procedure generates a truncated geometric with some extra mass at x = 0.  It takes three parameters (n, gamma and lambda) where n controls the dimension of the discrete distribution and is always observed (but n varies based on plate id).

Does Infer.net support writing such a generation procedure for the discrete distribution? it tried writing a custom factor but ran into a problem: the framework doesn't like that the nodes have distributions whose dimensions vary based on the value of a random variable (even if the variable is observed).

Thanks,

Wednesday, October 9, 2013 3:23 PM

### All replies

• You will need to use a custom factor for this.  The tricky part is telling the framework what the cardinality of the output is.  You can do it by providing two definitions of the output: the first one only defines the cardinality (and is never used) while the second one provides the intended definition.  Here is an example using Binomial:

```          var x = Variable.New<int>();
var b = Variable.Observed(false);
using (Variable.If(b))
{
x.SetTo(Variable.DiscreteUniform(n + 1));
}
using (Variable.IfNot(b))
{
x.SetTo(Variable.Binomial(n, 0.1));
}
```
This causes the cardinality of x to be (n+1) which is the correct cardinality for the output of Binomial.  Since the factor you are trying to implement is already a mixture (due to the extra mass at zero), you could use the above construction to represent the mixture and accomplish two things at once.  The first definition of x would be a Discrete whose probs represent a point mass at zero, and whose cardinality is n.
Thursday, October 10, 2013 10:36 AM