使用Boost C ++为一组数据生成指数分布 [英] Generate a exponential distribution for a set of data using boost c++
问题描述
如何使用boost c ++为一组数据生成指数分布.
How to generate a exponential distribution for a set of data using boost c++ .
我有一个向量,该向量包含名为 vararr 的浮点变量,并使用Boost c ++,我想对其进行拟合以实现指数分布并获取fit的相应速率参数(lambda).Matlab中的等效代码为:
I have a vector containing float variables named vararr and using Boost c++ and I want to fit it Exponential distribution and getting respective rate parameter(lambda) for the fit . The equivalance code in Matlab is :
PD = fitdist(vararr,'exponential');
vararr = sort(vararr);
fxx = pdf(PD,vararr);
我需要用C ++实现
推荐答案
指数分布用于在特定分布后生成随机数.要使用指数分布,请执行以下操作:
Exponential distribution is there to generate a random number following a certain distribution. To use exponential distribution, you do something like that:
#include <iostream>
#include <boost/random.hpp>
int main() {
boost::mt19937 seed(5u);
boost::variate_generator<boost::mt19937&, boost::exponential_distribution<>> random_n(seed, boost::exponential_distribution<>()) ;
cout << random_n() << endl; return 0;
}
现在,我怀疑您想做点什么,但对我来说还不太清楚.您想排序还是什么?在向量中选择一个随机数?
Now, I suspect you want to do something but it is not so clear for me. You want to sort or whatever? Pick a random number in your vector?
好的.简而言之,即使您可以使用C ++ 11,也无法走得太远.现在,大多数随机生成器都已成为标准配置.但不是PDF.如果您对原因感到好奇:此处为:http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1398.html .因此,您绝对需要boost.math工具包,这是计算PDF的唯一方法.这是为您提供的一些示例代码:
OK. In brief and short, even if you have access to C++11 you cannot go very far. Most of the random generator are now in the standard. But not the PDF. If you are curious about why: here it is: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1398.html. So you definitly need the boost.math toolkit, this is your only way to compute your PDF. Here is some sample code for you:
#include <boost/math/distributions/exponential.hpp>
int main() {
const auto lbda = 1.0;
const auto d = exponential_distribution<> { lbda };
cout << pdf(d, 0) << endl; // e(0) = 1
cout << pdf(d, 1) << endl; // e(-1) = 0.3678
return 0;
}
简而言之:对于使用boost PDF
,您本身就不需要矢量,因为PDF函数知道如何计算其分布.
In short: for using boost PDF
, you do not need per se the vector cause the PDF function knows how to compute its distribution.
因此,如果您需要使用某种分布填充向量,则可以简单地使用 std :: generate
并将其应用分布,这是一个带有 std的示例:: exponential_distribution
(c ++ 11,但您可以使用boost).
Edit 2: So if you need to populate a vector with some distribution, you can simply use std::generate
and apply the distribution to it, here is an example with a std::exponential_distribution
(c++11, but you can use boost).
std::vector<float> v(20);
random_device rd;
mt19937_64 gen(rd());
exponential_distribution<float> dis(1);
auto rand = bind(dis, gen);
generate(begin(v), end(v), rand);
for (auto& e : v) cout << e << endl;
我在这里显示的代码是用指数分布填充20个元素的 std :: array
,将float更改为所需的值,增加向量或数组的大小.
The code I am showing here is filling a vector
of 20 elements with an exponential distribution. You can switch to an std::array
, change the float to whatever you need, increase the size of the vector or array.
您还可以查看这个满足您需要的旧C ++库(今天是个强子库): http://myfitter.hepforge.org/.这可能是您最好的选择.它使用非参数方法.C ++版本已经很老了,我不确定功能是否完整,但是也许适合您.
You can also take a look at this old C++ library (nowaday a pytonic one) which does what you need : http://myfitter.hepforge.org/. This is probably your best bet. It uses a non parametric method. The C++ version is quite old and I am not so sure fully functional but perhaps this will go for you.
这篇关于使用Boost C ++为一组数据生成指数分布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!