Stan will do this all for you pretty much automatically.
The manual has some examples of how to do the predictive
distributions within Stan (that is, you use MCMC for
the integral).
As to the normalized category probabilities, you just
need to normalize:
vector[K] lp;
simplex[K] log_theta;
log_theta = lp - log_sum_exp(lp);
This assumes lp[k] = p(y | k) * p(k),
where p(y|k) is probably of observation given category k
and p(k) is the marginal probability of categories.
Then log_theta[k] is log Pr[C = k | y].
exp(theta) = exp(lp) / sum(exp(lp))
- Bob
> On Jul 20, 2016, at 11:27 AM, Karl Möller <
ka...@excors.net> wrote:
>
> Not doing too well for my 1st post to the group, sorry. Correction:
>
> generated quantities{
>
> real x_new_log_likelihood[M];
> for (m in 1:M)
> x_new_log_likelihood[m] <- normal_log(x_new, mu[m], sigma[m])
>
> }
>
> And everywhere the word "score" pops up, I mean X (stripped out this from something I am trying). I hope these all the errors...
>
> Regards
> Karl
>
> On Wednesday, July 20, 2016 at 5:11:02 PM UTC+2, Karl Möller wrote:
> Hi
>
> I am new to Bayesian modelling and MCMC / Stan so please forgive all rookie mistakes...
>
> I am trying to construct a minimal working example of a Bayesian classifier for labeled data that should work for my requirements. Let us assume that 1D training samples X = {x_i} from
> every class (label) c_i are generated by some normal distribution with unknown \mu_i and \sigma_i specific to class c_i. So for a new text pattern x_new we know that:
>
> P(c_i | x_new, X) = P(x_new | c_i, X) P(c_i) / (\sum_j (P(x_new | c_j, X) P(c_j))
>
> where we can further write P(x_new | c_i, X) = \int d \theta P(x_new | c_i, \theta_i) P(\theta_i | c_i, X) where \theta_i = (\mu_i, \sigma_i)
>
> So to get distribution P(\theta_i|c_i, X) is pretty trivial using the following Stan code:
>
> data {
> int<lower=0> N; // num of training set samples
> int<lower=1> M; // num of classes
> real X[N]; //training data
> int<lower=1, upper=M> C[N]; // class for score n
> real x_new; //new test pattern to classify
>
> }
> transformed data {}
> parameters {
> real mu[M];
> real sigma[M];
> real x_pred[M];
> }
> transformed parameters {}
> model {
> //likelihood
> for (n in 1:N)
> scores[n] ~ normal(mu[C[n]], sigma[C[n]])
>
> //predictive posterior
> for (m in 1:M)
> x_pred[m] ~ normal(mu[m], sigma[m])
>
> }
> generated quantities {
> }
>
> I probably don't need the predictive posterior, but just used it as a sanity check.
> What I do not understand how to do in Stan is the marginalisation (integral/sum) over \theta to get the expected likelihood (and evidence
> follows trivially) and then find P(c_i | x_new). What I can do is
>
> generated quantities{
>
> real x_new_log_likelihood[M];
> for (m in 1:M)
> new_score_log_likelihood[m] <- normal_log(x_new)
>
> }
>
> And then in Python calculated the mean of 'new_score_log_likelihood' for every class (actually doing the required transformations because log \sum is not equal to \sum log) and plug in some priors for P(c_i) and just calculate
> P(c_i | x_new) in Python directly. Is there a better way I can do all of this in Stan directly? I hope my explanation is sufficient. I have looked at the
> Naive Bayes example in the manual and I think it should be the same as what I want, but the classification for a new test pattern is not part of the example if I understand
> it correctly.
>
> Regards
> Karl
>
>
> --
> You received this message because you are subscribed to the Google Groups "Stan users mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to
stan-users+...@googlegroups.com.
> To post to this group, send email to
stan-...@googlegroups.com.
> For more options, visit
https://groups.google.com/d/optout.