# Softmax by hand

#1

Hi.
I’m working myself around the multinomial regression. At some point I have to change my model, more specifically my denominator dynamically (softmax). In order to accomplish that, I tried to work iteratively and change one thing at a time.

``````data {
int<lower=2> C; // # of alternatives choices
int<lower=1> N;	// # of observations
int<lower=1> K; // # of covariates
int<lower=1,upper=C> Y[N]; // observed choices
matrix[C,K] X[N]; // matrix of attributes for each N
}
parameters {
vector[K] beta;
}
model {
matrix[K,N] xb;
vector[N] s;
// priors
beta ~ normal(0, 5);

for (i in 1:N){
xb[,i] = exp(X[i]*beta);
s[i] = sum(xb[,i]);

Y[i] ~ categorical(xb[,i] / s[i]);
}
}
``````

Obviously I have missed something because this fails miserably.

``````Unrecoverable error evaluating the log probability at the initial value.
Exception: matrix[multi,uni] assign sizes: lhs (4) and assigning variable xb (8) must match in size  (in   'modela153dd30a65_feit2' at line 22)
``````

When specifying the model part as usual, things work fine:

``````model {
beta ~normal(0,3);
for (i in 1:N)
Y[i] ~ categorical_logit(X[i]*beta);
}
``````

Thanks for any help

#2

Could it be that you mixed up K and C in your declaration of xb. If so you need

``````matrix[C,N] xb;
``````