日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人文社科 > 生活经验 >内容正文

生活经验

概率论中指数分布介绍及C++11中std::exponential_distribution的使用

發布時間:2023/11/27 生活经验 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 概率论中指数分布介绍及C++11中std::exponential_distribution的使用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

指數分布:在深度學習中,我們經常會需要一個在x=0點處取得邊界點(sharp point)的分布。為了實現這一目的,我們可以使用指數分布(exponential distribution):

p(x;λ)= λlx≥0exp(-λx)

指數分布使用指示函數(indicator function) lx≥0來使得當x取負值時的概率為零。

指數分布:在概率論和統計學中,指數分布(Exponential distribution)是一種連續概率分布。指數分布可以用來表示獨立隨機事件發生的時間間隔,比如旅客進入機場的時間間隔、打進客服中心電話的時間間隔、中文維基百科新條目出現的時間間隔等等。

一個指數分布的概率密度函數是:

其中λ>0是分布的一個參數,常被稱為率參數(rate parameter)。即每單位時間發生該事件的次數。指數分布的區間是[0,∞)。如果一個隨機變量X呈指數分布,則可以寫作:X∽Exponential(λ)。

指數分布的累積分布函數可以寫成:

隨機變量X(X的率參數是λ)的期望值是:E[X]=1/λ,比方說:如果你平均每個小時接到2次電話,那么你預期等待每一次電話的時間是半個小時。X的方差是:D[X]=1/λ2,X的偏離系數是:V[X]=1.

以下簡單介紹Laplace分布、Dirac分布、經驗分布、混合分布:

Laplace分布:一個聯系緊密的概率分布是Laplace分布(Laplace distribution),它允許我們在任意一點μ處設置概率質量的峰值:

拉普拉斯分布:在概率論與統計學中,拉普拉斯分布是以皮埃爾-西蒙·拉普拉斯的名字命名的一種連續概率分布。由于它可以看作是兩個不同位置的指數分布背靠背拼接在一起,所以它也叫做雙指數分布。兩個相互獨立同概率分布指數隨機變量之間的差別是按照指數分布的隨機時間布朗運動,所以它遵循拉普拉斯分布。

如果隨機變量的概率密度函數分布為:

那么它就是拉普拉斯分布。其中,μ是位置參數,b>0是尺度參數。如果μ=0,那么,正半部分恰好是尺度為1/2的指數分布。

拉普拉斯分布的概率密度函數讓我們聯想到正態分布,但是,正態分布是用相對于μ平均值的差的平方來表示,而拉普拉斯概率密度函數用相對于平均值的差的絕對值來表示。因此,拉普拉斯分布的尾部比正態分布更加平坦。

Boost 1.64.0庫中實現了對Laplace分布的實現,在math/distributions/laplace.hpp文件中,對應類為laplace_distribution。

Dirac分布:在一些情況下,我們希望概率分布中的所有質量都集中在一個點上。這可以通過Dirac delta函數(Dirac delta function)δ(x)定義概率密度函數來實現:p(x)= δ(x-μ)

Dirac delta函數被定義成在除了0以外的所有點的值都為0,但是積分為1。Dirac delta函數不像普通函數一樣對x的每一個值都有一個實數值的輸出,它是一種不同類型的數學對象,被稱為廣義函數(generalized function),廣義函數是依據積分性質定義的數學對象。我們可以把Dirac delta函數想成一系列函數的極限點,這一系列函數把除0以外的所有點的概率密度越變越小。

通過把p(x)定義成δ函數左移-μ個單位,我們得到了一個在x=μ處具有無限窄也無限高的峰值的概率質量。

狄拉克δ函數:在科學和數學中,狄拉克δ函數或簡稱δ函數是在實數線上定義的一個廣義函數或分布。它在除零以外的點上都等于零,且其在整個定義域上的積分等于1。δ函數有時可看作是在原點處無限高、無限細,但是總面積為1 的一個尖峰,在物理上代表了理想化的質點或點電荷的密度。

從純數學的觀點來看,狄拉克δ函數并非嚴格意義上的函數,因為任何在擴展實數線上定義的函數,如果在一個點以外的地方都等于零,其總積分必須為零。δ函數只有在出現在積分以內的時候才有實質的意義。根據這一點,δ函數一般可以當做普通函數一樣使用。它形式上所遵守的規則屬于運算微積分的一部分,是物理學和工程學的標準工具。

δ函數的圖形通常可以視為整條x軸和正y軸。雖然稱為函數,但δ函數并非真正的函數,至少它的值域不在實數以內。

Dirac分布經常作為經驗分布(empirical distribution)的一個組成部分出現:

