Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Improving code execution: vectorization
조회 수: 1 (최근 30일)
이전 댓글 표시
I have these functions that calculate bond yields. In the code below, the most simple one is displayed, although there are similar function with more complicated code inside the loop. Execution time is of the essence as the functions go into larger optimization problems. The performance is highly dependent on the maturity input, which translates into the # of loops. Now, my question is whether it is somehow possible to rewrite this function - such as vectorization instead of looping - that will result in improvements of performance?
function [Yield] = Yield_shadowrate_FO(...
alpha, beta, mu, phi, sigma, maturity, X)
N = size(X, 1);
s_mean = zeros(maturity-1, 1);
s_var = zeros(maturity-1, 1);
a = zeros(maturity-1, 1);
FO = zeros(maturity-1, 1);
for k = 1:maturity
if k == 1
short_rate = max(alpha+beta' * X,0);
else
i = (k-1);
X_mean = mu + (eye(N)-phi)^i*X - (phi\(eye(N)-phi)^i)*phi*mu;
S = (eye(N*N)-kron(eye(N)-phi,eye(N)-phi)) \ ...
(eye(N*N)-kron(eye(N)-phi,eye(N)-phi)^i)* ...
reshape(sigma*sigma', N*N, 1);
X_var = reshape(S, N, N);
s_mean(i) = alpha + beta' * X_mean;
s_var(i) = beta' * X_var * beta;
a(i) = s_mean(i) / s_var(i)^0.5;
FO(i)=s_mean(i)*0.5*erfc(-a(i)/sqrt(2))+s_var(i)^0.5*normpdf(a(i));
end
end
Yield = 1/maturity*(short_rate+sum(FO,1));
end
댓글 수: 1
Geoff Hayes
2015년 3월 1일
Anders - please comment on the performance of the above code and give us some indication of the size/dimension of each of the inputs alpha, beta, mu, phi, sigma, maturity, X. How often do you call the above function and how long does it take to execute it? Have you tried to profile the code?
답변 (0개)
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!