Common error message: "Index exceeds matrix dimensions"
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello dear MATLAB users from the community, I am having few trouble programming an objective function for my master thesis and hope someone could help me out with this. What I need to do, is to solve a classic least squares minimization problem, where the objective function is calculated within a function and then passed to the main script.
This is the code for the main script:
clc;
clear;
tic
% necessary inputs for the SV Objective function
S=xlsread('NDX_calls_dati','<60 days','B1');
K=xlsread('NDX_calls_dati','<60 days','D4:D14')';
tau=xlsread('NDX_calls_dati','<60 days','C4:C14')';
Market_Price=xlsread('NDX_calls_dati','<60 days','E4:E14')';
V=xlsread('NDX Dollar Price History','18 years','M22');
R=0.0216; % 4 WEEKS rf at 31/10
NK=length(K);
NT=length(tau);
k_v = 1.15; theta_v = 0.04; sigma_v= 0.39; rho= -0.64;
[ Loss_SV ] = SV_ObjFun( NK, NT, Market_Price, S, K, R, V, k_v,...
theta_v, sigma_v, rho, tau )
toc
The "SV_ObjFun" is structured as follows:
function [ y ] = SV_ObjFun( NK, NT, Market_Price, S, K, R, V, k_v,...
theta_v, sigma_v, rho, tau )
% The function implements the loss function for the SV specification
% see: Fabrice D. R - The Heston Model and Its Extensions in Matlab and C#
% pp 149-150
% Set of NT maturities, NK strikes
% S, K(k), R, V, k_v, theta_v,sigma_v, rho, tau(k),NExp
% specify the parameters inside
% NT and NK have equal dimensions
for t = 1: NT
for k = 1:NK
Model_Price(t,k) = Call_SVMC(S, K(t), R, V, k_v, theta_v, sigma_v,...
rho, tau(t),1000); % NExp chosen by user
% compute the error from market and model prices for each iteration
error(t,k) = (Market_Price(t,k)-Model_Price(t,k))^2;
end
end
y = sum(sum(error))/(NT*NK);
end
As I run the code to calculate the loss "Loss_SV", the error pops out:
Index exceeds matrix dimensions.
Error in SV_ObjFun (line 19)
error(t,k) = (Market_Price(t,k)-Model_Price(t,k))^2;
Can anyone please help me understand what's happening and how to solve it? Thank you in advance.
0 Kommentare
Akzeptierte Antwort
Torsten
am 22 Nov. 2018
"Market_Price" is a one-dimensional array ; you try to access "Market_Price(t,k)".
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Financial Toolbox finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!