locked
Converting a simple BUGS example to Infer.net RRS feed

  • Question

  • I am new to Infer.net, and I have a working example in BUGS that I would like to translate to Infer.net.  Here is the BUGS code:   

    model{
      for (j in 1:19){
        alpha[j] ~ dexp(1)
        beta[j] ~ dgamma(0.1, 1.0)
        tau[j] ~ dgamma(0.1, 1.0)
        r[j] ~ dgamma(alpha[j], beta[j])
        n[j] ~ dnorm(mu[j], tau[j])
        for (i in 1:5000) {
          temp[i, j] <- d[i,j] * r[j]
          w[i, j] <- d[i, j] / sum(temp[i,])
        }
        mu[j] <- r[j] * r[j] * sum(w[,j])
      }
    }
    



    Both d and n are observed.  d is a data structure with 5000 rows and 19 columns, and n is a vector of length 19.  Because of the large sample size,the code takes several hours to run.  

    I was able to get most of the code worked out, but I cannot figure out how to write the lines in the for(i in 1:5000) {....} in Infer.net.  Here is what I have so far: 

                double[][] D0 = file2JaggedArray(strPath + "D.csv"); // returns a jagged array
                double[] n0 = file2array1D(strPath + "n.csv"); // returns a vector
                Range N = new Range(D0.GetLength(0)).Named("N"); // number of rows
                Range J = new Range(D0.GetLength(1)).Named("J"); // number of cols
    
                VariableArray<double> alpha = Variable.Array<double>(J).Named("alpha");
                alpha[J] = Variable.GammaFromShapeAndRate(1, 1).ForEach(J);
                VariableArray<double> beta = Variable.Array<double>(J).Named("beta");
                beta[J] = Variable.GammaFromShapeAndRate(.1, 1).ForEach(J);
                VariableArray<double> tau = Variable.Array<double>(J).Named("tau");
                tau[J] = Variable.GammaFromShapeAndRate(.1, 1).ForEach(J);
                VariableArray<double> r = Variable.Array<double>(J).Named("r");
                r[J] = Variable.GammaFromShapeAndRate(alpha[J], beta[J]).ForEach(J);
    
                VariableArray<double> n = Variable.Array<double>(J).Named("n");
                n.ObservedValue = n0;
    
                // VariableArray2D<double> d = Variable.Observed<double>(D0, N, J).Named("d");
                VariableArray<VariableArray<double>,double[][]> d = 
                    Variable.Array(Variable.Array<double>(J), N).Named("d");
                d.ObservedValue = D0;
    
                VariableArray<double> mu = r ^ 2 * Variable.Sum(d[N] / Variable.Sum(d[N] * r));
                n[J] = Variable.GaussianFromMeanAndPrecision(mu[J], tau[J]).ForEach(J);
    

    I am having the most difficulty with this line: 

    VariableArray<double> mu = r ^ 2 * Variable.Sum(d[N] / Variable.Sum(d[N] * r));

    This line doesn't work and I am wondering if it would be possible to translate the BUGS code above to Infer.net?  

    Thanks

    Friday, January 30, 2015 8:00 AM

All replies

  • There is no direct translation.  This model performs operations on Gamma variables that are not available in Infer.NET.
    Friday, January 30, 2015 10:16 AM
    Owner
  • Thanks Tom!  

    I can change the distributions for the hyperpriors.    

    I mostly need help figuring out how to write the equivalent of this piece of the BUGS code in infer.net: 

        for (i in 1:5000) {
          temp[i, j] <- d[i,j] * r[j]
          w[i, j] <- d[i, j] / sum(temp[i,])
        }
        mu[j] <- r[j] * r[j] * sum(w[,j])
      }

    I am not sure how to compute the sum along a specific dimension of a matrix.  Any reference that would help me figure out a solution would be greatly appreciated!


    Friday, January 30, 2015 5:20 PM
  • If you just want to express the model in the Infer.NET API (but not run inference), the direct translation would be:

    var temp = Variable.Array(Variable.Array<double>(J), N);
    temp[N][J] = d[N][J] * r[J];
    var w = Variable.Array(Variable.Array<double>(N), J);
    w[J][N] = d[N][J] / Variable.Sum(temp[N]);
    var mu = Variable.Array<double>(J);
    mu[J] = r[J]*r[J]*Variable.Sum(w[J]);
    

    Friday, January 30, 2015 5:32 PM
    Owner