A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/de/d72/geometric__dist_8cpp_source.html below:

TheAlgorithms/C++: probability/geometric_dist.cpp Source File

49 return static_cast<float>

(rand()) /

static_cast<float>

(RAND_MAX);

94 return

std::pow((1.0f -

p

),

static_cast<float>

(k - 1)) *

p

;

105 return

1.0f - std::pow((1.0f -

p

),

static_cast<float>

(k));

117 return

std::log(1.0f - cdf) / std::log(1.0f -

p

);

127 return static_cast<

uint32_t

>

(

146 const

uint32_t& max_tries =

147

std::numeric_limits<uint32_t>::max())

const

{

149 float

cdf_upper = max_tries == std::numeric_limits<uint32_t>::max()

152 return

cdf_upper - cdf_lower;

167

uint32_t n_tries = 1000000;

168

std::vector<float> tries;

169

tries.resize(n_tries);

172 for

(uint32_t i = 0; i < n_tries; ++i) {

173

tries[i] =

static_cast<float>

(dist.

draw_sample

());

177

mean /=

static_cast<float>

(n_tries);

180 for

(uint32_t i = 0; i < n_tries; ++i) {

181

var += (tries[i] - mean) * (tries[i] - mean);

185

var /=

static_cast<float>

(n_tries - 1);

187

std::cout <<

"This value should be near "

<< dist.

expected_value

() <<

": " 188

<< mean << std::endl;

189

std::cout <<

"This value should be near "

<< dist.

variance

() <<

": "

<< var

200 const float

threshold = 1e-3f;

202

std::cout <<

"Starting tests for p = 0.3..."

<< std::endl;

203

assert(std::abs(dist.

expected_value

() - 3.33333333f) < threshold);

204

assert(std::abs(dist.

variance

() - 7.77777777f) < threshold);

211

assert(std::abs(dist.

range_tries

() - 1.0f) < threshold);

212

assert(std::abs(dist.

range_tries

(3) - 0.49f) < threshold);

213

assert(std::abs(dist.

range_tries

(5, 11) - 0.2203267f) < threshold);

214

std::cout <<

"All tests passed"

<< std::endl;

219

std::cout <<

"Starting tests for p = 0.5..."

<< std::endl;

221

assert(std::abs(dist.

variance

() - 2.0f) < threshold);

228

assert(std::abs(dist.

range_tries

() - 1.0f) < threshold);

229

assert(std::abs(dist.

range_tries

(3) - 0.25f) < threshold);

230

assert(std::abs(dist.

range_tries

(5, 11) - 0.062011f) < threshold);

231

std::cout <<

"All tests passed"

<< std::endl;

236

std::cout <<

"Starting tests for p = 0.8..."

<< std::endl;

238

assert(std::abs(dist.

variance

() - 0.3125f) < threshold);

245

assert(std::abs(dist.

range_tries

() - 1.0f) < threshold);

246

assert(std::abs(dist.

range_tries

(3) - 0.04f) < threshold);

247

assert(std::abs(dist.

range_tries

(5, 11) - 0.00159997f) < threshold);

248

std::cout <<

"All tests have successfully passed!"

<< std::endl;

257

srand(time(

nullptr

));

A class to model the geometric distribution.

float cumulative_distribution(const uint32_t &k) const

The cumulative distribution function.

float standard_deviation() const

The standard deviation of a geometrically distributed random variable X.

float expected_value() const

The expected value of a geometrically distributed random variable X.

float range_tries(const uint32_t &min_tries=1, const uint32_t &max_tries=std::numeric_limits< uint32_t >::max()) const

This function computes the probability to have success in a given range of tries.

uint32_t draw_sample() const

Generates a (discrete) sample according to the geometrical distribution.

geometric_distribution(const float &p)

Constructor for the geometric distribution.

float inverse_cumulative_distribution(const float &cdf) const

The inverse cumulative distribution function.

float p

The succes probability p.

float variance() const

The variance of a geometrically distributed random variable X.

float probability_density(const uint32_t &k) const

The probability density function.

void sample_test(const probability::geometric_dist::geometric_distribution &dist)

Tests the sampling method of the geometric distribution.

float generate_uniform()

Returns a random number between [0,1].

static void test()

Self-test implementations.

int main()

Main function.

Functions for the Geometric Distribution algorithm implementation.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4