AHi,
I need to generate a gaussian correlated noise based on a covariance matrix with fortran. I am using spotrf subroutine to do the cholesky decomposition. However, the decomposition is always failed at a specific row of input matrix. As the reference indicates, it means the leading minors of that row is not positive definite. But previous work with the same data in matlab chol function shows that the matrix is positive definite.
This problem is killing me now. Can anyone help me get out of the swap ? Or just if there is a better way to generate correlated noise without getting involved in the covariance matrix.The code for generate the covariance matrix and cholesky decomposition is listed below. Thanks a lot!!
-------------------------------------------------------------------------------------------
Program DensityRealization
Implicit None
Real::lc
Integer::std,cases,Layer_Num,i,j,CholFlag
Real,Allocatable,Dimension(:,:)::c,sigma,rhostd
Real,Allocatable,Dimension(:)::z,Rnd
REAL,Parameter::p3=38.2273
Layer_Num=13827
std=20
cases=500
Allocate(z(Layer_Num),c(Layer_Num,Layer_Num),sigma(Layer_Num,Layer_Num),rhostd(Layer_Num,1),Rnd(cases*Layer_Num))
DO i=1,10000
z(i)=(i-1)*0.01
END DO
DO i=10001,11799
z(i)=100+(i-10000-1)*0.5
END DO
DO i=11800,Layer_Num
z(i)=1000+(i-11799-1)
END DO
! coveriance matrix
Do i=1,Layer_Num
Do j=1,i
c(i,j)=abs(z(i)-z(j))
c(j,i)=c(i,j)
End DO
End DO
c=-c/lc
c=exp(c)
rhostd(:,1)=std*exp(-z/p3)
sigma=matmul(rhostd,TRANSPOSE(rhostd))
sigma=sigma*c
!cholesky decomposition
CALL SPOTRF('U',Layer_Num,sigma,Layer_Num,CholFlag)
print*,CholFlag
End