Bayesian matrix factorization

Hi,
I am working with Bayesian matrix factorization using the MovieLens database. Data consist of a matrix n×d of n=943 users and d=1682 movies where users assign a rate (1-5) to movies. Clearly this results in a very sparse matrix since I have only 100000 observations out of 943*1682 possible entries.

I define the likelihood as P(X|W,Z,β)=∏(i,j)∈ObsN(Xij|ZiWtj,β−1) where Z=n \times k matrix and W=d \times k with k = number of features, Obs are the observed entries in the original matrix X.

I want to compute the posterior probability P(Z,W,β|X).
I wrote the following Stan code:

"""data {
    int<lower=0> n_users;                       // Number of users (943)
    int<lower=0> n_movies;                      // Number of movies (1682)
    int<lower=0> n_features;                    // Number of features 
    int<lower=1> n_entries;                     // Number of entries in matrix 
    int<lower=0,upper=n_users> ii[n_entries];   // Observations of users
    int<lower=0,upper=n_movies> jj[n_entries];  // Observations of movies
    vector[n_entries] rating;


}
parameters {
    matrix[n_movies, n_features] W;         
    matrix[n_users, n_features] Z;
    real beta;
}

model {

    for (n in 1:n_entries) {
            rating[n] ~ normal(W[jj[n],:] * Z[ii[n],:]', 1/sqrt(beta));
    } 

    // Priors
    beta ~ gamma(1.5, 1);
    for (n in 1:n_users) {
        Z[n,:] ~  exponential(1);
    }
    for (n in 1:n_movies) {
        W[n,:] ~ exponential(1);
    }
}
"""

but I get a Initialization failed Runtime error, I can’t understand why. If I take a gaussian distribution as prior instead of the exponential it works.

1 Like

I believe it is because you need to constrain W and Z to be positive since the exponential is defined on the positive reals.

for example

matrix<lower=0>[n_movies, n_features] W
1 Like

It works! Thank you!
I thought that there was no need in doing so