【问题标题】:mixture of gaussian distribution in C++C ++中高斯分布的混合
【发布时间】:2016-09-16 03:28:55
【问题描述】:

我知道像 gsl 这样的库可以生成高斯分布并根据高斯分布生成随机数。https://www.gnu.org/software/gsl/manual/html_node/The-Gaussian-Distribution.html 我想知道是否有任何库可以生成随机数服从混合高斯分布并可以根据混合高斯分布返回给定值的概率?非常感谢。

跟进:如果我先生成一个介于(0,1)之间的随机数,是否一样,如果它落入(0, 0.5),我只是生成服从一个高斯分布的数字,否则从其他高斯分布生成数字。这个过程是否与生成数服从两个高斯的混合相同? (假设两个高斯之间的权重相等)

【问题讨论】:

  • 根据您发布的图表,我假设“混合”您只是指两个高斯的总和?

标签: c++ probability gaussian gsl


【解决方案1】:

您是对的:您可以分两步从高斯混合模型生成样本:

  • 根据混合权重随机决定从哪个高斯分布中采样。
  • 从选定的高斯分布中创建一个随机样本。

这是一个使用 C++ 内置随机数生成器的最小工作示例:

#include <iostream>
#include <random>
#include <array>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());

    using normal_dist   = std::normal_distribution<>;
    using discrete_dist = std::discrete_distribution<std::size_t>;

    auto G = std::array<normal_dist, 3>{
        normal_dist{5.0, 0.1}, // mean, stddev of G[0]
        normal_dist{8.0, 0.4}, // mean, stddev of G[1]
        normal_dist{2.0, 0.3}  // mean, stddev of G[2]
    };
    auto w = discrete_dist{
        0.1, // weight of G[0]
        0.6, // weight of G[1]
        0.3  // weight of G[2]
    };

    for (int n = 0; n < 100; ++n) {
        // Create one sample of the Gaussian mixture model
        auto index = w(gen);
        auto sample = G[index](gen);
        std::cout << sample << " ";
    }
    std::cout << '\n';
}

Try it out here.

【讨论】:

    猜你喜欢
    • 2013-03-27
    • 2015-04-19
    • 2016-11-10
    • 2020-04-06
    • 2013-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多