@ function handle help for finance

1 view (last 30 days)
Will
Will on 20 Jan 2012
I'm trying to pass a funtion I have created called Itrend through another function. Itrend accepts two inputs, Alpha, and Lag. It outputs the sortino ratio. The function I want to run it through is designed to maximize the sortino ratio across a predefined range. This function is called parameterSweep, the code is here. The bolded part is what I can't format correctly:
range={.02:.005:.2,1:20}
fun = @(x) deal(Itrend(Alpha,Lag))
[respmax,varmax,resp,var] = parameterSweep(fun,range)
  1 Comment
Will
Will on 20 Jan 2012
The code for parameterSweep has been added below.

Sign in to comment.

Answers (3)

Walter Roberson
Walter Roberson on 20 Jan 2012
deal() is only used to return multiple outputs, which your description does not require.
You either want
fun = @(x) ltrend(x,Lag);
or
fun = @(x) ltrend(Alpha,x);
but we cannot tell from your description which. You said "range" but not which variable the range applies to.
  1 Comment
Will
Will on 20 Jan 2012
My apologies. Alpha and Lag are both parameters that are input into Itrend. When put into Itrend, they are constants. However, the purpose of the parameterSweep is to try a whole range of each of these parameters and their combinations and maximize the output of Itrend.
With regards to the range. The term {.02:.005:.2} is the range for Alpha that the parameterSweep is meant to scan over, and {1:1:20} is the same for Lag.

Sign in to comment.


Honglei Chen
Honglei Chen on 20 Jan 2012
I don't quite see the relation of inputs. It looks like Alpha and Lag are constants and it has nothing to do with x, so what's the purpose of this function?
If Alpha and Lag is derived from range, then you may want to define it as following:
fun = @(Alpha,Lag) ltrend(Alpha,Lag)
  2 Comments
Will
Will on 20 Jan 2012
please see my answer above to Walter. I don't believe I will need to use the deal function.
Honglei Chen
Honglei Chen on 20 Jan 2012
Then it is just fun = @(Alpha,Lag) ltrend(Alpha,Lag), I'm also updating my answers above.

Sign in to comment.


Will
Will on 20 Jan 2012
function [respmax,varmax,resp,var] = parameterSweep(fun,range)
%PARAMETERSWEEP performs a parameters sweep for a given function
% RESPMAX = PARAMETERSWEEP(FUN,RANGE) takes as input a function handle in
% FUN and the range of allowable values for inputs in RANGE.
%
% FUN is a function handle that accepts one input and returns one output,
% F = FUN(X), where X is an array of size N x M. N is the number of
% observations (rows) and M is the number of variables (columns). F is
% the response of size N x 1, a column vector;
%
% RANGE is a cell array of length M that contains a vector of ranges for
% each variable.
%
% [RESPMAX,VARMAX,RESP,VAR] = PARAMETERSWEEP(FUN,RANGE) returns the
% maximum response value in RESPMAX, the location of the maximum value in
% VARMAX, an N-D array of the response values in RESP, and and N-D array
% where the size of the N-D array depends upon the values in RANGE.
% NDGRID is used to generate the arrays.
%
% Examples:
% % Example 1: peaks function
% range = {-3:0.1:3, -3:0.2:3}; % range of x and y variables
% fun = @(x) deal( peaks(x(:,1),x(:,2)) ); % peaks as a function handle
% [respmax,varmax,resp,var] = parameterSweep(fun,range);
% surf(var{1},var{2},resp)
% hold on, grid on
% plot3(varmax(1),varmax(2),respmax,...
% 'MarkerFaceColor','k', 'MarkerEdgeColor','k',...
% 'Marker','pentagram', 'LineStyle','none',...
% 'MarkerSize',20, 'Color','k');
% hold off
% xlabel('x'),ylabel('y')
% legend('Surface','Max Value','Location','NorthOutside')
%
% See also ndgrid
%% % Copyright 2010, The MathWorks, Inc. % All rights reserved.
%% Check inputs if nargin == 0 example(1) else %% Generate expression for ndgrid N = length(range); if N == 1 var = range; else in = ''; out = ''; for i = 1:N in = [in,'range{',num2str(i),'},']; out = [out,'var{',num2str(i),'},']; end in(end) = []; % remove last commas out(end) = [];
%%Evaluate ndgrid
eval( ['[',out,'] = ndgrid(',in,');'] );
end
%%Perform parameter sweep
sz = size(var{1});
for i = 1:N
var{i} = var{i}(:);
end
resp = fun(cell2mat(var));
%%Find maximum value and location
[respmax,idx] = max(resp);
for i = 1:N
varmax(i) = var{i}(idx);
end
%%Reshape output only if requested
if nargout > 2
resp = reshape(resp,sz);
for i = 1:N
var{i} = reshape(var{i},sz);
end
end %if
end %if

Community Treasure Hunt

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

Start Hunting!