Okey, guys thanks for the answers. However, I cannot understand why the code below (with the line filling G just set to some random float number) runs in 50% of the time as the one with filling of G using the b function included. Is this computation so slow?
function gen_volterra_for(a,s,n,rand_1,rand_2)
G = zeros(s+1);
Z_1 = zeros(s);
Z_2 = zeros(s+1);
for i =1:s
#G[i+1] = b(a,i+1)/(n^a);
G[i+1] = 34.74;
Z_1[i] = get_sigma_Z1(n)*rand_1[i]
Z_2[i+1] = get_sigma_Z2(a,n)*(get_rho(a)*rand_1[i]+sqrt(1-get_rho(a)^2.0)*rand_2[i]);
end
return (direct_conv(Z_1,G)[1:s+1]+Z_2);
end
and
function gen_volterra_for(a,s,n,rand_1,rand_2)
G = zeros(s+1);
Z_1 = zeros(s);
Z_2 = zeros(s+1);
for i =1:s
G[i+1] = b(a,i+1)/(n^a);
#G[i+1] = 34.74;
Z_1[i] = get_sigma_Z1(n)*rand_1[i]
Z_2[i+1] = get_sigma_Z2(a,n)*(get_rho(a)*rand_1[i]+sqrt(1-get_rho(a)^2.0)*rand_2[i]);
end
return (direct_conv(Z_1,G)[1:s+1]+Z_2);
end
Setup:
a = -0.43;
rho = -0.9;
eta = 1.9;
S_0 = 1.0
xi = 0.235^2;
n = 312;
s = round(Int,n*1.0;)
dt = 1.0/n;
mean = 0;
rand_nums = randn(3*n)
rand_1 = rand_nums[1:n]
rand_2 = rand_nums[n+1:2*n]
@btime volterra = gen_volterra_for(a,s,n,rand_1,rand_2)
Various utility functions
function b(a, k)
return (k^(a+1.0)-(k-1.0)^(a+1.0))/(a+1.0);
end
function power_ker(a, x)
return x^a;
end
function get_sigma_Z1(n)
return sqrt(1.0/n);
end
function get_sigma_Z2(a,n)
return sqrt(1.0/((2.0*a+1.0)*n^(2.0*a+1.0)))
end
function get_rho(a)
return sqrt(2.0*a+1.0)/(a+1.0)
end
function direct_conv{T}(a::Array{T}, b::Array{T})
m = length(a)
n = length(b)
c = zeros(T,m+n-1)
@inbounds @simd for j=1:m
@inbounds @simd for k=1:n
c[j+k-1] += a[j]*b[k]
end
end
return c
end