I saw a bunch of these models for the Columbia river at
the ADMB meeting and ISEC last year.
More below. These involved questions are daunting.
If I may summarize, I'm thinking you want to have
X ~ foo(alpha);
Y ~ bar(beta);
and you only observe Z = (X + Y) and want to estimate alpha and
beta? I don't know how to do this in general, other than to have
a latent variable for X for each observation
data {
real Z[N];
parameters {
real X[N];
}
model {
X ~ foo(alpha);
(Z - X) ~ bar(beta);
}
I don't know how to do this in general. Obviously, you'd need to respect
other constraints, but that can be tricky with our language---it won't let
you put varying constraints on parameter vectors, so you need to do the
transform and Jacobian by hand. Suppose you need X and Y to
both be positive and Z is positive. Then you'd have
parameters {
real X_raw[N];
...
transformed parameters {
real X[N] = inv_logit(X) .* Z; // X[n] now in (0, Z[n])
...
model {
// log Jacobian of X_raw -> X transform
target += Z .* log_inv_logit(X) + log1m_inv_logit(X);
- Bob
> On Apr 19, 2017, at 1:11 PM, Dalton Hance <
dalton...@gmail.com> wrote:
>
> Hello Stan Users,
>
> I'm trying to do something which seems like it should be relatively simple, but is turning out to be nigh impossible (at least with my skillset). First some context: We (USGS Columbia River Research Lab) are working on implementing a multi-state mark recapture model in Stan to estimate survival and transition probabilities (between different migration routes) for individually marked fish migrating through a rather complicated river system. As part of a our multistate model we are interested in amount of time it takes a fish to transit the system depending on the route it takes. So for example consider the following ASCII schematic:
>
> 2 3
> A_____|______|__
> 1 / \ 4
> R----|-- ----|
> \_____|______|___/
> B
>
> A group of fish are released all at the same time at point R on the left-hand side of the schematic. They migrate towards the right. If they are detected at a station (denoted by "|") their time of passage is detected. After station 1, fish encounter an intersection where they can take either path A or path B. These paths merge back together shortly after each path's respective station 3. We are interested in the distribution of travel times between each station. For example we want to know the mean travel time from R -> 1, from 1 -> 2A or 1->2B, 2A->3A, etc. If we had perfect detection at each station this wouldn't be a problem, but of course we don't. FIsh can pass a station or multiple stations without being detected. But for simplicity lets imagine that no fish die, all fish migrate downstream, and detection at station 4 (the last station) is perfect. So now let's consider three fish, Annie, Billy, and Pat. Annie is detected at station 1, 2A, 3A, and 4 and has the following travel times: R->1 = 1, 1->2A = 2 (cumulative 3), 2A -> 3A = 2 (cumulative 5), and 3A -> 4 = 1 (cumulative 6) days respectively. Billy is detected at station 1, 2B, 3B, and 4 with travel times of 0.75, 4 (4.75), 4 (8.75), 2 (10.75). Pat on the other hand is detected only at station 1 and station 4 with a travel time of R -> 1 = 1.25 and 1 -> 4 of 7.75 (cumulative 10). So the question is what is the probability model for these travel times?
>
> If these were nice independent Guassian fish, this wouldn't be a problem at all. For Annie we have R->1 ~ N(mu_1, sigma_1), 1->2A ~ N(mu_2A, sigma_2A), 2A->3A ~ N(mu_3A, sigma_3A), 3A->4 ~ N(mu_4A, sigma_4A). For Billy we have R->1 ~ N(mu_1, sigma_1), 1->2B ~ N(mu_2B, sigma_2B), 2B->3B ~ N(mu_3B, sigma_3B), 3B->4 ~ N(mu_4B, sigma_4B). Pat is just a little bit more complicated with R->1 ~ N(mu_1, sigma_1) and 1->4 ~ pA * N(mu_2A + mu_3A + mu_4A, sigma_2A + sigma_3A + sigma_4A) + (1-pA) * N(mu_2B + mu_3B + mu_4B, sigma_2B + sigma_3B + sigma_4B), where pA is the probability of going through route A. But this isn't a problem. It's just a mixture distribution and Stan does great with mixture distributions. I can figure out how to implement and wrap it up together using the forward algorithm within a comprehensive multistate model.
>
> With me so far?
Yes.
> Here's the problem: these aren't nice independent Gaussian fish. The travel times we observe are of course all non-negative, but they are also right skewed. So lognormal or gamma are the obvious choices here. Well no big deal right? In order to marginalize everything and get the likelihood for the mixture distribution that describes Pat's cumulative travel time we just need the pdf of the sum of lognormals or gammas. That's where I run into trouble. There is no closed-form pdf for the sum of lognormals. I also don't think I can do imputation because I don't see a way to do that within the mixture distribution framework and keep track of the state when imputing more than one travel time.
>
> So that led me to attempt to implement a model for the sum of independent gammas (different alpha and beta, it's trivial if beta is the same) for which there is a closed form pdf. Paper attached.
>
> Ok so after all that and before I get into my specific programming question let me lay out my goals for this post. I am primarily interested only in determining mean travel time for each station-to-station segment within a multistate model where I know travel-times are non-negative and right-skewed and where sometimes all I observe are cumulative travel times over multiple segments. I am not married to sum of independent gammas as the solution to this problem, but I latched onto it because I'm fairly certain I need some closed form pdf for the sum of travel times when all I observe is a cumulative travel time.
Usually you can choose a latent parameterization where you
introduce the intermediate steps.
Not that I know of, but somebody else may know. If it's
just purely a sum, though, it won't be identified.
> Like I said at the start this seems like it should be simple, but it's most definitely not.
>
> R code to simulate data to fit the model as outlined in the attached Stan code:
>
>
> N = 1000
> p = c(0.75, 0.75, 1)
> alpha = c(2, 2.5, 3)
> beta = c(1/0.25, 1/0.5, 1/0.75)
> y = matrix(rep(NA, 3*N), ncol = 3)
> x = matrix(rep(NA, 3*N), ncol = 3)
>
> x[,1] = rgamma(N, shape = alpha[1], rate = beta[1])
> x[,2] = rgamma(N, shape = alpha[2], rate = beta[2])
> x[,3] = rgamma(N, shape = alpha[3], rate = beta[3])
>
> y[,1] = x[,1] * rbinom(N, 1, p[1])
> y[,2] = (x[,2] + x[,1]) * rbinom(N, 1, p[2])
> y[,3] = (x[,3] + x[,2] + x[,1]) * rbinom(N, 1, p[3])
>
> stan_dat = list(N = N,
> y = y,
> m = 15)
>
> s = stan("sum_of_gammas_Moschopoulos.stan",
> data = stan_dat,
> chain = 4,
> iter = 1000)
>
>
>
>
> --
> 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.
> <1985-The_Distribution_of_the_Sum_of_Independent_Gamma_Random_Variables.pdf><sum_of_gammas_Moschopoulos.stan>