# How to represent ratings

• ### Question

• Hello all,

I have a question regarding data representation in Infer.NET. I want to built a rating prediction system. Rating values are doubles with a Gaussian prior. I also have an item and a user index for each item in my data (should be represented by integers). What I'm trying to do is similar to the LDA model. I've tried building a jagged array to store my data. The outer array holds user indices and the inner arrays are 2D arrays holding <item,rating> pairs.The difference is that there is a mean and a precision value for each <item,user_community> pair, so my model looks like this:

// For each user

using (Variable.ForEach(U))

{

// For each rating of that user

using (Variable.ForEach(RatedItems))

{

// Choose a topic

var attitude = Variable.Discrete(Theta[U]).Named("attitude");

using (Variable.Switch(attitude))

{

Ratings[U][RatedItems,1] = Variable.GaussianFromMeanAndPrecision(Means[Ratings[U][RatedItems,0], attitude], Precs[Ratings[U][RatedItems,0], attitude]);

}

}

}

Which is not working, since Ratings[U][RatedItems,0] is a double and the index must be integer.

Do you have any suggestions on how I could overcome this? I'm not even sure that I'm thinking about this the right way.

Kind regards,

Nikos

Wednesday, August 31, 2011 1:15 PM

### All replies

• You need an array ItemNumber[U][RatedItems] that specifies for each user the items that they have rated.  Then the generative process is:

Ratings[U][RatedItems] = Variable.GaussianFromMeanAndPrecision(Means[ItemNumber[U][RatedItems], attitude], Precs[ItemNumber[U][RatedItems], attitude]);

Wednesday, August 31, 2011 1:55 PM
• Hello Tom and thanks for the prompt reply which confirms that I'm in the right path, at least.

However, I had already tried what you suggested, and just in case, I tried it again right now. For testing purposes my data are:

double[][] ratings = new double[5][]

{

new double[] {4, 5, 5},

new double[] {4, 2, 1},

new double[] {3, 2, 4},

new double[] {4, 4},

new double[] {2, 1, 3, 5}

};

int[][] items = new int[5][]

{

new int[] {0, 2, 3},

new int[] {0, 1, 2},

new int[] {0, 2, 3},

new int[] {0, 1},

new int[] {0, 1, 2, 3}

};

and the means and precs arrays are initialized in that way:

Means = Variable.Array<double>(Y, K).Named("Means");

Means[Y, K] = Variable.GaussianFromMeanAndVariance(5, 5).ForEach(Y, K);

Precs = Variable.Array<double>(Y, K).Named("Precs");

Precs[Y, K] = Variable.GammaFromShapeAndScale(1, 1).ForEach(Y, K);

where Y and K are ranges defined with the number of items and number of attitudes as parameters.

Then I define the ratings and items arrays:

Ratings = Variable.Array(Variable.Array<double>(RatedItems), U).Named("Ratings");

Items = Variable.Array(Variable.Array<int>(RatedItems), U).Named("Items");

and define the model as you suggested above.

When trying to perform inference (using Variational Message Passing), however, I get a "Process is terminated due to StackOverflowException." message and the program ends. This is really hard to debug since it happens exactly when the Engine.Infer() method is invoked. Do you have any suggestions on what might be wrong or how I can debug this?

Wednesday, August 31, 2011 3:10 PM
• This is due to a bug in 2.4 Beta 2 which will be fixed in the next version.  Meanwhile you can work around it by using jagged arrays for Means and Precs:

```var Means = Variable.Array<double>(Variable.Array<double>(Y), K).Named("Means");
Means[K][Y] = Variable.GaussianFromMeanAndVariance(5, 5).ForEach(K,Y);
var Precs = Variable.Array<double>(Variable.Array<double>(Y), K).Named("Precs");
Precs[K][Y] = Variable.GammaFromShapeAndScale(1, 1).ForEach(K,Y);
```

Wednesday, August 31, 2011 3:50 PM
• Many thanks Tom, it is working now!