英文:
How to create noise with time dependent correlation in Julia?
问题
我想创建满足性质 $E[xi_t\xi_s] = f(t, s)$ 的实际噪声 $\xi(t)$,并且我想在 Julia 中实现它。是否有库或函数可以帮助我实现这个?
我尝试使用 $f(t, s) = e^{-(t-s)}$ 进行实现,但我的代码不起作用(噪声呈指数下降)。
using LinearAlgebra
function generate_gaussian_noise(timesteps)
n = length(timesteps)
noise = zeros(n)
for i in 1:n
t = timesteps[i]
noise[i] = real(randn())
end
return noise
end
function construct_correlated_noise(timesteps)
n = length(timesteps)
noise = generate_gaussian_noise(timesteps)
correlation_matrix = zeros(n, n)
for i in 1:n
for j in 1:n
correlation_matrix[i, j] = exp(-(timesteps[i] - timesteps[j]))
end
end
# 执行特征值分解
eigenvalues, eigenvectors = eigen(correlation_matrix)
D = Diagonal(sqrt.(eigenvalues))
Q = eigenvectors
L = Q * D
correlated_noise = L * noise
return correlated_noise
end
# 示例用法
timesteps = collect(1:401)
correlated_noise = construct_correlated_noise(timesteps)
希望这可以帮助你。
英文:
I want to create real noise $\xi(t)$ which fulfills the property $E[xi_t\xi_s] = f(t, s)$ in Julia. Is there a library or function with which I could implement this?
I tried the impementation with $f(t, s) = e^{-(t-s)}$
My code is not working (the noise is decreasing exponentially).
using LinearAlgebra
function generate_gaussian_noise(timesteps)
n = length(timesteps)
noise = zeros(n)
for i in 1:n
t = timesteps[i]
noise[i] = real(randn())
end
return noise
end
function construct_correlated_noise(timesteps)
n = length(timesteps)
noise = generate_gaussian_noise(timesteps)
correlation_matrix = zeros(n, n)
for i in 1:n
for j in 1:n
correlation_matrix[i, j] = exp(-(timesteps[i] - timesteps[j]))
end
end
# Perform eigenvalue decomposition
eigenvalues, eigenvectors = eigen(correlation_matrix)
D = Diagonal(sqrt.(eigenvalues))
Q = eigenvectors
L = Q * D
correlated_noise = L * noise
return correlated_noise
end
# Example usage
timesteps = collect(1:401)
correlated_noise = construct_correlated_noise(timesteps)
答案1
得分: 4
使用Distributions库
f(t, s) = exp(-abs(t-s))
时间步长 = 1:401;
协方差矩阵 = f.(时间步长, 时间步长');
均值向量 = zeros(length(时间步长));
d = MvNormal(均值向量, 协方差矩阵);
x = rand(d) # 这是一个样本
英文:
If I understand you correctly you want something like this?
using Distributions
f(t, s) = exp(-abs(t-s))
timesteps = 1:401;
covariance_matrix = f.(timesteps, timesteps');
mean_vector = zeros(length(timesteps));
d = MvNormal(mean_vector, covariance_matrix);
x = rand(d) # this is a single sample
Note that f(t,s)
must have a property that f(t,s)=f(s,t)
which your function did not have, that is why I have added abs
. I also modelled covariance because what you defined is not correlation but covariance assuming that the expected value of the process is constant and equal to 0.
答案2
得分: 4
看起来你只是想要模拟一个具有任意相关结构的零均值高斯向量。你可以使用 Distributions.jl 包来完成这个任务。例如:
using Distributions
numobs = 100;
muvec = zeros(Float64, numobs);
covmat = Matrix{Float64}(I, numobs, numobs);
x = rand(MvNormal(muvec, covmat));
当然,你需要根据你想要的协方差矩阵结构来填充 covmat
行。我只是在这里使用了一个对角协方差矩阵的代码,但你可以按照自己的需求进行修改。
如果你有更具体的相关结构,例如自回归结构,那么有更高效的方法可以实现。但对于任意结构,这 可能 是最好和最简单的方法。
英文:
It looks like you're just asking how to simulate a zero-mean Gaussian vector with arbitrary correlation structure. You can just use the Distributions.jl package to do this. For example:
using Distributions
numobs = 100;
muvec = zeros(Float64, numobs);
covmat = Matrix{Float64}(I, numobs, numobs);
x = rand(MvNormal(muvec, covmat);
You will, of course, need to populate the covmat
line with whatever covariance matrix structure you are after. I've just used code for a diagonal covariance matrix here, but you can do whatever you want.
If you have a more specific correlation structure in mind, e.g. maybe an autoregressive structure, then there will be more efficient ways to go about this. But for an arbitrary structure, this is probably the best and easiest approach.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论