locked
Shared Variable 2D Jagged Arrays RRS feed

  • Question

  • Hello,

    I'm having problems creating a 2D Jagged Shared Variable array using the overload: 

    Random<ItemType, DistributionArrayType>(ItemType, Range, DistributionArrayType, Boolean)

    The code I've done so far (which works) is:

    using GaussianArray = DistributionStructArray<Gaussian, double>;
        using GaussianArrayArray = DistributionRefArray<DistributionStructArray<Gaussian, double>, double[]>;
        using GaussianArrayArrayArray = DistributionRefArray<DistributionRefArray<DistributionStructArray<Gaussian, double>, double[]>, double[][]>;

    Followed by setting up the prior (which also works):

    var prior = new GaussianArrayArrayArray(new GaussianArrayArray(new GaussianArray(Gaussian.Uniform(), innerinner.SizeAsInt), inner.SizeAsInt), outer.SizeAsInt);

    I've then tried the following code:

    var x = SharedVariable<double>.Random(Variable.Array(Variable.Array<double>(innerinner), inner), outer, prior);
    However this just gives an error saying I can't use 'prior' as a DistributionArrayType. What am I doing wrong here?

    Tuesday, August 5, 2014 5:34 PM

Answers

  • The domain type is double[][][] for a GaussianArrayArrayArray. So the following should work:

                var outerSize = Variable.New<int>();
                var outerRange = new Range(outerSize);
                var innerSize = Variable.Array<int>(outerRange);
                var innerRange = new Range(innerSize[outerRange]);
                var innerInnerSize = Variable.Array(Variable.Array<int>(innerRange), outerRange);
                var innerInnnerRange = new Range(innerInnerSize[outerRange][innerRange]);
    
                outerSize.ObservedValue = 2;
                innerSize.ObservedValue = new int[] {3, 4};
                innerInnerSize.ObservedValue = new int[][]
                {
                    new int[] {5, 6, 7},
                    new int[] {8, 9, 10, 11}
                };
    
                var prior = (GaussianArrayArrayArray)Distribution<double>.Array(Util.ArrayInit(
                    outerSize.ObservedValue, i => Util.ArrayInit(
                        innerSize.ObservedValue[i], j => Util.ArrayInit(
                            innerInnerSize.ObservedValue[i][j], k => Gaussian.Uniform()))));
    
                var x = SharedVariable<double[][][]>.Random(prior);
    John

    • Marked as answer by MarkG87 Wednesday, August 6, 2014 1:45 PM
    Wednesday, August 6, 2014 1:19 PM
    Owner
  • You need to use:

    var x = SharedVariable<double[][][]>.Random(Variable.Array(Variable.Array<double>(innerinner), inner), outer, prior);
    

    • Marked as answer by MarkG87 Wednesday, August 6, 2014 1:45 PM
    Wednesday, August 6, 2014 1:24 PM
    Owner

All replies

  • The domain type is double[][][] for a GaussianArrayArrayArray. So the following should work:

                var outerSize = Variable.New<int>();
                var outerRange = new Range(outerSize);
                var innerSize = Variable.Array<int>(outerRange);
                var innerRange = new Range(innerSize[outerRange]);
                var innerInnerSize = Variable.Array(Variable.Array<int>(innerRange), outerRange);
                var innerInnnerRange = new Range(innerInnerSize[outerRange][innerRange]);
    
                outerSize.ObservedValue = 2;
                innerSize.ObservedValue = new int[] {3, 4};
                innerInnerSize.ObservedValue = new int[][]
                {
                    new int[] {5, 6, 7},
                    new int[] {8, 9, 10, 11}
                };
    
                var prior = (GaussianArrayArrayArray)Distribution<double>.Array(Util.ArrayInit(
                    outerSize.ObservedValue, i => Util.ArrayInit(
                        innerSize.ObservedValue[i], j => Util.ArrayInit(
                            innerInnerSize.ObservedValue[i][j], k => Gaussian.Uniform()))));
    
                var x = SharedVariable<double[][][]>.Random(prior);
    John

    • Marked as answer by MarkG87 Wednesday, August 6, 2014 1:45 PM
    Wednesday, August 6, 2014 1:19 PM
    Owner
  • You need to use:

    var x = SharedVariable<double[][][]>.Random(Variable.Array(Variable.Array<double>(innerinner), inner), outer, prior);
    

    • Marked as answer by MarkG87 Wednesday, August 6, 2014 1:45 PM
    Wednesday, August 6, 2014 1:24 PM
    Owner