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

歡迎訪(fǎng)問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 人文社科 > 生活经验 >内容正文

生活经验

概率论中高斯分布(正态分布)介绍及C++11中std::normal_distribution的使用

發(fā)布時(shí)間:2023/11/27 生活经验 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 概率论中高斯分布(正态分布)介绍及C++11中std::normal_distribution的使用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

高斯分布:最常用的分布是正態(tài)分布(normal distribution),也稱(chēng)為高斯分布(Gaussian distribution):

正態(tài)分布N(x;μ,σ2)呈現(xiàn)經(jīng)典的”鐘形曲線(xiàn)”的形狀,其中中心峰的x坐標(biāo)由μ給出,峰的寬度受σ控制。

正態(tài)分布由兩個(gè)參數(shù)控制,μ∈R和σ∈(0,∞)。參數(shù)μ給出了中心峰值的坐標(biāo),這也是分布的均值:E[x]= μ。分布的標(biāo)準(zhǔn)差用σ表示,方差用σ2表示。

當(dāng)我們要對(duì)概率密度函數(shù)求值時(shí),我們需要對(duì)σ平方并且取倒數(shù)。當(dāng)我們需要經(jīng)常對(duì)不同參數(shù)下的概率密度函數(shù)求值時(shí),一種更高效的參數(shù)化分布的方式是使用參數(shù)β∈(0,∞),來(lái)控制分布的精度(precision)(或方差的倒數(shù)):

采用正態(tài)分布在很多應(yīng)用中都是一個(gè)明智的選擇。當(dāng)我們由于缺乏關(guān)于某個(gè)實(shí)數(shù)上分布的先驗(yàn)知識(shí)而不知道該選擇怎樣的形式時(shí),正態(tài)分布是默認(rèn)的比較好的選擇,其中有兩個(gè)原因:

(1)、我們想要建模的很多分布的真實(shí)情況是比較接近正態(tài)分布的。中心極限定理(central limit theorem)說(shuō)明很多獨(dú)立隨機(jī)變量的和近似服從正態(tài)分布。這意味著在實(shí)際中,很多復(fù)雜系統(tǒng)都可以被成功地建模成正態(tài)分布的噪聲,即使系統(tǒng)可以被分解成一些更結(jié)構(gòu)化的部分。

(2)、在具有相同方差的所有可能的概率分布中,正態(tài)分布在實(shí)數(shù)上具有最大的不確定性。因此,我們可以認(rèn)為正態(tài)分布是對(duì)模型加入的先驗(yàn)知識(shí)量最少的分布。

正態(tài)分布可以推廣到Rn空間,這種情況下被稱(chēng)為多維正態(tài)分布(multivariate normal distribution)。它的參數(shù)是一個(gè)正定對(duì)稱(chēng)矩陣∑:

參數(shù)μ仍然表示分布的均值,只不過(guò)現(xiàn)在是向量值。參數(shù)∑給出了分布的協(xié)方差矩陣。和單變量的情況類(lèi)似,當(dāng)我們希望對(duì)很多不同參數(shù)下的概率密度函數(shù)多次求值時(shí),協(xié)方差矩陣并不是一個(gè)很高效的參數(shù)化分布的方式,因?yàn)閷?duì)概率密度函數(shù)求值時(shí)需要對(duì)∑求逆。我們可以使用一個(gè)精度矩陣(precision matrix)β進(jìn)行替代:

我們常常把協(xié)方差矩陣固定成一個(gè)對(duì)角陣。一個(gè)更簡(jiǎn)單的版本是各向同性(isotropic)高斯分布,它的協(xié)方差矩陣是一個(gè)標(biāo)量乘以單位陣。

正態(tài)分布(normal distribution):又名高斯分布(Gaussian distribution,以德國(guó)數(shù)學(xué)家卡爾·弗里德里希·高斯的姓冠名),是一個(gè)在數(shù)學(xué)、物理及工程等領(lǐng)域都非常重要的概率分布,由于這個(gè)分布函數(shù)具有很大非常漂亮的性質(zhì),使得其在諸多涉及統(tǒng)計(jì)科學(xué)離散科學(xué)等領(lǐng)域的許多方面都有著重大的影響力。比如圖像處理中最常用的濾波器類(lèi)型為Gaussian濾波器(也就是所謂的正態(tài)分布函數(shù))。

若隨機(jī)變量X服從一個(gè)位置參數(shù)為μ、尺度參數(shù)為σ的概率分布,記為:X∽N(μ,σ2),則其概率密度函數(shù)為:

正態(tài)分布的數(shù)學(xué)期望值或期望值μ等于位置參數(shù),決定了分布的位置;其方差σ2的開(kāi)平方或標(biāo)準(zhǔn)差σ等于尺度參數(shù),決定了分布的幅度。

正態(tài)分布的概率密度函數(shù)曲線(xiàn)呈鐘形,因此人們又經(jīng)常稱(chēng)之為鐘形曲線(xiàn)。我們通常所說(shuō)的標(biāo)準(zhǔn)正態(tài)分布是位置參數(shù)μ=0,尺度參數(shù)σ2=1的正態(tài)分布。

以上內(nèi)容摘自:?《深度學(xué)習(xí)中文版》和?維基百科

以下是對(duì)C++11中的正態(tài)分布模板類(lèi)std::normal_distribution的介紹:

C++11中引入了正態(tài)分布模板類(lèi)std::normal_distribution,在頭文件<random>中。

