initialize a random variableArray with different distributions in different elements

• Question

• When we have a VariableArray, and each element comes from a different prior, how can we implement it?

If all elements have the same prior (called xPrior for example) the code is like this:

`VariableArray<double> x = Variable.Array<double>(xRange).Named("x");x[xRange] = Variable<double>.Random(xPrior).ForEach(xRange);`

But what if the prior is different for different elements?

Can I initialize priors separately? Not all comming from the same distribution?

I wrote this code but I got an error since ObservedValue cannot be derived from an array element (but from the array itself).

```VariableArray<double> x = Variable.Array<double>(xRange).Named("x");

VariableArray<double> means = Variable.Observed(new double[] { 0, 5 }, xRange);
VariableArray<double> precs = Variable.Observed(new double[] { 0, 1 }, xRange);

x[xRange] = Variable<double>.Random(Gaussian.FromMeanAndPrecision(means[xRange].ObservedValue, precs[xRange].ObservedValue)).ForEach(xRange);
```

Is there any way to handle this? Or I should just switch to C# arrays to handle this?

Wednesday, October 15, 2014 4:49 PM

• ```var xPrior = Variable.Array<Gaussian>(xRange);
x[xRange] = Variable<double>.Random(xPrior[xRange]);
```

• Marked as answer by Wednesday, October 15, 2014 9:27 PM
Wednesday, October 15, 2014 5:47 PM

All replies

• ```var xPrior = Variable.Array<Gaussian>(xRange);
x[xRange] = Variable<double>.Random(xPrior[xRange]);
```

• Marked as answer by Wednesday, October 15, 2014 9:27 PM
Wednesday, October 15, 2014 5:47 PM
• Oh I thought I need to do this - so the same problem with range:

```xPrior[0] = Gaussian.FromMeanAndVariance(0,0);
xPrior[1] = Gaussian.FromMeanAndVariance(5,1);```

But I understood I can initialize xPrior like this:

`VariableArray<Gaussian> xPrior = Variable.Observed(             new Gaussian[] { Gaussian.FromMeanAndVariance(0,0), Gaussian.FromMeanAndVariance(5,1)  },              xRange);`
Then I don't need to work with range for initialization and it works. Thanks.

• Edited by Wednesday, October 15, 2014 9:27 PM
Wednesday, October 15, 2014 9:06 PM