在量化交易里,运算速度直接决定交易的结果好坏。
用matlab生成随机数,方法多种多样
unifrnd生成连续数据 rand*k有同样效果
unidrnd生成离散数据 ceil(rand*k)有同样的效果
下面比较一下运行效率:
k=1:10;
tic
for i=k
x1=unifrnd(0,100,1000,1000);
end
toc
tic
for i=k
x2=unidrnd(100,1000,1000);
end
toc
tic
for i=k
x3=rand(1000,1000)*100;
end
toc
tic
for i=k
x4=ceil(rand(1000,1000)*100);
end
toc
运行结果:
Elapsed time is 0.273217 seconds.
Elapsed time is 0.211033 seconds.
Elapsed time is 0.168255 seconds.
Elapsed time is 0.194122 seconds.
>> eee
Elapsed time is 0.230651 seconds.
Elapsed time is 0.201707 seconds.
Elapsed time is 0.152219 seconds.
Elapsed time is 0.196154 seconds.
>> eee
Elapsed time is 0.252402 seconds.
Elapsed time is 0.196528 seconds.
Elapsed time is 0.147061 seconds.
Elapsed time is 0.193649 seconds.
>> eee
Elapsed time is 0.227293 seconds.
Elapsed time is 0.192890 seconds.
Elapsed time is 0.148696 seconds.
Elapsed time is 0.195020 seconds.
可以看出,专门命令的效率还不如用rand搭建的效率。
所以不需要记那么多代码了,直接用rand代替吧。
当然normrnd还是要记住的,能够缩减代码长度。