泊松分布表(常用)

由于博主刚学到泊松分布,但当做题时需要查泊松分布表,奇怪的是比如λ=0.1,题目没有给相应数据,教材课后附表也没有给出,于是我萌生出自己编程实现泊松分布表的念头。
代码如下:
泊松分布公式:P{X=k}= C a b C_a^b Cab p k p^k pk ( 1 − p ) n − k (1-p)^{n-k} (1p)nk λ k k ! \frac{λ^k}{k!} k!λk e − λ e^{-λ} eλ

#include<bits/stdc++.h>
using namespace std;
long long f[100];
int main()
{
    cout<<"The program is to calculate P{X=k} of Poisson distribution"<<endl;
    cout<<"The k is no more than 20\n"<<endl;
    f[0]=1;
    for(int i=1;i<=20;i++){
        f[i]=f[i-1]*i;
    }
    double lambda,ans;
    int k;
    while(1){
    cout<<"lambda="; cin>>lambda;
    cout<<"k="; cin>>k;
    ans=pow(lambda,k)*exp(-lambda)/f[k];
    printf("P{X=%d}=%.7lf\n\n",k,ans);
    }
    return 0;
}

P{X≥x} =1-F(x-1)= ∑ r = x ∞ e − λ λ r r ! \sum_{r=x}^∞\frac{e^{-λ}λ^r}{r!} r=xr!eλλr

#include<bits/stdc++.h>
using namespace std;
long long f[100];
int k_recommend(double x){
   if(0.1<=x&&x<=1.0) return 10;
   if(1.0<x&&x<3) return 15;
   if(3<=x&&x<=5) return 20;
   return 0;
}
int main()
{
    cout<<"The program is to calculate P{X>=k} of Poisson distribution"<<endl;
    cout<<"The k is no more than 20"<<endl;
    cout<<"The lambda can only be 0.1<=lambda<=5.0"<<endl;
    cout<<"When you input a lambda, the program will give you the value of k automatically"<<endl;
    f[0]=1;
    for(int i=1;i<=20;i++){
        f[i]=f[i-1]*i;
    }
    double lambda,p,ans[100];
    int k,kk;
    while(1){
    memset(ans,0,sizeof ans);
    cout<<"Input:\n"<<"lambda="; cin>>lambda;///input lambda
    if(k=k_recommend(lambda)){
        kk=k;
    }
    else {cout<<"Your lambda are out of order, please input again!\n"<<endl; continue;}
    cout<<"k="<<kk<<endl;
    while(k--){
        p=pow(lambda,k)*exp(-lambda)/f[k];
        ans[k]=p+ans[k+1];
    }
    cout<<"Output:"<<endl;
    for(int i=0;i<=kk-1;i++)
        printf("P{X>=%d}=%.7lf\n",i,ans[i]);///P{X>=k} =ans[k];
        cout<<endl;
    }
    return 0;
}

以上代码可能会有许多不足之处,敬请各位大佬批评指正!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值