normal-distribution - 为高斯马尔可夫随机场创建精度矩阵

标签 normal-distribution

我目前正在尝试为高斯马尔可夫随机字段创建一个精度矩阵。可以说我在6x6的空间网格中有随机变量。然后,我将得到一个36x36的精度矩阵。

现在假设我有一个3x3的邻居罩,那么我的精度矩阵将是

Q= nnbs[1] -1      0        0    0     0    -1.......0
   -1      nnbs[2] -1       0    0     0     0 ......0
   0       -1      nnbs[3]  -1   0     0     0 ......0
   ...................................................
   ...................................................

等等。谁能建议我如何编码此精度矩阵。我的意思是,如果将窗口大小/邻域大小更改为5x5,那么我将拥有一个新的精度矩阵。我该如何编码?其中nnbs是该元素的邻居数
rows=20;
columns=20;

%Random initialization
data=zeros(1000,3);
index=1;
value=-1;

%3x3 neighborhood
%For each element the neighbors are accessible within 1 hop so neighbors=1
neighbors=1;


for i=1:rows
    for j=1:columns

        for k=1:neighbors
            %same row right
            if j+k <= columns
                data(index,1) = (i-1)*columns+j;
                data(index,2) = ((i-1)*columns) + (j+k);
                data(index,3) = value;
                index=index+1;
            end

            %same row left
            if j-k >= 1;
                data(index,1) = (i-1)*columns+j;
                data(index,2) = ((i-1)*columns) + (j-k);
                data(index,3) = value;
                index=index+1;
            end
        end


        %row below -> bottom left right
        for k=i+1:i+neighbors
            if k <= rows
                %bottom
                data(index,1) = (i-1)*columns+j;
                data(index,2) = (k-1)*columns + j;
                data(index,3) = value;
                index=index+1;

                for l=1:neighbors
                    %right
                    if j+l <= columns
                        data(index,1) = (i-1)*columns+j;
                        data(index,2) = ((k-1)*columns) + (j+1);
                        data(index,3) = value;
                        index=index+1;
                    end

                    %left
                    if j-l >= 1;
                        data(index,1) = (i-1)*columns+j;
                        data(index,2) = ((k-1)*columns)+(j-1);
                        data(index,3) = value;
                        index=index+1;
                    end
                end

            end


        end




        %row above top left right
        for k=i-1:i-neighbors
            if k >= 1
                %top
                data(index,1) = (i-1)*columns+j;
                data(index,2) = ((k-1)*columns) +j;
                data(index,3) = value;
                index=index+1;

                for l=1:neighbors
                    %right
                    if j+l <= columns
                        data(index,1) = (i-1)*columns+j;
                        data(index,2) = ((k-1)*columns) + (j+1);
                        data(index,3) = value;
                        index=index+1;
                    end

                    %left
                    if j-k >= 1;
                        data(index,1) = (i-1)*columns+j;
                        data(index,2) = ((k-1)*columns) + (j-1);
                        data(index,3) = value;
                        index=index+1;
                    end
                end
            end
        end  
    end
end

%Get the values for the diagonal elements(which is equal to the number of
%neighbors or absolute sum of the nondiagonal elements of the corresponding
%row)

diagonal_values = zeros(rows*columns,3);
for i=1:rows*columns
    pointer=find(data(:,1) == i);
    diag_value=abs(sum(data(pointer,3)));
    diagonal_values(i,1) = i;
    diagonal_values(i,2) = i;
    diagonal_values(i,3) = diag_value;
end

data(index:index+rows*columns-1,:)=diagonal_values(:,:);


Q = sparse(data(:,1), data(:,2), data(:,3), rows*columns, rows*columns);    

我尝试过类似的方法,但我认为这不是最有效的方法。我认为应该有更好的方法。

最佳答案

为时已晚,但对其他人可能有用:
您的精度矩阵是对称Toeplitz矩阵的kronecker积的线性组合:到每个邻居类型都对应2 Toeplitz矩阵的kronecker积。
More info about toeplitz Matrix
范例:
您只需要每个像素具有水平链接的精度矩阵
I_n的单位矩阵写入n,并将H_{n,p}的大小写成[n n]的对称Toeplitz矩阵,除第p个对角线外,其余各处均填充0

H_ {4,2} =

      0  1  0  0
      1  0  1  0
      0  1  0  1
      0  0  1  0

在Matlab中:

H_nonSym_n_p = toeplitz(zeros(n,1),[[zeros(1,p-1)1] zeros(1,n-p)]);
H_sym_n_p = H_nonSym + H_nonSym';

然后,如果您有一个[n m]图像,并且想要对每个像素的水平邻域进行编码,则可以通过kronecker乘积来表示它(希望可以使用类似LaTeX的代码):Q =-I_n \ otimes \ H_ {n ,2}。
最后,得到您的nnbs:类似于Q = Q - diag(sum(Q,2)) ...
现在,如果您需要其他链接,例如2个水平链接和2个垂直链接:Q =-I_n \ otimes \ H_ {n,2}-I_n \ otimes \ H_ {n,3}-\ H_ {n,2} \ otimes I_ {n}-\ H_ {n,3} \ otimes I_ {n}。
并再次Q = Q - diag(sum(Q,2))请注意,对角线邻居很难生成,但是仍然由托普利兹矩阵的克罗内克积表示(这次可能是非对称的)。

关于normal-distribution - 为高斯马尔可夫随机场创建精度矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11298258/

相关文章:

r - 在 R 和 ggplot2 中绘制正态分布的垂直密度

c++ - 使用 Boost skew_normal_distribution

python - 联合正态先验分布的后验

random - 在偏向中间的范围内生成随机无符号整数

r - dmvnorm 中 x 参数的目的是什么?

python - 调整随机数列表的平均值和标准差?

python - 如何使用 PYMC 编写多元正态的分层混合模型

python-3.x - python 中的置信区间

r - curve() 在第一个示例中有效,但在第二个示例中无效,但它们看起来相同。为什么?

R - R 中的多元正态分布