1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511
|
namespace quetzal {
namespace demography {
namespace strategy {
/*!
* \brief Traits class for individual based demographic history simulation, strategy suited for small number of individuals in the landscape (small populations).
* \ingroup demography
*
* \details Simulate the demographic history with an individual-based strategy: each
* individual is dispersed individually.
* \par History Initialization:
* The population size history\f$N\f$ is initialized by setting \f$N(.,t_0)\f$, the initial distribution of individuals across demes at the
* first time of the history \f$t_0\f$. Typically for a biological invasion,
* this is restricted to the introduction site(s) with the number of introduced
* individuals. For endemic species, paleoclimatic distribution can be considered
* as starting points.
*
* \par Growth:
* The offspring number \f$ \tilde{N}_{x}^{t} \f$ in each deme is freely defined by the user. It can for example be
* sampled in a distribution conditionally to a function of the local density of
* parents. Non-overlapping generations are considered (the parents die just after reproduction).
*
* \par Dispersal:
* The children dispersal is done by sampling their destination in a multinomial
* law, that defines \f$ \Phi_{x,y}^t \f$, the number of individuals going from
* \f$x\f$ to \f$y\f$ at time \f$t\f$:
*
* \f[ (\Phi_{x,y}^{t})_{y\in X} \sim \mathcal{M}(\tilde{N}_{x}^{t},(m_{xy})_y) ~. \f]
*
* The term \f$ (m_{xy})_y \f$ denotes the parameters of the multinomial law,
* giving for an individual in \f$x\f$ its proability to go to \f$y\f$.
* These probabilities are given by the dispersal law with parameter \f$\theta\f$:
*
* \f[
* \begin{array}{cclcl}
* m & : & X^2 & \mapsto & R_{+} \\
* & & (x,y) & \mapsto & m^{\theta}(x,y) ~. \\
* \end{array}
* \f]
*
* After migration, the number of individuals in deme \f$x\f$ is defined by
* the total number of individuals converging to \f$x\f$:
*
* \f[
* N(x,t+1) = \displaystyle \sum_{i\in X} \Phi_{i,x}^{t}~.
* \f]
* \section Example
* \snippet demography/test/History/Individual_based_history_test.cpp Example
* \section Output
* \include demography/test/History/Individual_based_history_test.output
*/
struct individual_based {
using value_type = unsigned int;
};
/*!
* \brief Traits class for simulating the demographic history of importnat masses of populations.
* \ingroup demography
*
* \details Simulate the demographic history by considering that populations of individuals are divisible masses,
* leading to faster simulations.
*
* \par Intialization:
* \f$N\f$ is initialized by setting \f$N(.,t_0)\f$ the initial distribution
* of individuals across demes at the first time \f$t_0\f$.
* Typically for a biological invasion, this is restricted to the introduction site(s)
* with the number of introduced individuals. For endemic species, paleoclimatic
* distribution can be considered as starting points.
*
* \par Growth:
* The offspring number \f$\tilde{N}_{x}^{t}\f$ in each deme is freely defined by
* the user. For example, it can be sampled in a distribution conditionally
* to a function of the local density of parents. Non-overlapping generations
* are considered (the parents die just after reproduction).
*
* \par Dispersal:
* The children dispersal is done by splitting the population masses according
* to their migration probabilities, defining
* \f$ \Phi_{x,y}^t \f$, the population flow going from \f$x\f$ to \f$y\f$ at time \f$t\f$:
* \f[ (\Phi_{x,y}^{t})_{y\in X} = (\tilde{N}_{x}^{t} \times m_{xy})_{y\in X} ~. \f]
* The term \f$ m_{xy} \f$ denotes the parameters of the transition kernel,
* giving for an individual in \f$x\f$ its probability to go to \f$y\f$.
* These probabilities are given by the dispersal law with parameter \f$\theta\f$:
* \f[
* \begin{array}{cclcl}
* m & : & X^2 & \mapsto & R_{+} \\
* & & (x,y) & \mapsto & m^{\theta}(x,y) ~. \\
* \end{array}
* \f]
* After migration, the population size in deme \f$x\f$ is defined by the sum of population flows converging to \f$x\f$:
* \f[
* N(x,t+1) = \displaystyle \sum_{i\in X} \Phi_{i,x}^{t}~.
* \f]
* \section Example
* \snippet demography/test/History/Mass_based_history_test.cpp Example
* \section Output
* \include demography/test/History/Mass_based_history_test.output
*/
struct mass_based {
using value_type = double;
};
}
/*!
* \brief Base class for spatially explicit and forward-in time population history simulators.
*
* \tparam Space Demes identifiers.
* \tparam Time EqualityComparable, CopyConstructible.
* \tparam Strategy Strategy use for simulating populations dynamics
*
* \details Is used as an implementation base of the specialized simulation strategies.
*
* \ingroup demography
*
*/
template<typename Space, typename Time, typename Strategy>
class BaseHistory {
public:
//! \typedef strategy used for simulating populations dynamics
using strategy_type = Strategy;
//! \typedef type of the population flows database
using flow_type = Flow<Space, Time, typename strategy_type::value_type>;
//! \typedef type of the population size database
using pop_sizes_type = PopulationSize<Space, Time, typename strategy_type::value_type>;
//! \typedef space type
using coord_type = Space;
//! \typedef time type
using time_type = Time;
//! \typedef type of the discrete distribution used inside the backward dispersal kernel
using discrete_distribution_type = quetzal::random::DiscreteDistribution<coord_type>;
//! \typedef Backward dispersal kernel type
using backward_kernel_type = quetzal::random::TransitionKernel<time_type, discrete_distribution_type>;
protected:
// Need to be accessed by the expand method
std::unique_ptr<pop_sizes_type> m_sizes;
std::unique_ptr<flow_type> m_flows;
std::vector<Time> m_times;
std::unique_ptr<backward_kernel_type> m_kernel;
private:
auto make_backward_distribution(coord_type const& x, time_type const& t) const
{
std::vector<double> weights;
std::vector<coord_type> support;
weights.reserve(m_flows->flux_to(x,t).size());
support.reserve(m_flows->flux_to(x,t).size());
for(auto const& it : m_flows->flux_to(x,t) )
{
support.push_back(it.first);
weights.push_back(static_cast<double>(it.second));
}
return discrete_distribution_type(std::move(support),std::move(weights));
}
public:
/**
* \brief Constructor initializing the demographic database.
* \param x the coordinate of introduction
* \param t the introduction time
* \param N the population size at coordinate x at time t
* \section Example
* \snippet demography/test/History/History_test.cpp Example
* \section Output
* \include demography/test/History/History_test.output
*/
BaseHistory(coord_type const& x, time_type const& t, typename strategy_type::value_type N):
m_sizes(std::make_unique<pop_sizes_type>()),
m_flows(std::make_unique<flow_type>()),
m_kernel(std::make_unique<backward_kernel_type>())
{
m_sizes->operator()(x,t) = N;
m_times.push_back(t);
}
/**
* \brief Read-only access to the demographic flows database
*/
flow_type const& flows() const
{
return *m_flows;
}
/**
* \brief Read and write access to the demographic flows database
*/
flow_type & flows()
{
return *m_flows;
}
/**
* \brief Read-only access to the demographic sizes database.
* \remark Can be used for composition into time dependent growth functions.
*/
const pop_sizes_type & pop_sizes() const
{
return *m_sizes;
}
/**
* \brief Read-and-write access to the demographic sizes database
*/
pop_sizes_type & pop_sizes()
{
return *m_sizes;
}
/**
* \brief First time recorded in the foward-in-time database history.
*/
time_type const& first_time() const
{
return m_times.front();
}
/**
* \brief Last time recorded in the foward-in-time database history.
*/
time_type const& last_time() const
{
return m_times.back();
}
/**
* \brief Samples a coordinate from the backward-in-time transition matrix
*
* \details The transition matrix is computed from the demographic flows
* database. The returned coordinate object will basically answer the question:
* when an individual is found in \f$x\f$ at time \f$t\f$, where could it
* have been at time \f$t-1\f$ ?
* Let \f$ X \f$ be the geographic space and \f$\Phi_{x,y}^t\f$ be the number of individuals going
* from coordinate \f$x \in X \f$ to coordinate \f$y\f$ at time \f$t\f$.
* Knowing that a child node \f$c\f$ is found in \f$ j \in X \f$, the probability for its parent
* \f$p\f$ to be in \f$i\in X \f$ is: \f$ P( p \in i ~|~ e \in j) = \frac{\Phi_{i, j}^{t}}{ \sum_{k} \Phi_{k, j}^{t} } ~.\f$
* \section Example
* \snippet demography/test/History/Mass_based_history_test.cpp Example
* \section Output
* \include demography/test/History/Mass_based_history_test.output
*/
template<typename Generator>
coord_type backward_kernel(coord_type const& x, time_type t, Generator& gen)
{
--t;
assert(m_flows->flux_to_is_defined(x,t));
if( ! m_kernel->has_distribution(x, t))
{
m_kernel->set(x, t, make_backward_distribution(x, t));
}
return m_kernel->operator()(gen, x, t);
}
};
/**
* \brief Demographic history simulated from an individual-based strategy (each individual is dispersed individually).
*
* \tparam Space Demes identifiers.
* \tparam Time EqualityComparable, CopyConstructible.
* \tparam Strategy Strategy used for simulating populations dynamics
*
* \details Inherit from this class and specialize the Strategy template parameter
*
* \ingroup demography
*
*/
template<typename Space, typename Time, typename Strategy>
class History : public BaseHistory<Space, Time, Strategy>
{
};
/**
* \brief Demographic history simulated from an individual-based strategy (each individual is dispersed individually).
*
* \tparam Space Demes identifiers.
* \tparam Time EqualityComparable, CopyConstructible.
*
* \ingroup demography
*
* \details $N$ is initialized by setting \f$ N(.,t_0) \f$ the initial distribution
* of individuals across demes at the first time \f$ t_0 \f$.
* Typically for a biological invasion, this is restricted to the introduction site(s)
* with the number of introduced individuals. For endemic species, paleoclimatic
* distribution can be considered as starting points. The number of descendants
* \f$ \tilde{N}_{x}^{t} \f$ in each deme is sampled in a distribution conditionally
* to a function of the the local density of parents. Non-overlapping generations
* are considered (the parents die just after reproduction). The children dispersal
* is done by sampling their destination in a multinomial law, that defines
* \f$ \Phi_{x,y}^t \f$ the number of individuals going from \f$ x \f$ to \f$ y \f$ at time \f$ t \f$:
* \f[ (\Phi_{x,y}^{t})_{y\in X} \sim \mathcal{M}(\tilde{N}_{x}^{t},(m_{xy})_y) ~. \f]
* The term \f$ (m_{xy})_y \f$ denotes the parameters of the multinomial law,
* giving for an individual in \f$x\f$ its proability to go to \f$y\f$.
* These probabilities are given by the dispersal law with parameter \f$\theta\f$:
* \f[
* \begin{array}{cclcl}
* m & : & X^2 & \mapsto & R_{+} \\
* & & (x,y) & \mapsto & m^{\theta}(x,y) ~. \\
* \end{array}
* \f]
* After migration, the number of individuals in deme \f$x\f$ is defined by the total number of individuals converging to \f$x\f$:
* \f[
* N(x,t+1) = \displaystyle \sum_{i\in X} \Phi_{i,x}^{t}~.
* \f\]
*
* \section Example
* \snippet demography/test/History/History_test.cpp Example
* \section Output
* \include demography/test/History/History_test.output
*/
template<typename Space, typename Time>
class History<Space, Time, strategy::individual_based> : public BaseHistory<Space, Time, strategy::individual_based>
{
using BaseHistory<Space, Time, strategy::individual_based>::BaseHistory;
public:
/**
* \brief Expands the demographic database.
*
* \tparam Space Demes identifiers.
* \tparam Time EqualityComparable, CopyConstructible.
*
* \exception std::domain_error if the population goes extincted before the simulation is completed.
*
* \param nb_generations the number of generations to simulate
* \param sim_growth a functor simulating \f$\tilde{N}_{x}^{t}\f$.
*
* The functor can possibly internally use a reference on the population sizes database to represent the time dependency.
* The signature of the function should be equivalent to the following:
* `unsigned int sim_growth(Generator &gen, const coord_type &x, const time_type &t);`
*
* \param kernel a functor representing the dispersal location kernel that simulates the coordinate of the next location conditionally to the current location.
* The signature should be equivalent to `coord_type kernel(Generator &gen, const coord_type &x, const time_type &t);`
*
* \section Example
* \snippet demography/test/History/History_test.cpp Example
* \section Output
* \include demography/test/History/History_test.output
*/
template<typename Growth, typename Dispersal, typename Generator>
void expand(unsigned int nb_generations, Growth sim_growth, Dispersal kernel, Generator& gen)
{
for(unsigned int g = 0; g < nb_generations; ++g)
{
auto t = this->last_time();
auto t_next = t; ++ t_next;
this->m_times.push_back(t_next);
unsigned int landscape_individuals_count = 0;
// TODO : optimize the definition_space function (for loop)
for(auto x : this->m_sizes->definition_space(t) )
{
auto N_tilde = sim_growth(gen, x, t);
landscape_individuals_count += N_tilde;
if(N_tilde >= 1)
{
for(unsigned int ind = 1; ind <= N_tilde; ++ind)
{
auto y = kernel(gen, x, t);
this->m_flows->add_to_flux_from_to(x, y, t, 1);
this->m_sizes->operator()(y, t_next) += 1;
}
}
}
if(landscape_individuals_count == 0)
{
throw std::domain_error("Landscape populations went extinct before sampling");
}
}
}
};
/**
* \brief Demographic history where populations levels are assumed high enough to be considered as divisible masses.
*
* \ingroup demography
*
* \tparam Space Demes identifiers.
* \tparam Time EqualityComparable, CopyConstructible.
*
* \details $N$ is initialized by setting \f$N(.,t_0)\f$ the initial distribution
* of individuals across demes at the first time \f$t_0\f$.
* Typically for a biological invasion, this is restricted to the introduction site(s)
* with the number of introduced individuals. For endemic species, paleoclimatic
* distribution can be considered as starting points. The number of descendants
* \f$\tilde{N}_{x}^{t}\f$ in each deme is sampled in a distribution conditionally
* to a function of the the local density of parents. Non-overlapping generations
* are considered (the parents die just after reproduction). The children dispersal
* is done by sampling their destination in a multinomial law, that defines
* \f$ \Phi_{x,y}^t \f$ the number of individuals going from \f$x\f$ to \f$y\f$ at time \f$t\f$:
* \f[ (\Phi_{x,y}^{t})_{y\in X} \sim \mathcal{M}(\tilde{N}_{x}^{t},(m_{xy})_y) ~. \f]
* The term \f$ (m_{xy})_y \f$ denotes the parameters of the multinomial law,
* giving for an individual in \f$x\f$ its probability to go to \f$y\f$.
* These probabilities are given by the dispersal law with parameter \f$\theta\f$:
* \f[
* \begin{array}{cclcl}
* m & : & X^2 & \mapsto & R_{+} \\
* & & (x,y) & \mapsto & m^{\theta}(x,y) ~. \\
* \end{array}
* \f]
* After migration, the number of individuals in deme \f$x\f$ is defined by the total number of individuals converging to \f$x\f$:
* \f[
* N(x,t+1) = \displaystyle \sum_{i\in X} \Phi_{i,x}^{t}~.
* \f\]
*
* \section Example
* \snippet demography/test/History/History_test.cpp Example
* \section Output
* \include demography/test/History/History_test.output
*/
template<typename Space, typename Time>
class History<Space, Time, strategy::mass_based> : public BaseHistory<Space, Time, strategy::mass_based>{
using BaseHistory<Space, Time, strategy::mass_based>::BaseHistory;
public:
/**
* \brief Expands the demographic database,
* \details $N$ is initialized by setting \f$N(.,t_0)\f$ the initial distribution
* of individuals across demes at the first time \f$t_0\f$.
* Typically for a biological invasion, this is restricted to the introduction site(s)
* with the number of introduced individuals. For endemic species, paleoclimatic
* distribution can be considered as starting points. The number of descendants
* \f$\tilde{N}_{x}^{t}\f$ in each deme is sampled in a distribution conditionally
* to a function of the the local density of parents. Non-overlapping generations
* are considered (the parents die just after reproduction). The children dispersal
* is done by sampling their destination in a multinomial law, that defines
* \f$ \Phi_{x,y}^t \f$ the population flow going from \f$x\f$ to \f$y\f$ at time \f$t\f$:
* \f[ (\Phi_{x,y}^{t})_{y\in X} = (\tilde{N}_{x}^{t}*m_{xy})_{y\in X} ~. \f]
* The term \f$ m_{xy} \f$ denotes the parameters of the transition kernel,
* giving for an individual in \f$x\f$ its probability to go to \f$y\f$.
* These probabilities are given by the dispersal law with parameter \f$\theta\f$:
* \f[
* \begin{array}{cclcl}
* m & : & X^2 & \mapsto & R_{+} \\
* & & (x,y) & \mapsto & m^{\theta}(x,y) ~. \\
* \end{array}
* \f]
* After migration, the population size in deme \f$x\f$ is defined by the sum of population flows converging to \f$x\f$:
* \f[
* N(x,t+1) = \displaystyle \sum_{i\in X} \Phi_{i,x}^{t}~.
* \f\]
* \param nb_generations the number of generations to simulate
* \param sim_growth a functor simulating \f$\tilde{N}_{x}^{t}\f$.
* The functor can possibly internally use a reference on the population sizes database to represent the time dependency.
* The signature of the function should be equivalent to the following:
* `double sim_growth(Generator &gen, const coord_type &x, const time_type &t);`
* \param kernel a functor representing the dispersal location kernel that simulates the coordinate of the next location conditionally to the current location.
* The signature should be equivalent to `coord_type kernel(Generator &gen, const coord_type &x, const time_type &t);`.
* The expression `kernel.support(time_type const& t)` must be valid and return an iterable container of geographic coordinates
* indicating the transition kernel state space at time \f$t\f$.
* \section Example
* \snippet demography/test/History/History_test.cpp Example
* \section Output
* \include demography/test/History/History_test.output
*/
template<typename Growth, typename Dispersal, typename Generator>
void expand(unsigned int nb_generations, Growth sim_growth, Dispersal kernel, Generator& gen)
{
for(unsigned int g = 0; g < nb_generations; ++g)
{
auto t = this->last_time();
auto t_next = t; ++ t_next;
this->m_times.push_back(t_next);
for(auto x : this->m_sizes->definition_space(t) )
{
auto N_tilde = sim_growth(gen, x, t);
for(auto y : kernel.state_space(t) )
{
auto m = kernel(x, y, t);
this->m_flows->set_flux_from_to(x, y, t, m*N_tilde);
this->m_sizes->operator()(y, t_next) += m*N_tilde;
}
}
}
}
};
} // namespace demography
} // namespace quetzal
#endif |
Partager