新浪云虚拟主机做电影网站,用什么l软件做网站了,乱码网站怎么办,七牛 wordpress 媒体python/C 生成beta分布的随机数 文章目录 python/C 生成beta分布的随机数前言一、beta分布理论知识二、python 生成服从beta分布的随机数三、C语言生成服从beta分布的随机数 前言
想把一个算法用C语言实现#xff0c;其中涉及到了beta分布取随机数#xff0c;记录一下结果 一…python/C 生成beta分布的随机数 文章目录 python/C 生成beta分布的随机数前言一、beta分布理论知识二、python 生成服从beta分布的随机数三、C语言生成服从beta分布的随机数 前言
想把一个算法用C语言实现其中涉及到了beta分布取随机数记录一下结果 一、beta分布理论知识
参考博客 Beta分布及其应用
Beta分布是一个定义在[0,1]区间上的连续概率分布族它有两个正值参数称为形状参数一般用α和β表示。在贝叶斯推断中Beta分布是Bernoulli、二项分布、负二项分布和几何分布的共轭先验分布。Beta分布的概率密度函数形式如下
定义域[0,1]参数α , β 均为正值参数又称为形状参数
Beta分布的概率密度函数: 其中 Γ ( x ) \Gamma(x) Γ(x)为gamma函数 B ( α , β ) B(\alpha,\beta) B(α,β)为beta函数 Beta分布的均值 α α β \frac{\alpha}{\alpha\beta} αβα Beta分布的方差 α β ( α β ) 2 ( α β 1 ) \frac{\alpha\beta}{(\alpha\beta)^2(\alpha\beta1)} (αβ)2(αβ1)αβ Beta分布的图形
从Beta分布的概率密度函数的图形我们可以看出Beta分布有很多种形状但都是在0-1区间内因此Beta分布可以描述各种0-1区间内的形状事件。因此它特别适合为某件事发生或者成功的概率建模。同时当α1β1的时候它就是一个均匀分布。 beta分布主要有 α和 β两个参数这两个参数决定了分布的形状从上图及其均值和方差的公式可以看出 1α/(αβ)也就是均值其越大概率密度分布的中心位置越靠近1依据此概率分布产生的随机数也多说都靠近1反之则都靠近0。 2αβ越大则分布越窄也就是集中度越高这样产生的随机数更接近中心位置从方差公式上也能看出来。 二、python 生成服从beta分布的随机数
参考文章
一个满足beta分布的随机变量可以被两个服从Gamma分布的独立随机变量X,Y推导得到
P.S. python是有相应的库函数可以用来生成beta分布随机数scipy.stats.beta下面的代码是通过数值近似从均匀分布随机数给出beta分布随机数的代码 def do_Beta_Universality_Uniform(self)-np.ndarray:Creates an array with samples from the desired beta(a,b) distribution.Returns-------beta_rv : numpy.ndarrayan array with samples from the desired beta(a,b) distribution.# 1) Create a and b many Expos by drawing n samples# from the Uniform distr. and plugging them into F^(1) of the#Expo (-log(1-U))n self.number_of_simulationsX []for i in np.arange(self.a): #number 3 of plan of attackuniform_samples uniform().rvs(sizen) #number 1 and 2 of plan of attackX.append((-1*np.log(1-uniform_samples)))Y []for i in np.arange(self.b): #number 4 of plan of attackuniform_samples uniform(0, 1).rvs(sizen) #number 1 and 2 of plan of attackY.append(-1*np.log(1-uniform_samples))#5 of plan of attack - create Gamma(a,1) from Expos by summing all #the a uniforms samplesX np.array(X)X X.sum(axis0)#6 of plan of attack - create Gamma(b,1) from Expos by summing all #the b uniform samplesY np.array(Y)Y Y.sum(axis0)#7 of plan of attack -the final Beta r.v. is X/(XY)beta_rv X/(XY)return beta_rv三、C语言生成服从beta分布的随机数
算法是一样的只不过C语言没有生成均匀分布的随机数的库函数需要从rand()生成正态分布随机数出发
#include stdio.h
#include stdlib.h
#include time.h
#include math.h/* 函数功能 产生0,1区间上均匀分布的随机数输入参数说明0 给定区间的下限1 给定区间的上线seed 长整型指针变量 *seed 为伪随机数的种子
*/
double uniform_data(long int * seed)
{double t;*seed 2045.0 * (*seed) 1;*seed *seed - (*seed / 1048576) * 1048576;t (*seed) / 1048576.0;return t;
}// 生成贝塔分布随机数
double rand_beta_dist(double alpha, double beta, long int* s) {// 生成服从 [0, 1] 均匀分布的随机数double x0,y0;double tmp10;for(int i0; ialpha; i){tmp1 uniform_data(s);// printf(%f\n, tmp1);x x (-log(1-tmp1));}double tmp20;for(int i0; ibeta; i){tmp2 uniform_data(s);// printf(%f\n, tmp2);y y (-log(1-tmp1));}// 生成贝塔分布随机数return x / (x y);
}int main() {srand((unsigned int)time(NULL));double alpha 2.0; // 根据需要更改double beta 200.0; // 根据需要更改long int s;s 1000;printf(%f\n, log(10));for (int i 0; i 10; i) {double sample rand_beta_dist(alpha, beta, s);printf(%f\n, sample);}return 0;
}