pops-core  0.9
PoPS (Pest or Pathogen Spread) Model Core C++ library
power_law_kernel.hpp
Go to the documentation of this file.
1 /*
2  * PoPS model - power law dispersal kernel
3  *
4  * Copyright (C) 2015-2020 by the authors.
5  *
6  * Authors: Margaret Lawrimore malawrim ncsu edu
7  *
8  * The code contained herein is licensed under the GNU General Public
9  * License. You may obtain a copy of the GNU General Public License
10  * Version 2 or later at the following locations:
11  *
12  * http://www.opensource.org/licenses/gpl-license.html
13  * http://www.gnu.org/copyleft/gpl.html
14  */
15 
16 #ifndef POPS_POWER_LAW_KERNEL_HPP
17 #define POPS_POWER_LAW_KERNEL_HPP
18 
19 #include "kernel_types.hpp"
20 #include "raster.hpp"
21 
22 #include <random>
23 
24 namespace pops {
25 
26 using std::pow;
27 
32 {
33 protected:
34  double alpha;
35  double xmin;
36  std::uniform_real_distribution<double> distribution;
37 
38 public:
39  PowerLawKernel(double a, double xm) : alpha(a), xmin(xm), distribution(0.0, 1.0)
40  {
41  // if (alpha < 1.0) {
42  // throw std::invalid_argument("alpha must be greater than or equal
43  // to 1.0");
44  // }
45  if (xmin == 0) {
46  throw std::invalid_argument("xmin cannot equal 0.0");
47  }
48  }
49 
56  template<class Generator>
57  double random(Generator& generator)
58  {
59  double x = distribution(generator);
60  return icdf(x);
61  }
62 
70  double pdf(double x)
71  {
72  if (x < 0) {
73  throw std::invalid_argument("x cannot be less than 0.0");
74  }
75  // x = distance to center and is always 0 for the center cell
76  // so have to add xmin to account for minimum x values
77  x = x + xmin;
78  if (x < xmin) {
79  throw std::invalid_argument("x must be greater than or equal to xmin");
80  }
81  return ((alpha - 1.0) / xmin) * pow(x / xmin, -alpha);
82  }
83 
90  double icdf(double x)
91  {
92  if (x <= 0 || x >= 1) {
93  throw std::invalid_argument("icdf: x must be between 0 and 1.0");
94  }
95  return pow(x / xmin, (-alpha + 1.0));
96  }
97 };
98 
99 } // namespace pops
100 
101 #endif // POPS_POWER_LAW_KERNEL_HPP
pops::PowerLawKernel::xmin
double xmin
Definition: power_law_kernel.hpp:35
raster.hpp
pops::PowerLawKernel::distribution
std::uniform_real_distribution< double > distribution
Definition: power_law_kernel.hpp:36
pops::PowerLawKernel::icdf
double icdf(double x)
Power law inverse cumulative distribution (quantile) function Used by DeterministicKernel to determin...
Definition: power_law_kernel.hpp:90
pops
Definition: cauchy_kernel.hpp:25
pops::PowerLawKernel
Dispersal kernel for power law distribution class utilized by RadialKernel and DeterministicKernel.
Definition: power_law_kernel.hpp:31
pops::PowerLawKernel::random
double random(Generator &generator)
Returns random value from power law distribution Used by RadialKernel to determine location of spread...
Definition: power_law_kernel.hpp:57
pops::PowerLawKernel::pdf
double pdf(double x)
Power law probability density function Used by DeterministicKernel to determine location of spread.
Definition: power_law_kernel.hpp:70
pops::PowerLawKernel::alpha
double alpha
Definition: power_law_kernel.hpp:34
pops::PowerLawKernel::PowerLawKernel
PowerLawKernel(double a, double xm)
Definition: power_law_kernel.hpp:39
kernel_types.hpp
Kernel types enum and helper functions.