error statement is not inside anny function

조회 수: 19 (최근 30일)
Manuel
Manuel 2023년 11월 22일
답변: Dyuman Joshi 2023년 11월 22일
%----------------------------------------
% Definición de funciones
%----------------------------------------
function option_price = monteCarloSimulation_withProjection(S0, K, r, T, sigma, num_trajectories)
dt = T / 252;
num_steps = round(T / dt);
price_paths = zeros(num_steps + 1, num_trajectories);
price_paths(1, :) = S0;
for i = 1:num_steps
dW = sqrt(dt) * randn(1, num_trajectories);
price_paths(i + 1, :) = price_paths(i, :) .* exp((r - (sigma^2) / 2) * dt + sigma * dW);
end
option_payoffs = max(price_paths(end, :) - K, 0);
option_price = exp(-r * T) * mean(option_payoffs);
end
function option_price = calculateBS_withProjection(S0, K, r, T, sigma, option_type)
S_T = S0 * exp((r - (sigma^2) / 2) * T + sigma * sqrt(T) * randn);
if strcmpi(option_type, 'call')
option_price = exp(-r * T) * max(S_T - K, 0);
elseif strcmpi(option_type, 'put')
option_price = exp(-r * T) * max(K - S_T, 0);
else
error('Tipo de opción no válido. Use "call" o "put".');
end
end
%----------------------------------------
% Código principal
%----------------------------------------
% Parámetros
S0 = 100; % Precio inicial
This statement is not inside any function.
(It follows the END that terminates the definition of the function "calculateBS_withProjection".)
K = 100; % Precio de ejercicio
r = 0.05; % Tasa libre de riesgo
T = 1; % Tiempo al vencimiento
sigma = 0.2; % Volatilidad
% Cálculos de opciones
call_BS = calculateBS_withProjection(S0, K, r, T, sigma, 'call');
put_BS = calculateBS_withProjection(S0, K, r, T, sigma, 'put');
[call_MC, put_MC] = monteCarloSimulation_withProjection(S0, K, r, T, sigma, 20000);
% Comparación con precios de mercado
market_price_call = 5; % Precio de mercado para la CALL
market_price_put = 3; % Precio de mercado para la PUT
% Mostrar resultados
fprintf('Precio de mercado para la CALL: %f\n', market_price_call);
fprintf('Precio BYS para la CALL: %f\n', call_BS);
fprintf('Precio Montecarlo para la CALL: %f\n', call_MC);
fprintf('Precio de mercado para la PUT: %f\n', market_price_put);
fprintf('Precio BYS para la PUT: %f\n', put_BS);
fprintf('Precio Montecarlo para la PUT: %f\n', put_MC);

답변 (1개)

Dyuman Joshi
Dyuman Joshi 2023년 11월 22일
Any functions defined in a script must be at the end of the script.
Also, the function monteCarloSimulation_withProjection is defined to give a single output only, whereas you have called it to give two outputs, which results in an error. So, you might want to edit the function or the call according to your need.
%----------------------------------------
% Código principal
%----------------------------------------
% Parámetros
S0 = 100; % Precio inicial
K = 100; % Precio de ejercicio
r = 0.05; % Tasa libre de riesgo
T = 1; % Tiempo al vencimiento
sigma = 0.2; % Volatilidad
% Cálculos de opciones
call_BS = calculateBS_withProjection(S0, K, r, T, sigma, 'call');
put_BS = calculateBS_withProjection(S0, K, r, T, sigma, 'put');
[call_MC, put_MC] = monteCarloSimulation_withProjection(S0, K, r, T, sigma, 20000);
Error using solution>monteCarloSimulation_withProjection
Too many output arguments.
% Comparación con precios de mercado
market_price_call = 5; % Precio de mercado para la CALL
market_price_put = 3; % Precio de mercado para la PUT
% Mostrar resultados
fprintf('Precio de mercado para la CALL: %f\n', market_price_call);
fprintf('Precio BYS para la CALL: %f\n', call_BS);
fprintf('Precio Montecarlo para la CALL: %f\n', call_MC);
fprintf('Precio de mercado para la PUT: %f\n', market_price_put);
fprintf('Precio BYS para la PUT: %f\n', put_BS);
fprintf('Precio Montecarlo para la PUT: %f\n', put_MC);
%% Moved all the functions to the end of the script
%----------------------------------------
% Definición de funciones
%----------------------------------------
function option_price = monteCarloSimulation_withProjection(S0, K, r, T, sigma, num_trajectories)
dt = T / 252;
num_steps = round(T / dt);
price_paths = zeros(num_steps + 1, num_trajectories);
price_paths(1, :) = S0;
for i = 1:num_steps
dW = sqrt(dt) * randn(1, num_trajectories);
price_paths(i + 1, :) = price_paths(i, :) .* exp((r - (sigma^2) / 2) * dt + sigma * dW);
end
option_payoffs = max(price_paths(end, :) - K, 0);
option_price = exp(-r * T) * mean(option_payoffs);
end
function option_price = calculateBS_withProjection(S0, K, r, T, sigma, option_type)
S_T = S0 * exp((r - (sigma^2) / 2) * T + sigma * sqrt(T) * randn);
if strcmpi(option_type, 'call')
option_price = exp(-r * T) * max(S_T - K, 0);
elseif strcmpi(option_type, 'put')
option_price = exp(-r * T) * max(K - S_T, 0);
else
error('Tipo de opción no válido. Use "call" o "put".');
end
end

태그

Community Treasure Hunt

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

Start Hunting!

Translated by