生成正半定矩阵的简单算法

发布于 2024-07-14 10:51:33 字数 69 浏览 5 评论 0原文

我想生成正随机半定矩阵。 我正在寻找一种算法,或更优选的是用 C、matlab、java 或任何语言实现该算法的简单实现。

I want to generate positive random semi-definite matrices. I am looking for an algorithm or more preferably an simple implementation of the algorithm in C, matlab, java or any language.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清音悠歌 2024-07-21 10:51:33
  1. 生成随机矩阵,
  2. 将其乘以它自己的转置,
  3. 就得到了一个正半定矩阵。

示例代码(Python):

import numpy as np
matrixSize = 10 
A = np.random.rand(matrixSize, matrixSize)
B = np.dot(A, A.transpose())
print 'random positive semi-define matrix for today is', B
  1. generate random matrix
  2. multiply it by its own transposition
  3. you have obtained a positive semi-definite matrix.

Example code (Python):

import numpy as np
matrixSize = 10 
A = np.random.rand(matrixSize, matrixSize)
B = np.dot(A, A.transpose())
print 'random positive semi-define matrix for today is', B
深海夜未眠 2024-07-21 10:51:33

您需要明确“随机”的定义。 您对所得矩阵有哪些限制? 您希望系数均匀分布还是正态分布? 您希望特征值具有特定的分布吗? (等等)

有多种方法可以生成正半定矩阵 M,包括:

  1. 给定任意矩阵 A,计算 M = ATA (构造一个 Cholesky 分解
  2. 给定一个具有非负对角元素的任意对角矩阵 S 和一个相同大小的正交矩阵 Q,计算 M = QSQT (构建奇异值分解

出于数字原因,我可能会选择第二种方法是生成具有所需属性的对角矩阵,然后生成 Q 作为许多Householder 反射<的组合/a> (生成随机向量 v,缩放至单位长度,H = I - 2vvT); 我怀疑你想要使用 K * N,其中 N 是矩阵 M 的大小,K 是 1.5-3 之间的数字(我猜测这一点),以确保它具有足够的自由度。

您还可以使用 Givens 旋转 生成正交矩阵 Q:选择从 1 到 N 的 2 个不同值,然后围绕该对轴生成吉文斯旋转,角度在 0 到 2 * pi 之间均匀分布。 然后取其中的 K * N(与上段相同的推理),它们的组合产生 Q。

编辑:我猜测(不确定)如果你有独立生成且正态分布的系数,那么整个矩阵将是“正态分布”(无论这意味着什么)。 至少对于向量来说是这样。 (N 个独立生成的高斯随机变量,每个分量一个,为您提供一个高斯随机向量)对于均匀分布的分量来说,情况并非如此。

You need to be clear on your definition of "random". What are your constraints on the resulting matrix? Do you want the coefficients to be uniformly or normally distributed? Do you want the eigenvalues to have a particular distribution? (etc.)

There are a number of ways to generate positive semidefinite matrices M, including:

  1. Given an arbitrary matrix A, compute M = ATA (constructing a Cholesky decomposition)
  2. Given an arbitrary diagonal matrix S with nonnegative diagonal entries, and an orthonormal matrix Q of the same size, compute M = QSQT (constructing a singular value decomposition)

For numerical reasons I'd probably choose the second approach by generating the diagonal matrix with desired properties, then generating Q as the composition of a number of Householder reflections (generate a random vector v, scale to unit length, H = I - 2vvT); I suspect you'd want to use K * N where N is the size of the matrix M, and K is a number between 1.5-3 (I'm guessing on this) that ensures that it has enough degrees of freedom.

You could also generate an orthonormal matrix Q using Givens rotations: pick 2 distinct values from 1 to N and generate a Givens rotation about that pair of axes, with an angle uniformly distributed from 0 to 2 * pi. Then take K * N of these (same reasoning as above paragraph) and their composition yields Q.

edit: I'd guess (not sure) that if you have coefficients that are independently-generated and normally distributed, then the matrix as a whole would be "normally distributed" (whatever that means). It's true for vectors, at least. (N independently-generated Gaussian random variables, one for each component, gives you a Gaussian random vector) This isn't true for uniformly-distributed components.

浴红衣 2024-07-21 10:51:33

如果你可以用你选择的语言生成一个随机矩阵,那么通过使用矩阵乘以其转置是半正定的属性,你可以生成一个随机正半定矩阵

在Matlab中它会像下面这样简单

% Generate a random 3x3 matrix
    A = rand(3,3) 
% Multiply by its tranpose
    PosSemDef = A'*A 

If you can generate a random matrix in your chosen language, then by using the property that a matrix multiplied by its transpose is positive semi-definte, you can generate a random positive semi-definite matix

In Matlab it would be as simple as

% Generate a random 3x3 matrix
    A = rand(3,3) 
% Multiply by its tranpose
    PosSemDef = A'*A 
沐歌 2024-07-21 10:51:33

正半定矩阵上的自然分布是 Wishart 分布

Natural distributions on positive semidefinite matrices are Wishart distributions.

浅忆 2024-07-21 10:51:33

A'*A 将给出一个正半定矩阵当且仅当 A 是秩亏的。 因此,上述答案和从维基百科复制的答案通常并不正确。 要计算正半定矩阵,只需取任何矩形 m × n 矩阵 (m < n) 并将其乘以其转置即可。 即,如果B是m×n矩阵,其中m<1。 n,则 B'*B 是半定矩阵。 我希望这有帮助。

A'*A will give a positive semidefite matrix iff and only if A is rank-deficient. So the answers stated above and that copied from wikipedia are not generally true. To compute a positive semidefinite matrix simply take any rectangular m by n matrix (m < n) and multiply it by its transpose. I.e. if B is an m by n matrix, with m < n, then B'*B is a semidefinite matrix. I hope this helps.

作死小能手 2024-07-21 10:51:33

澄清一点(我希望)。 令 A 为随机矩阵(例如,由随机正态变量填充),mxn,其中 m >= n。 那么如果 A 是满列秩的,A'A 将是正定的。 如果 A 的秩 < n 那么 A'A 将是半正定的(但不是
正定)。

m >= n 的随机正态矩阵几乎肯定是满秩的; 为了生成秩亏矩阵,可以附加一列或多列,这些列是其他列的线性组合。

To clarify a little (I hope). Let A be a random matrix (for example, populated by random normal variates), m x n with m >= n. Then if A is of full column rank, A'A will be positive definite. If A is of rank < n then A'A will be positive semidefinite (but not
positive definite).

A random normal matrix with m >= n will almost surely be of full rank; to generate a rank-deficient matrix one can append one or more columns that are linear combinations of other columns.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文