pops-core  0.9
PoPS (Pest or Pathogen Spread) Model Core C++ library
hyperbolic_secant_kernel.hpp
Go to the documentation of this file.
1 /*
2  * PoPS model - hyperbolic secant 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_HYPERBOLIC_SECANT_KERNEL_HPP
17 #define POPS_HYPERBOLIC_SECANT_KERNEL_HPP
18 
19 #include "kernel_types.hpp"
20 #include "raster.hpp"
21 
22 #include <random>
23 
24 namespace pops {
25 
26 using std::cosh;
27 using std::tan;
28 using std::log;
29 
34 {
35 protected:
36  double sigma;
37  std::uniform_real_distribution<double> distribution;
38 
39 public:
40  HyperbolicSecantKernel(double s) : sigma(s), distribution(0.0, 1.0)
41  {
42  if (s == 0) {
43  throw std::invalid_argument("s cannot be 0.0");
44  }
45  }
46 
53  template<class Generator>
54  double random(Generator& generator)
55  {
56  double x = distribution(generator);
57  return icdf(x);
58  }
59 
66  double pdf(double x)
67  {
68  if (sigma == 1) {
69  return 0.5 * (1.0 / cosh((M_PI * x) / 2));
70  }
71  return (1.0 / (2 * sigma)) * (1 / cosh((M_PI * x) / (2 * sigma)));
72  }
73 
80  double icdf(double x)
81  {
82  if (x <= 0 || x >= 1) {
83  throw std::invalid_argument("icdf: x must be between 0.0 and 1.0");
84  }
85  if (sigma == 1) {
86  return (2.0 / M_PI) * log(tan(M_PI / 2.0 * x));
87  }
88  return ((log(tan((x * M_PI) / 2.0)) * (2.0 * sigma)) / M_PI);
89  }
90 };
91 
92 } // namespace pops
93 
94 #endif // POPS_HYPERBOLIC_SECANT_KERNEL_HPP
raster.hpp
pops::HyperbolicSecantKernel::icdf
double icdf(double x)
Hyperbolic secant inverse cumulative distribution (quantile) function Used by DeterministicKernel to ...
Definition: hyperbolic_secant_kernel.hpp:80
M_PI
#define M_PI
Definition: utils.hpp:35
pops::HyperbolicSecantKernel::HyperbolicSecantKernel
HyperbolicSecantKernel(double s)
Definition: hyperbolic_secant_kernel.hpp:40
pops::HyperbolicSecantKernel::pdf
double pdf(double x)
Hyperbolic secant probability density function Used by DeterministicKernel to determine location of s...
Definition: hyperbolic_secant_kernel.hpp:66
pops::HyperbolicSecantKernel::random
double random(Generator &generator)
Returns random value from hyperbolic secant distribution Used by RadialKernel to determine location o...
Definition: hyperbolic_secant_kernel.hpp:54
pops
Definition: cauchy_kernel.hpp:25
pops::HyperbolicSecantKernel::sigma
double sigma
Definition: hyperbolic_secant_kernel.hpp:36
pops::HyperbolicSecantKernel::distribution
std::uniform_real_distribution< double > distribution
Definition: hyperbolic_secant_kernel.hpp:37
pops::HyperbolicSecantKernel
Dispersal kernel for hyperbolic secant class utilized by RadialKernel and DeterministicKernel.
Definition: hyperbolic_secant_kernel.hpp:33
kernel_types.hpp
Kernel types enum and helper functions.