C 中泊松分布的计算

发布于 2024-07-25 06:27:50 字数 50 浏览 4 评论 0原文

我需要一个 C 函数来计算 k 值高达 720 的泊松分布。我需要一个高效的解决方案。

I need a C function to calculate Poisson distribution for values of k up to 720. I need a highly efficient solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

慢慢从新开始 2024-08-01 06:27:50

泊松随机生成器

int poissonRandom(double expectedValue) {
  int n = 0; //counter of iteration
  double limit; 
  double x;  //pseudo random number
  limit = exp(-expectedValue);
  x = rand() / INT_MAX; 
  while (x > limit) {
    n++;
    x *= rand() / INT_MAX;
  }
  return n;
}

我想我对您的紧急需求来说已经很晚了。

Poisson Random Generator

int poissonRandom(double expectedValue) {
  int n = 0; //counter of iteration
  double limit; 
  double x;  //pseudo random number
  limit = exp(-expectedValue);
  x = rand() / INT_MAX; 
  while (x > limit) {
    n++;
    x *= rand() / INT_MAX;
  }
  return n;
}

I guess I'm pretty late for your urgent demand.

一直在等你来 2024-08-01 06:27:50

如果你想自己计算而不是使用库
您可以使用公式计算它.. e^k*e^(-lambda)/k!
您可以使用 log(n!) = log(n)+log(n-1!) 和动态规划

If you want to calculate it yourself instead of using a library
You can calculate it using the formula.. e^k*e^(-lambda)/k!
you can use log(n!) = log(n)+log(n-1!) and dynamic programming

是你 2024-08-01 06:27:50

我想这对于最初的请求来说太晚了,但我认为有些答案没有抓住重点——我不认为他想从分布中生成随机数,而是想要分布本身。 这是一个执行此操作的函数
避免计算可能变大的阶乘。

double poisson( int k, double mean ) { 
  double p = std::exp(-mean);
  double f = 1;
  for ( int i=0 ; i<k ; i++ ) f *= mean/(i+1);     
  return p*f;
}

I guess this is far too late for the original request, but I think some of the answers miss the point - I don't think he wants to generate random numbers from a distribution, but wants the distribution itself. Here is a function to do this
avoiding the calculation of factorials which can become large.

double poisson( int k, double mean ) { 
  double p = std::exp(-mean);
  double f = 1;
  for ( int i=0 ; i<k ; i++ ) f *= mean/(i+1);     
  return p*f;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文