Sobol序列详解

发布时间:2023-05-20

Sobol序列算法

Sobol序列是一种低差异性序列(low discrepancy sequence),主要用于模拟和优化中的随机抽样。Sobol序列采用有限串的异或和代替随机化,性质比纯随机序列更优。 Sobol序列的生成使用基础网格点和生成矩阵,在每个生成步骤中,产生新的点,然后用它们来更新基础点。 下面是Sobol序列的核心代码示例:

def sobol_sequence(n, dim):
    # 初始化参数
    s = prime_table(dim)
    a = init_matrix(dim)
    v = init_points(n, dim)
    # 生成新的点
    for i in range(n):
        v[i] = sobol_point(i, a, s)
    return v
def sobol_point(n, a, s):
    res = [0] * len(a)
    for j in range(len(a)):
        while(n > 0):
            res[j] ^= (n%2) * s[j]
            n //= 2
            s[j] //= a[j]
        s[j] *= a[j]
    return res

Sobol序列正态分布

由于Sobol序列是伪随机数,所以无法直接得到正态分布。但是可以通过转换,将Sobol序列转化为服从正态分布的序列。 将Sobol序列转换为正态分布需要使用逆正态分布函数,例如 scipy.stats.norm.ppf 函数。通过逆正态分布函数将Sobol序列转化后,即可得到服从正态分布的样本。

Sobol序列抽样

Sobol序列通常用于Monte Carlo抽样。在Monte Carlo方法中,需要产生大量随机样本。使用Sobol序列可以保证序列的低差异性,降低误差。 Sobol序列的抽样使用 Python 的 random 库名来实现:

import random
sobol = sobol_sequence(n, dim)
for i in range(n):
    # 生成服从正态分布的样本
    x = scipy.stats.norm.ppf(sobol[i])
    # 对样本进行处理
    y = f(x)
    # 计算积分
    res += y / n

Sobol序列原理

Sobol序列的生成原理是利用生成矩阵和基础网格点。在生成过程中,每个维度使用特定的生成矩阵来计算得到第i个点的第j个分量。在Sobol序列中,所有分量都是互不相同的,这保证了低差异性。 下面是基础网格点的示例代码:

def init_points(n, dim):
    # 初始化点的位置
    v = [[0] * dim] * n
    # 将每个位置进行赋值
    for i in range(n):
        v[i][0] = i
    return v

Sobol序列代码

Sobol序列的代码实现是相对比较简单的。以下是完整的代码示例:

def init_matrix(dim):
    a = [[0] * dim] * dim
    vec = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824]
    for i in range(dim):
        for j in range(dim):
            if i == j:
               a[i][j] = 1
            else:
                a[i][j] = vec[j] ^ a[i][j-1]
    return a
def prime_table(dim):
    p = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113]
    s = [1] * dim
    for i in range(dim):
        j = 0
        while i >= j:
            s[i] *= p[j]
            j += 1
    return s
def sobol_sequence(n, dim):
    # 初始化参数
    s = prime_table(dim)
    a = init_matrix(dim)
    v = init_points(n, dim)
    # 生成新的点
    for i in range(n):
        v[i] = sobol_point(i, a, s)
    return v
def sobol_point(n, a, s):
    res = [0] * len(a)
    for j in range(len(a)):
        while(n > 0):
            res[j] ^= (n%2) * s[j]
            n //= 2
            s[j] //= a[j]
        s[j] *= a[j]
    return res

Sobol序列 Verilog

Sobol序列不仅可以在Python中实现,也可以在硬件描述语言Verilog中实现。以下是Sobol序列Verilog的示例代码:

module sobol(input clk, input [31:0] n, input [31:0] dim, output reg [31:0] rand_out []);
    reg [31:0] res;
    reg [31:0] a1, a2;
    reg [31:0] s1[dim-1:0], s2[dim-1:0];
    initial begin
        res = 0;
        for(int i = 0; i < dim; i++) begin
            a1[i] = 1;
        end
        for(int i = 0; i < dim-1; i++) begin
            s1[i] = 1;
        end
        for(int i = 0; i < n; i++) begin
            sobol_point();
            rand_out[i] = res;
        end
    end
    function void sobol_point;
        res = 0;
        for(int i = 0; i < dim; i++) begin
            a2[i] = init_matrix(i, dim);
            s2[i] = prime_table(i, dim);
            while(n > 0) begin
                res[i] = $xor(res[i], (n%2) * s1[i]);
                n = n / 2;
                s1[i] = s1[i] / a1[i];
            end
            s1[i] = s1[i] * a1[i];
        end
    end
    function int prime_table(int i, int dim);
        int [29:0] p = '{2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113};
        int [31:0] s = 1;
        for(int j = 0; j <= i; j++) begin
            s = s * p[j];
        end
        return s;
    end
    function int init_matrix(int i, int dim);
        int [31:0] vec[31:0] = '{1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824};
        int [31:0] a[dim-1:0]; 
        for(int j = 0; j < dim; j++) begin
            if(j == i) begin
                a[i] = 1;
            end else begin
                a[j] = vec[i] ^ a[j-1];
            end
        end
        return a[i];
    end
endmodule

Sobol序列知乎

Sobol序列在知乎上有很多相关的问题和讨论,例如在模拟优化、金融工程等领域中的应用。以下是知乎上的几个相关讨论:

  1. 模拟优化中Sobol序列的应用:https://www.zhihu.com/question/20685683
  2. Sobol序列与随机数生成:https://www.zhihu.com/question/24494840
  3. Sobol序列在金融工程中的应用:https://www.zhihu.com/question/60063008