經驗分布將概率密度1/m賦給m個點x(1),…,x(m)中的每一個,這些點是給定的數據集或者采樣的集合。只有在定義連續型隨機變量的經驗分布時,Dirac delta函數才是必要的。對于離散型隨機變量,情況更加簡單:經驗分布可以被定義成一個Multinoulli分布,對于每一個可能的輸入,其概率可以簡單地設為在訓練集上那個輸入值的經驗頻率(empirical frequency)。

當我們在訓練集上訓練模型時,我們可以認為從這個訓練集上得到的經驗分布指明了我們采樣來源的分布。關于經驗分布另外一個重要的觀點是,它是訓練數據的似然最大的那個概率密度函數。

Empirical distribution function: In statistics, an empirical distribution function is the distribution function associated with the empirical measure of a sample. This cumulative distribution function is a step function that jumps up by 1/n at each of the n data points. Its value at any specified value of the measured variable is the fraction of observations of the measured variable that are less than or equal to the specified value.

The empirical distribution function is an estimate of the cumulative distribution function that generated the points in the sample. It converges with probability 1 to that underlying distribution, according to the Glivenko–Cantelli theorem.A number of results exist to quantify the rate of convergence of the empirical distribution function to the underlying cumulative distribution function.

分布的混合:通過組合一些簡單的概率分布來定義新的概率分布也是很常見的。一種通用的組合方法是構造混合分布(mixture distribution)。混合分布由一些組件(component)分布構成。每次實驗,樣本是由哪個組件分布產生的取決于從一個Multinoulli分布中采樣的結果:這里P(c)是對各組件的一個Multinoulli分布。

一個非常強大且常見的混合模型是高斯混合模型(Gaussian Mixture Model),它的組件p(x|c=i)是高斯分布。每個組件都有各自的參數,均值μ(i)和協方差矩陣∑(i)。有一些混合可以有更多的限制。和單個高斯分布一樣,高斯混合模型有時會限制每個組件的協方差矩陣為對角的或者各向同性的(標量乘以單位矩陣)。

除了均值和協方差以外,高斯混合模型的參數指明了給每個組件i的先驗概率(prior probability)αi=P(c=i)。”先驗”一詞表明了在觀測到x之前傳遞給模型關于c的信念(belief)。作為對比,P(c|x)是后驗概率(posterior probability),因為它是在觀測到x之后進行計算的。高斯混合模型是概率密度的萬能近似器(universal approximator),在這種意義上,任何平滑的概率密度都可以用具有足夠多組件的高斯混合模型以任意精度來逼近。

Mixture distribution: In probability and statistics, a mixture distribution is the probability distribution of a random variable that is derived from a collection of other random variables as follows: first, a random variable is selected by chance from the collection according to given probabilities of selection, and then the value of the selected random variable is realized. The underlying random variables may be random real numbers, or they may be random vectors (each having the same dimension), in which case the mixture distribution is a multivariate distribution.

以上內容摘自:《深度學習中文版》和?維基百科

以下是對C++11中的指數分布模板類std::exponential_distribution的介紹:

C++11中引入了指數分布(Exponential distribution)模板類std::exponential_distribution.指數分布是一個連續概率分布,產生隨機非負浮點數,可以用來表示獨立隨機事件發生的時間間隔。
std::exponential_distribution:Random number distribution that produces floating-point values according to an exponential distribution, which is described by the following probability density function: p(x|λ)= λe-λx, x>0
This distribution produces random numbers where each value represents the interval between two random events that are independent but statistically defined by a constant average rate of occurrence (its lambda, λ).The distribution parameter, lambda, is set on construction. To produce a random value following this distribution, call its member function operator().Its analogous discrete distribution is the geometric_distribution.?

?下面是從其它文章中copy的std::exponential_distribution測試代碼,詳細內容介紹可以參考對應的reference:

