Common error message: "Index exceeds matrix dimensions"

3 vues (au cours des 30 derniers jours)
N/A
N/A le 22 Nov 2018
Commenté : N/A le 22 Nov 2018
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.

Réponse acceptée

Torsten
Torsten le 22 Nov 2018
"Market_Price" is a one-dimensional array ; you try to access "Market_Price(t,k)".
  1 commentaire
N/A
N/A le 22 Nov 2018
Ops I missed it! Thank you sir.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Financial Toolbox dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by