Hi all,
I’m using Stan for the first time and I’d like to get feedback on the way I specified my model. I’m trying to predict whether two proteins interact given a particular vector-space encoding. Each pair of features needs a parameter informing whether that pair of features increases or decreases the probability of interaction. I was able to specify the model and run it, but I’m unhappy with the need for the for-loops to specify the prior and likelihood. Is there a way around this? The stan code and PyStan code are below. Thanks!
data {
int<lower=0> P; // number of proteins
int<lower=0> N; // number of features
matrix[P, N] feature_vectors; // data matrix
int<lower=0,upper=1> interaction_matrix[P, P]; // 0-1 matrix indicating interaction/lack of interaction
}
parameters {
matrix[N,N] w; // matrix of feature-feature weights contributing to interaction
}
transformed parameters {
real interaction_strength[P, P];
for (p1 in 1:P)
for (p2 in 1:P)
interaction_strength[p1, p2] = feature_vectors[p1] * w * feature_vectors[p2]';
}
model {
for (p1 in 1:P)
for (p2 in 1:P)
interaction_matrix[p1, p2] ~ bernoulli_logit(interaction_strength[p1, p2]);
for (n1 in 1:N)
for (n2 in 1:N)
w[n1,n2] ~ normal(0, 1);
}
import pystan
import numpy as np
model_fn = 'interaction_model.stan'
feature_vectors = np.array([
[.3, 0, 0, .9],
[.2, .5, .5, 0],
[.8, .2, 0, 0]
])
interaction_matrix = np.array([
[0, 0, 0],
[0, 0, 1],
[0, 1, 0]
])
P, N = feature_vectors.shape
dat = {
'P': P,
'N': N,
'feature_vectors': feature_vectors,
'interaction_matrix': interaction_matrix
}
sm = pystan.StanModel(model_fn)
fit = sm.sampling(data=dat, iter=1000, chains=4)