run_state_process(n_state_years::int, n_age::int,
pop_first::vector[n_demo], birth_rate_first::real, pop_total_first::real,
baseline_bbr::vector[Tb], dd_intercept::real, dd_slope::real,
aging::matrix[n_demo, n_demo], S_diag::vector[n_demo], mu_m::vector[n_demo],
hs_sw::vector[n_demo], hs_fi::vector[n_demo],
hq_sw::int[n_state_years], hq_fi::int[n_state_years],
he_sd_sw::real, he_sd_fi::real,
eps_h_sw::vector[n_state_years], eps_h_fi::vector[n_state_years],
t_mate_to_preg::real, t_birth_to_end_hunt::real,
eps_birth::vector[n_state_years], eps_sex::vector[n_state_years],
transition_noise_raw::matrix[Tn, n_state_years],
pi_s::vector[n_state_years], pi_c::vector[n_state_years], prob_of_ca::real,
ode_init_state::vector[1], ode_times::vector[1]) = begin
n_demo = 2 * n_age
ode_ts = to_array_1d(ode_times)
birth_rate::vector[n_state_years]
pregnancy_rate::vector[n_state_years]
population_total::vector[n_state_years]
hunted_sweden::matrix[n_demo, n_state_years]
hunted_finland::matrix[n_demo, n_state_years]
bycatch_expected::matrix[n_demo, n_state_years]
hunting_bag_total_sweden::vector[n_state_years]
hunting_bag_total_finland::vector[n_state_years]
reproductive_probs::matrix[4, n_state_years]
population_comp::matrix[n_demo, n_state_years]
survivors::matrix[n_demo, n_state_years]
for year in 1:n_state_years
if year == 1
birth_rate[year] = birth_rate_first
population_comp[:, year] = pop_first
population_total[year] = pop_total_first
else
birth_rate[year] = update_birth_rate(
baseline_bbr[year], dd_intercept, dd_slope, population_total[year - 1])
population_comp[:, year] = update_population_from_survivors(
survivors[:, year - 1], aging, birth_rate[year],
eps_birth[year], eps_sex[year], n_age)
population_total[year] = sum(population_comp[:, year])
end
pregnancy_rate[year] = update_pregnancy_rate(
baseline_bbr[year + 1], dd_intercept, dd_slope,
population_total[year], t_mate_to_preg)
hp_sw::vector[n_demo]
hp_fi::vector[n_demo]
log_N = log(population_comp[:, year])
log_denom_sw = log_sum_exp(hs_sw + log_N)
log_denom_fi = log_sum_exp(hs_fi + log_N)
if hq_sw[year] == 0
hp_sw = rep_vector(0.0, n_demo)
else
hp_sw = exp(hs_sw + log(hq_sw[year]) + log(2.0)
- 2.0 * log(t_birth_to_end_hunt)
- eps_h_sw[year] * he_sd_sw - log_denom_sw)
end
if hq_fi[year] == 0
hp_fi = rep_vector(0.0, n_demo)
else
hp_fi = exp(hs_fi + log(hq_fi[year]) + log(2.0)
- 2.0 * log(t_birth_to_end_hunt)
- eps_h_fi[year] * he_sd_fi - log_denom_fi)
end
exp_hunted_sw::vector[n_demo]
exp_hunted_fi::vector[n_demo]
for demo in 1:n_demo
# Reference package defaults are literal here because Stan requires
# solver controls to be data-only and @deffun has no such qualifier yet.
sol_sw = ode_rk45_tol(dH_dt, ode_init_state, 0.0, ode_ts, 1.0e-6, 1.0e-6, 1000,
population_comp[demo, year], t_birth_to_end_hunt,
hp_sw[demo], hp_fi[demo], mu_m[demo])
exp_hunted_sw[demo] = sol_sw[1][1]
sol_fi = ode_rk45_tol(dH_dt, ode_init_state, 0.0, ode_ts, 1.0e-6, 1.0e-6, 1000,
population_comp[demo, year], t_birth_to_end_hunt,
hp_fi[demo], hp_sw[demo], mu_m[demo])
exp_hunted_fi[demo] = sol_fi[1][1]
end
transition_matrix = create_transition_matrix(
exp_hunted_sw, exp_hunted_fi, population_comp[:, year],
hp_sw, hp_fi, t_birth_to_end_hunt, S_diag)
expected_fate = to_matrix(transition_matrix * population_comp[:, year], n_demo, 4)
noise_year = to_matrix(transition_noise_raw[:, year], n_demo, 3)
realized_fate::matrix[n_demo, 4]
for demo in 1:n_demo
realized_fate[demo, :] = multinomial_allocation(
expected_fate[demo, :], noise_year[demo, :], population_comp[demo, year])
end
survivors[:, year] = realized_fate[:, 1]
bycatch_expected[:, year] = realized_fate[:, 2]
hunted_sweden[:, year] = realized_fate[:, 3]
hunted_finland[:, year] = realized_fate[:, 4]
hunting_bag_total_sweden[year] = sum(hunted_sweden[:, year])
hunting_bag_total_finland[year] = sum(hunted_finland[:, year])
reproductive_probs[2, year] = birth_rate[year] * pi_s[year] * (1.0 - pi_c[year])
reproductive_probs[3, year] = birth_rate[year] * (1.0 - pi_s[year]) * pi_c[year] +
(1.0 - birth_rate[year]) * prob_of_ca * pi_c[year]
reproductive_probs[4, year] = birth_rate[year] * pi_s[year] * pi_c[year]
reproductive_probs[1, year] = 1.0 - sum(reproductive_probs[2:4, year])
end
(; birth_rate, pregnancy_rate, population_total,
hunted_sweden, hunted_finland, bycatch_expected,
hunting_bag_total_sweden, hunting_bag_total_finland, reproductive_probs)
end