How to use probability functions in C?
Probability functions play a very important role in data science and statistics. In the C programming language, we can use functions in its standard library to implement various probability calculations. This article will introduce how to use probability functions in C to perform common probability calculations.
The C standard library provides a header file named "cmath", which contains many functions related to mathematical calculations, including probability functions. Before using probability functions, we first need to understand some concepts and terminology.
The following are some common probability functions and usage examples:
Normal distribution function:
The normal distribution is a common probability distribution , represented using the "normal_distribution" class.
#include#include #include int main() { std::random_device rd; std::mt19937 gen(rd()); std::normal_distribution dist(0.0, 1.0); double x = 2.0; double p = std::exp(-std::pow(x, 2) / 2) / std::sqrt(2 * M_PI); double cdf = std::erfc(-x / std::sqrt(2)) / 2; std::cout << "PDF: " << p << std::endl; std::cout << "CDF: " << cdf << std::endl; return 0; }
In this example, we first define a normal distribution with a mean of 0 and a standard deviation of 1 through the "normal_distribution" class. Then, we calculated the probability density and cumulative distribution at x=2.0.
Binomial distribution function:
The binomial distribution is used to describe the probability of success in a fixed number of repeated trials. Represented using the "binomial_distribution" class.
#include#include int main() { std::random_device rd; std::mt19937 gen(rd()); std::binomial_distribution dist(10, 0.5); int k = 5; double p = std::tgamma(11) / (std::tgamma(6) * std::tgamma(5)) * std::pow(0.5, 5) * std::pow(0.5, 5); double cdf = 0; for (int i = 0; i <= k; i++) { cdf += std::tgamma(11) / (std::tgamma(i + 1) * std::tgamma(11 - i)) * std::pow(0.5, i) * std::pow(0.5, 11 - i); } std::cout << "PDF: " << p << std::endl; std::cout << "CDF: " << cdf << std::endl; return 0; }
In this example, we first define a binomial distribution repeated 10 times with a success probability of 0.5 through the "binomial_distribution" class. Then, we calculated the probability density and cumulative distribution of k = 5 successes.
Through the above example, we can see how to use the probability function in C to perform common probability calculations. In practical applications, sometimes we also need to deal with other types of probability distribution functions, such as Poisson distribution, exponential distribution, etc. The C standard library also provides corresponding classes to handle these distributions. In practice, we can choose a suitable distribution function for calculation according to specific needs.
To summarize, the probability function in C provides rich functions through the mathematical functions of the standard library, which can help us perform various probability calculations. Using these functions, common probability density calculations, cumulative distribution calculations, random number generation, etc. can be easily implemented. This provides us with great convenience in the fields of scientific computing and data analysis.
The above is the detailed content of How to use probability functions in C++?. For more information, please follow other related articles on the PHP Chinese website!