f(x; a,b) = a * b * x^(a - 1) * (1 - x^a)^(b - 1) for a,b > 0 and 0 < x < 1
log(f(x; a,b)) = log(a) + log(b) + (a - 1)
* log(x) + (b - 1) *
log(1 - x^a) for a,b > 0 and 0 < x < 1
data {
int<lower=1> N;
real<lower=0,upper=1> x[N];
}
transformed data {
real sum_log_x; // calculate this constant only once
sum_log_x <- 0.0;
for (i in 1:N)
sum_log_x <- sum_log_x + log(x[i]);
}
parameters {
real<lower=0> a;
real<lower=0> b;
}
model { real summands[N];
// put priors on a and b here if you want
// log-likelihood
lp__ <- lp__ + N * (log(a) + log(b)) + (a - 1) * sum_log_x;
for (i in 1:N) {
summands[i] <- (b - 1) * log1m(pow(x[i],a)); // log1m(y) := log(1 - y) } lp__ <- lp__ + sum(summands); // faster than doing inside loop
}
stopifnot(require(rstan))
N <- 1000
a <- 3
b <- 2
x <- rbeta(N, 1, b)^(1/a)
Kumaraswamy <- stan(file = "Kumaraswamy.stan", data = list(N = N, x = x), verbose = FALSE, refresh = -1)
print(Kumaraswamy)
> print(Kumaraswamy)
Inference for Stan model: Kumaraswamy.
4 chains: each with iter=2000; warmup=1000; thin=1; 2000 iterations saved.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
a 2.9 0 0.1 2.7 2.9 2.9 3.0 3.1 766 1
b 1.9 0 0.1 1.8 1.9 1.9 2.0 2.1 787 1
lp__ 282.4 0 1.0 279.7 282.1 282.7 283.1 283.4 683 1
Samples were drawn using NUTS2 at Sat Oct 6 16:00:30 2012.
For each parameter, n_eff is a crude measure of effective sample size,
and Rhat is the potential scale reduction factor on split chains (at
convergence, Rhat=1).
make O=3 /path/to/my/dotstanfile_without_extension
Many
(but not all in any given release) density functions accept a vector on
the left-hand side and either a vector or a scalar arguments. One such
distribution is the normal. In other words, this BUGS-like code
fragment is unnecessarily slow in Stan
real mu[N];
for (i in 1:N) {
mu[i] <- x[i] * beta;
y[i] ~ normal(mu[i], sigma);
}
while this is faster
vector[N] mu;
mu <- X * beta; // presuming X is a NxK matrix
y ~ normal(mu, sigma);
real mu[N];
for (i in 1:N) {
mu[i] <- x[i] * beta + RandomIntercept;
}
y ~ normal(mu, sigma);
parameters {
vector[K] beta[J];
vector[K] mu; // hier prior loc
real<lower=0> tau[K]; // hier prior scale
}
model { for(j in 1:J) beta[j] ~ normal(mu, tau);
// likelihood as a function of beta
}
parameters {
vector[K] e_beta[J]; // errors in beta
vector[K] mu; // hier prior loc
real<lower=0> tau[K]; // hier prior scale
}
transformed parameters {
vector[K] beta[J]; // intercept + slopes
for (k in 1:K)
for (j in 1:J)
beta[j,k] <- mu[k] + e_beta[j,k]*tau[k];
}
model { for(j in 1:J) e_beta[j] ~ normal(0,1); // standard normal prior implies beta ~ normal(mu,tau)
// proper priors on mu and tau
// likelihood as a function of beta, NOT e_beta
}
parameters {
vector[K] beta[J];
vector[K] mu;
cov_matrix[K] Sigma;
}
model {
for (j in 1:J) {
beta[j] ~ multi_normal(mu, Sigma);
}
Sigma ~ wishart(some_df, some_Scale);
// prior on mu
// likelihood as a function of beta
}
transformed data {
vector[K] zero_vector;
for (k in 1:K) {
zero_vector[k] <- 0;
}
}
parameters {
vector[K] e_beta[J];
vector[K] mu;
corr_matrix Tau;
real<lower=0> tau[K]; // standard deviations
}
transformed parameters {
vector[K] beta[J];
for (k in 1:K) {
for (j in 1:J) {
beta[j,k] <- mu[k] + e_beta[j,k] * tau[k]; // thanks to Mike Lawrence for catching typo
}
}
}
model {
for (j in 1:J) {
e_beta[j] ~ multi_normal(zero_vector, Tau);
}
// Note: 1.0 implies a uniform prior on Tau so omit it, but uncomment for shape != 1.0
// Tau ~ corr_matrix(1.0);
// proper priors on mu and tau
// likelihood as a function of beta not e_beta
}
parameters {
real mu[2]; // two unrestricted means
real<lower=0> sigma[2]; // two nonnegative standard deviations
real<lower=-1,upper=1> rho; // one correlation between -1 and 1
}
parameters {
real mu[2]; // two unrestricted means
real sigma[2]; // two "standard deviations", not restricted to be nonnegative
real rho; // one "correlation", not restricted to be between -1 and 1
}
model {
sigma[1] ~ SomePositivePrior()
;
sigma[2] ~ SomePositivePrior()
;
rho ~ uniform(-1.0, 1.0);
// more stuff
}
parameters {
real mu[2]; // two unrestricted means
real<lower=0> sigma[2]; // two nonnegative standard deviations
real<lower=-1,upper=1> rho; // one correlation between -1 and 1
}
model {
mu[1] ~ SomeUnrestrictedPrior();
mu[2] ~ SomeUnrestrictedPrior();
sigma[1] ~ SomePositivePrior()
;
sigma[2] ~ SomePositivePrior();
// implicit: rho ~ uniform(-1.0, 1.0);
// more stuff
}
transformed data {
int<lower=1> K;
real<lower=0> eta;
K <- 10000;
eta <- 1.0;
}
parameters {
corr_matrix[K] Sigma; // one correlation matrix of order K
}
model {
Sigma ~ lkj_corr(eta); // jointly uniform prior over valid correlation matrices of order K
// more stuff
}
parameters {
real mu[2]; // two unrestricted means
real<lower=0> sigma[2]; // two nonnegative standard deviations
real<lower=-1,upper=1> rho; // one correlation between -1 and 1
}
transformed parameter {
real<lower=0> variance1;
variance1 <- pow(sigma[1], 2.0);
}
model {
real precision2;
precision2 <- 1.0 / pow(sigma[2], 2.0);
mu[1] ~ SomeUnrestrictedPrior();
mu[2] ~ SomeUnrestrictedPrior();
sigma[1] ~ SomePositivePrior()
;
sigma[2] ~ SomePositivePrior();
// implicit: rho ~ uniform(-1.0, 1.0);
// more stuff that depends on sigma2 and precision
}
parameters {
real mu[2]; // two unrestricted means
real<lower=0> sigma[2]; // two nonnegative standard deviations
real<lower=L,upper=U> rho; // one correlation between -1 and 1
real<lower=-1,upper=1> mu_rho; // unknown mean for rho
}
model {
mu[1] ~ SomeUnrestrictedPrior();
mu[2] ~ SomeUnrestrictedPrior();
sigma[1] ~ SomePositivePrior()
;
sigma[2] ~ SomePositivePrior();
rho ~ normal(mu_rho, 0.5);
// implicit: mu_rho ~ uniform(-1.0,1.0);
// more stuff
}
parameters {
real mu[2]; // two unrestricted means
real<lower=0> sigma[2]; // two nonnegative standard deviations
real<lower=-1,upper=1> rho; // one correlation between -1 and 1
real<lower=-1,upper=1> mu_rho; // unknown mean for rho
}
model {
mu[1] ~ SomeUnrestrictedPrior();
mu[2] ~ SomeUnrestrictedPrior();
sigma[1] ~ SomePositivePrior()
;
sigma[2] ~ SomePositivePrior();
rho ~ normal(mu_rho, 0.5) T[-1.0,1.0];
// implicit: mu_rho ~ uniform(-1.0,1.0);
// more stuff
}
parameters {
real mu[2]; // two unrestricted means
real<lower=0> sigma[2]; // two nonnegative standard deviations
real<lower=-1,upper=1> rho; // one correlation between -1 and 1
}
model {
mu[1] ~ cauchy(0.0, 5.0); // a diffuse prior over the whole real line
mu[2] ~ cauchy(0.0, 5.0); // ditto
sigma[1] ~ cauchy(0.0, 5.0); // explicit truncation is not necessary
sigma[2] ~ cauchy(0.0, 5.0); // ditto
// implicit: rho ~ uniform(-1.0, 1.0);
// more stuff
}
If you have additional questions on this topic, please start a new thread on stan-users and paste in the relevant parts of your .stan file.
--
You received this message because you are subscribed to a topic in the Google Groups "Stan users mailing list" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/stan-users/4gv3fNCqSNk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to stan-users+...@googlegroups.com.
--
had status 127