NumCpp  1.0
A C++ implementation of the Python Numpy library
negativeBinomial.hpp
Go to the documentation of this file.
1 #pragma once
30 
31 #include "NumCpp/NdArray.hpp"
32 #include "NumCpp/Core/Shape.hpp"
37 
38 #include "boost/random/negative_binomial_distribution.hpp"
39 
40 #include <string>
41 
42 namespace nc
43 {
44  namespace random
45  {
46  //============================================================================
47  // Method Description:
57  template<typename dtype>
58  dtype negativeBinomial(dtype inN, double inP = 0.5)
59  {
60  STATIC_ASSERT_INTEGER(dtype);
61 
62  if (inN < 0)
63  {
64  THROW_INVALID_ARGUMENT_ERROR("input number of trials must be greater than or equal to zero.");
65  }
66 
67  if (inP < 0 || inP > 1)
68  {
69  THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
70  }
71 
72  const boost::random::negative_binomial_distribution<dtype, double> dist(inN, inP);
73  return dist(generator_);
74  }
75 
76  //============================================================================
77  // Method Description:
89  template<typename dtype>
90  NdArray<dtype> negativeBinomial(const Shape& inShape, dtype inN, double inP = 0.5)
91  {
92  STATIC_ASSERT_INTEGER(dtype);
93 
94  if (inN < 0)
95  {
96  THROW_INVALID_ARGUMENT_ERROR("input number of trials must be greater than or equal to zero.");
97  }
98 
99  if (inP < 0 || inP > 1)
100  {
101  THROW_INVALID_ARGUMENT_ERROR("input probability of sucess must be of the range [0, 1].");
102  }
103 
104  NdArray<dtype> returnArray(inShape);
105 
106  const boost::random::negative_binomial_distribution<dtype, double> dist(inN, inP);
107 
108  stl_algorithms::for_each(returnArray.begin(), returnArray.end(),
109  [&dist](dtype& value) -> void
110  {
111  value = dist(generator_);
112  });
113 
114  return returnArray;
115  }
116  }
117 }
STATIC_ASSERT_INTEGER
#define STATIC_ASSERT_INTEGER(dtype)
Definition: StaticAsserts.hpp:41
StaticAsserts.hpp
Error.hpp
nc::stl_algorithms::for_each
void for_each(InputIt first, InputIt last, UnaryFunction f) noexcept
Definition: StlAlgorithms.hpp:214
generator.hpp
nc::NdArray< dtype >
NdArray.hpp
nc::random::negativeBinomial
dtype negativeBinomial(dtype inN, double inP=0.5)
Definition: negativeBinomial.hpp:58
nc::Shape
A Shape Class for NdArrays.
Definition: Core/Shape.hpp:41
nc::NdArray::end
iterator end() noexcept
Definition: NdArrayCore.hpp:1435
Shape.hpp
nc
Definition: Coordinate.hpp:45
nc::random::generator_
static std::mt19937_64 generator_
generator function
Definition: generator.hpp:40
THROW_INVALID_ARGUMENT_ERROR
#define THROW_INVALID_ARGUMENT_ERROR(msg)
Definition: Error.hpp:37
StlAlgorithms.hpp
nc::NdArray::begin
iterator begin() noexcept
Definition: NdArrayCore.hpp:1091