#include "exponential_distribution.hpp"
#include <iostream>
#include <random>
#include <string>
#include <chrono>
#include <thread>
#include <iomanip>
#include <map>///
// reference: http://www.cplusplus.com/reference/random/exponential_distribution/
int test_exponential_distribution_1()
{
{const int nrolls = 10000;  // number of experimentsconst int nstars = 100;    // maximum number of stars to distributeconst int nintervals = 10; // number of intervalsstd::default_random_engine generator;std::exponential_distribution<double> distribution(3.5);int p[nintervals] = {};for (int i = 0; i<nrolls; ++i) {double number = distribution(generator);if (number<1.0) ++p[int(nintervals*number)];}std::cout << "exponential_distribution (3.5):" << std::endl;std::cout << std::fixed; std::cout.precision(1);for (int i = 0; i<nintervals; ++i) {std::cout << float(i) / nintervals << "-" << float(i + 1) / nintervals << ": ";std::cout << std::string(p[i] * nstars / nrolls, '*') << std::endl;}
}{ // (1)、exponential_distribution::exponential_distribution: Construct exponential distribution,
//   Constructs an exponential_distribution object, adopting the distribution parameters specified either by lambda or by object parm.
//   (2)、exponential_distribution::lambda: Returns the parameter lambda (λ) associated with the exponential_distribution.
//   This parameter represents the number of times the random events are observed by interval, on average.
//   This parameter is set on construction.
//   (3)、exponential_distribution::max: Maximum value
//   Returns the least upper bound of the range of values potentially returned by member operator().
//   (4)、exponential_distribution::min: Minimum value
//   Returns the greatest lower bound of the range of values potentially returned by member operator(),
//   which for exponential_distribution is always zero.
//   (5)、exponential_distribution::operator(): Generate random number
//   Returns a new random number that follows the distribution's parameters associated to the object (version 1)
//   or those specified by parm (version 2).// construct a trivial random generator engine from a time-based seed:int seed = std::chrono::system_clock::now().time_since_epoch().count();std::default_random_engine generator(seed);std::exponential_distribution<double> distribution(1.0);std::cout << "ten beeps, spread by 1 second, on average: " << std::endl;for (int i = 0; i<10; ++i) {double number = distribution(generator);std::chrono::duration<double> period(number);std::this_thread::sleep_for(std::chrono::seconds(1)/*period*/);std::cout << "beep!" << std::endl;}std::cout << "lambda: " << distribution.lambda() << std::endl;std::cout << "max: " << distribution.max() << std::endl;std::cout << "min: " << distribution.min() << std::endl;
}{ // exponential_distribution::param: Distribution parameters
//   The first version(1) returns an object with the parameters currently associated with the distribution object.
//   The second version(2) associates the parameters in object parm to the distribution object.std::default_random_engine generator;std::exponential_distribution<double> d1(0.8);std::exponential_distribution<double> d2(d1.param());// print two independent values:std::cout << d1(generator) << std::endl;std::cout << d2(generator) << std::endl;
}{ // exponential_distribution::reset: Resets the distribution, so that subsequent uses of the object do not depend on values already produced by it.
//   This function may have no effect if the library implementation for this distribution class produces independent values.std::default_random_engine generator;std::exponential_distribution<double> distribution(1.0);// print two independent values:std::cout << distribution(generator) << std::endl;distribution.reset();std::cout << distribution(generator) << std::endl;
}return 0;
}// reference: http://en.cppreference.com/w/cpp/numeric/random/exponential_distribution
int test_exponential_distribution_2()
{std::random_device rd;std::mt19937 gen(rd());// if particles decay once per second on average,// how much time, in seconds, until the next one?std::exponential_distribution<> d(1);std::map<int, int> hist;for (int n = 0; n<10000; ++n) {++hist[2 * d(gen)];}for (auto p : hist) {std::cout << std::fixed << std::setprecision(1)<< p.first / 2.0 << '-' << (p.first + 1) / 2.0 <<' ' << std::string(p.second / 200, '*') << '\n';}return 0;
}// reference: https://msdn.microsoft.com/en-us/library/bb982914.aspx
static void test(const double l, const int s)
{// uncomment to use a non-deterministic generator//    std::random_device gen;std::mt19937 gen(1701);std::exponential_distribution<> distr(l);std::cout << std::endl;std::cout << "min() == " << distr.min() << std::endl;std::cout << "max() == " << distr.max() << std::endl;std::cout << "lambda() == " << std::fixed << std::setw(11) << std::setprecision(10) << distr.lambda() << std::endl;// generate the distribution as a histogram  std::map<double, int> histogram;for (int i = 0; i < s; ++i) {++histogram[distr(gen)];}// print results  std::cout << "Distribution for " << s << " samples:" << std::endl;int counter = 0;for (const auto& elem : histogram) {std::cout << std::fixed << std::setw(11) << ++counter << ": "<< std::setw(14) << std::setprecision(10) << elem.first << std::endl;}std::cout << std::endl;
}
int test_exponential_distribution_3()
{double l_dist = 0.5;int samples = 10;std::cout << "Use CTRL-Z to bypass data entry and run using default values." << std::endl;std::cout << "Enter a floating point value for the 'lambda' distribution parameter (must be greater than zero): ";//std::cin >> l_dist;std::cout << "Enter an integer value for the sample count: ";//std::cin >> samples;test(l_dist, samples);return 0;
}
GitHub: https://github.com/fengbingchun/Messy_Test

總結

以上是生活随笔為你收集整理的概率论中指数分布介绍及C++11中std::exponential_distribution的使用的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。