正態(tài)分布(normal distribution)又名高斯分布(Gaussian distribution)。若隨機(jī)變量X服從一個(gè)數(shù)學(xué)期望為μ、方差為σ2的高斯分布,記為N(μ, σ2)。其概率密度函數(shù)為正態(tài)分布的期望值μ決定了其位置,其標(biāo)準(zhǔn)差σ決定了分布的幅度。通常所說(shuō)的標(biāo)準(zhǔn)正態(tài)分布是μ=0, σ=1的正態(tài)分布。

std::normal_distribution:Normal distribution, Random number distribution that produces floating-point values according to a normal distribution, which is described by the following probability density function:

This distribution produces random numbers around the distribution mean (μ) with a specific standard deviation (σ).

The normal distribution is a common distribution used for many kind of processes,since it is the distribution that the aggregation of a large number of independent random variables approximates to, when all follow the same distribution (no matter which distribution).

The distribution parameters, mean (μ) and stddev (σ), are set on construction. To produce a random value following this distribution, call its member function operator().

?normal distribution represents an unbounded distribution.

以下是std::normal_distribution的測(cè)試code:

#include "normal_distribution.hpp"
#include <iostream>
#include <random>
#include <string>
#include <chrono>
#include <map>
#include <iomanip>///
// reference: http://www.cplusplus.com/reference/random/normal_distribution/
int test_normal_distribution_1()
{
{const int nrolls = 10000;  // number of experimentsconst int nstars = 100;    // maximum number of stars to distributestd::default_random_engine generator;std::normal_distribution<double> distribution(5.0, 2.0);int p[10] = {};for (int i = 0; i<nrolls; ++i) {double number = distribution(generator);if ((number >= 0.0) && (number<10.0)) ++p[int(number)];}std::cout << "normal_distribution (5.0,2.0):" << std::endl;for (int i = 0; i<10; ++i) {std::cout << i << "-" << (i + 1) << ": ";std::cout << std::string(p[i] * nstars / nrolls, '*') << std::endl;}
}{ // (1)、normal_distribution::normal_distribution: Constructs a normal_distribution object,
//   adopting the distribution parameters specified either by mean and stddev or by object parm.
//   (2)、normal_distribution::max: Returns the least upper bound of the range of values potentially returned by member operator().
//   (3)、normal_distribution::min: Returns the greatest lower bound of the range of values potentially returned by member operator().
//   (4)、normal_distribution::mean: Returns the mean(μ)parameter associated with the normal_distribution object
//   (5)、normal_distribution::stddev: Returns the standard deviation (σ) associated with the normal_distribution object
//   (6)、normal_distribution::operator(): 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:unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();std::default_random_engine generator(seed);std::normal_distribution<double> distribution(0.0, 1.0);std::cout << "some Normal-distributed(0.0,1.0) results:" << std::endl;for (int i = 0; i<10; ++i)std::cout << distribution(generator) << std::endl;std::cout << "max: " << distribution.max() << std::endl;std::cout << "min: " << distribution.min() << std::endl;std::cout << "mean: " << distribution.mean() << std::endl;std::cout << "stddev: " << distribution.stddev() << std::endl;
}{ // normal_distribution::param: Distribution parametersstd::default_random_engine generator;std::normal_distribution<double> d1(0.0, 1.0);std::normal_distribution<double> d2(d1.param());// print two independent values:std::cout << d1(generator) << std::endl;std::cout << d2(generator) << std::endl;
}{ // normal_distribution::reset: Resets the distribution,
//   so that subsequent uses of the object do not depend on values already produced by it.std::default_random_engine generator;std::normal_distribution<double> distribution(0.0, 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/normal_distribution
int test_normal_distribution_2()
{std::random_device rd;std::mt19937 gen(rd());// values near the mean are the most likely// standard deviation affects the dispersion of generated values from the meanstd::normal_distribution<> d(5, 2);std::map<int, int> hist;for (int n = 0; n<10000; ++n) {++hist[std::round(d(gen))];}for (auto p : hist) {std::cout << std::fixed << std::setprecision(1) << std::setw(2)<< p.first << ' ' << std::string(p.second / 200, '*') << '\n';}return 0;
}// reference: https://msdn.microsoft.com/en-us/library/bb982827.aspx
static void test(const double m, const double s, const int samples)
{using namespace std;// uncomment to use a non-deterministic seed  //    random_device gen;  //    mt19937 gen(rd());  mt19937 gen(1701);normal_distribution<> distr(m, s);cout << endl;cout << "min() == " << distr.min() << endl;cout << "max() == " << distr.max() << endl;cout << "m() == " << fixed << setw(11) << setprecision(10) << distr.mean() << endl;cout << "s() == " << fixed << setw(11) << setprecision(10) << distr.stddev() << endl;// generate the distribution as a histogram  map<double, int> histogram;for (int i = 0; i < samples; ++i) {++histogram[distr(gen)];}// print results  cout << "Distribution for " << samples << " samples:" << endl;int counter = 0;for (const auto& elem : histogram) {cout << fixed << setw(11) << ++counter << ": "<< setw(14) << setprecision(10) << elem.first << endl;}cout << endl;
}int test_normal_distribution_3()
{using namespace std;double m_dist = 0;// 1;double s_dist = 1;int samples = 10;cout << "Use CTRL-Z to bypass data entry and run using default values." << endl;cout << "Enter a floating point value for the 'mean' distribution parameter: ";//cin >> m_dist;cout << "Enter a floating point value for the 'stddev' distribution parameter (must be greater than zero): ";//cin >> s_dist;cout << "Enter an integer value for the sample count: ";//cin >> samples;test(m_dist, s_dist, samples);return 0;
}

GitHub:? https://github.com/fengbingchun/Messy_Test

總結(jié)

以上是生活随笔為你收集整理的概率论中高斯分布(正态分布)介绍及C++11中std::normal_distribution的使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。