Problem about mvnrnd generating random numbers
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi, I encounter a strange problem about mvnrnd:
mu=[-6.755 0.224 -7.18 -26740 -5.21E-10 -1.03E-07];
covmatrix=[7.20E-01 1.36E-02 -1.37E-01 -3.17E+02 -5.11E-11 3.57E-08
1.36E-02 8.25E-04 2.25E-04 1.18E+01 -1.61E-12 2.07E-10
-1.37E-01 2.25E-04 1.34E-01 4.45E+01 1.76E-12 -5.05E-09
-3.17E+02 1.18E+01 4.45E+01 8.20E+05 8.12E-09 -3.61E-05
-5.11E-11 -1.61E-12 1.76E-12 8.12E-09 2.08E-19 -3.46E-16
3.57E-08 2.07E-10 -5.05E-09 -3.61E-05 -3.46E-16 5.83E-13];
num=mvnrnd(mu,covmatrix,100);
std(num(:,5))
sqrt(covmatrix(5,5))
After running the above code, mvnrnd generates a data set of the 5th column that is far beyong the 2 sigma error. This will contribute to a wrong result when I do further calculations. But when I use another similar covmatrix, it seems OK. Does anyone knows the above problem? Thanks!
Below is another covariance matrix that seems OK.
covmatrix2=[4.97E-01 3.63E-03 -1.32E-01 -3.78E+02 -2.77E-11 2.69E-08
3.63E-03 4.31E-04 1.08E-03 1.02E+01 -6.41E-13 -1.99E-10
-1.32E-01 1.08E-03 1.49E-01 5.10E+01 -1.46E-13 -4.71E-09
-3.78E+02 1.02E+01 5.10E+01 8.40E+05 1.33E-08 -3.94E-05
-2.77E-11 -6.41E-13 -1.46E-13 1.33E-08 2.33E-19 -3.91E-16
2.69E-08 -1.99E-10 -4.71E-09 -3.94E-05 -3.91E-16 6.61E-13];
num2=mvnrnd(mu,covmatrix2,100);
std(num2(:,5))
sqrt(covmatrix2(5,5))
댓글 수: 1
Torsten
2022년 5월 16일
I get a small, but negative eigenvalue for "covmatrix" (which is not the case for covmatrix2).
답변 (1개)
arushi
2024년 1월 23일
Hi Liguang,
I understand that you are facing the problem with the “mvnrnd” which generates a data set of the 5th column that is far beyond the sigma error. The problem you're encountering is likely due to the properties of the covariance matrix you are using with “mvnrnd”. The “mvnrnd” function in MATLAB generates random vectors from a multivariate normal distribution, where “mu” is the mean vector and “covmatrix” is the covariance matrix. For a covariance matrix to be valid, it must be positive semidefinite.
A positive semidefinite matrix has the property that all its eigenvalues are non-negative. If the covariance matrix is not positive semidefinite, “mvnrnd” may produce unexpected results.
If any of the eigenvalues in “eigvals” are negative, the matrix is not positive semidefinite, and this could be the source of the problem.
Another potential issue is numerical stability. The elements of your covariance matrix vary by many orders of magnitude. This can lead to numerical issues when performing matrix decompositions and other linear algebra operations. The standard deviation for the 5th column you observed (1.1832e-08) is much larger than the square root of the variance (4.5607e-10), which indicates that the generated samples are not consistent with the specified covariance.
To address this issue, you might need to regularize your covariance matrix to ensure it is positive semidefinite and to mitigate numerical stability issues. Regularization can involve adding a small value to the diagonal elements of the covariance matrix or using other techniques to adjust the matrix.
For the second covariance matrix (covmatrix2), the standard deviation of the generated samples (4.5670e-10) is closer to the square root of the variance (4.8270e-10), which suggests that it is better conditioned, or at least, the scale of the elements is such that it doesn't cause the same numerical issues.
In conclusion, you should:
- Check the eigenvalues of your covariance matrix to ensure it is positive semidefinite.
- Consider regularizing your covariance matrix if necessary.
- Be aware of the scale of the elements in your covariance matrix and the potential for numerical instability.
Please refer to the documentation of the “mvrnd” function –
Hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!