python 别名方法 - 离散采样

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 别名方法 - 离散采样相关的知识,希望对你有一定的参考价值。

"""
    author: node2vec
"""
# Alias method 采样   
def alias_setup(probs):
    '''
    Compute utility lists for non-uniform sampling from discrete distributions.
    Refer to hips.seas.harvard.edu/blog/2013/03/03/the-alias-method-efficient-sampling-with-many-discrete-outcomes/
    for details
    '''
    K = len(probs)
    q = np.zeros(K) # 表示选择当前格子时落在事件i的概率
    J = np.zeros(K, dtype=np.int) # J 中把保存第i列中不是不是事件i的另一个事件的编号

    smaller = []
    larger = []
    for kk, prob in enumerate(probs):
        q[kk] = K * prob
        if q[kk] < 1.0:
            smaller.append(kk)
        else:
            larger.append(kk)

    while len(smaller) > 0 and len(larger) > 0:
        small = smaller.pop()
        large = larger.pop()

        J[small] = large
        q[large] = q[large] + q[small] - 1.0
        if q[large] < 1.0:
            smaller.append(large)
        else:
            larger.append(large)

    return J, q

def alias_draw(J, q):
    '''
    Draw sample from a non-uniform discrete distribution using alias sampling.
    '''
    K = len(J)

    kk = int(np.floor(np.random.rand() * K))
    if np.random.rand() < q[kk]:
        return kk
    else:
        return J[kk]

以上是关于python 别名方法 - 离散采样的主要内容,如果未能解决你的问题,请参考以下文章

Python图像处理采样卷积与离散傅里叶变换

MATLAB对离散采样信号添加高斯白噪声(已知Eb/N0)

Python scipy.interpolate插值

正弦波信号发生器(离散采样)

照片重复取样法计算原理

离散时间信号采样