COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### Undefined Function or Method

• Email
• Follow

```I have written a function to calculate a vector of weights, a portion of which includes the following code:

port_var_diff = @(h)((2.*sum(h)-2).*s1.^2+(2-4.*sum(h)).*s1.*s2(h).*rho+2.*sum(h).*s2(h).^2)

and

target = @(h)(port_var_diff(h).*(b1(h)+b2(h).*port_skew(h)+b3(h).*port_kurt(h)+b4(h).*port_skew(h).^2)+sqrt(port_var(h)).*(b2(h).*port_skew_diff(h)+b3(h).*port_kurt_diff(h)+2.*b4(h).*port_skew(h).*port_skew_diff(h))).^2;

I'm getting the old:

??? Undefined function or method 'times' for input arguments of type 'function_handle'.

But I can't figure out what the problem is - I've gone through and changed multiplication to scalars. Is this an issue with patternsearch?
```
 0

See related articles to this posting

```On 04/01/11 1:30 PM, J. wrote:
> I have written a function to calculate a vector of weights, a portion of
> which includes the following code:
>
> port_var_diff =
> @(h)((2.*sum(h)-2).*s1.^2+(2-4.*sum(h)).*s1.*s2(h).*rho+2.*sum(h).*s2(h).^2)
>
>
> and
>
> target =
> @(h)(port_var_diff(h).*(b1(h)+b2(h).*port_skew(h)+b3(h).*port_kurt(h)+b4(h).*port_skew(h).^2)+sqrt(port_var(h)).*(b2(h).*port_skew_diff(h)+b3(h).*port_kurt_diff(h)+2.*b4(h).*port_skew(h).*port_skew_diff(h))).^2;
>
>
>
> I'm getting the old:
>
> ??? Undefined function or method 'times' for input arguments of type
> 'function_handle'.
>
> But I can't figure out what the problem is - I've gone through and
> changed multiplication to scalars. Is this an issue with patternsearch?

How are you calling "target" ? What are you passing in to it?
```
 0

```"J. " <jay.womack.wg07@wharton.upenn.edu> wrote in message <ifvsge\$lks\$1@fred.mathworks.com>...
> I have written a function to calculate a vector of weights, a portion of which includes the following code:
>
> port_var_diff = @(h)((2.*sum(h)-2).*s1.^2+(2-4.*sum(h)).*s1.*s2(h).*rho+2.*sum(h).*s2(h).^2)
>
> and
>
>
> target = @(h)(port_var_diff(h).*(b1(h)+b2(h).*port_skew(h)+b3(h).*port_kurt(h)+b4(h).*port_skew(h).^2)+sqrt(port_var(h)).*(b2(h).*port_skew_diff(h)+b3(h).*port_kurt_diff(h)+2.*b4(h).*port_skew(h).*port_skew_diff(h))).^2;
>
>
> I'm getting the old:
>
>  ??? Undefined function or method 'times' for input arguments of type 'function_handle'.
>
> But I can't figure out what the problem is - I've gone through and changed multiplication to scalars. Is this an issue with patternsearch?

No.

However, but as a start, you do not tell us what are s1
and s2, nor do you show us the definitions for your other
functions.

John
```
 0

```Here is the code:

r2 = @(h)(h*mean(hedge_returns_1)');
s2 = @(h)(h*cov(hedge_returns_1)*h');
rho = @(h)(corr(portfolio,repmat(repmat(1/size(hedge_returns,2),1,size(hedge_returns,2)),size(hedge_returns_1,1),1).*hedge_returns_1*ones(size(hedge_returns_1,2),1),'type','Spearman'));
skew1 = r1^3/s1^3;
skew2 = @(h)(r2(h).^3/s2(h).^3);
skew_a = @(h)((r1^2*r2(h))/(s1^2*s2));
skew_b = @(h)((r1*r2(h).^2)/(s1*s2^2));
kurt1 = r1^4/s1^4;
kurt2 = @(h)(r2(h).^4/s2(h).^4);
kurt_a = @(h)((r1^3*r2(h))/(s1^3*s2(h)));
kurt_b = @(h)((r1^2*r2(h).^2)/(s1^2*s2(h).^2));
kurt_c = @(h)((r1*r2(h).^3)/(s1*s2(h).^3));

%Set up formulas that take hedge ratio as an input to calculate hedge ratio
port_ret = @(h)((1-sum(h))*r1+h*r2(h));
port_var = @(h)((1-sum(h))^2*s1^2 + sum(h)^2*s2(h).^2 + 2*sum(h)*rho*s1*s2(h).*(1-sum(h)));
var_pctl = @(h)(port_ret(h)+norminv(var_p,0,1)*sqrt(port_var(h)));
max_pctl = @(h)(port_ret(h)+norminv(10e-300,0,1)*sqrt(port_var(h)));
port_var_diff = @(h)((2.*sum(h)-2).*s1.^2+(2-4.*sum(h)).*s1.*s2(h).*rho+2.*sum(h).*s2(h).^2);
port_skew = @(h)((skew1*s1^3-3*sum(h)*skew_a*s1^2*s2(h)+3*sum(h)^2*skew_b*s1*s2(h).^2-sum(h)^3*skew2*s2(h).^3)/(s1^2+sum(h)^2*s2(h).^2+2*sum(h)*rho*s1*s2(h))^(3/2));
port_skew_diff = @(h)(- (3*skew2*sum(h)^2*s2(h).^3 - 6*skew_b*sum(h)*s1*s2(h).^2 + 3*skew_a*s1^2*s2(h))/(sum(h)^2*s2(h).^2 + 2*rho*sum(h)*s1*s2(h) + s1^2)^(3/2) - (3*(2*sum(h)*s2(h).^2 + 2*rho*s1*s2(h))*(- skew2*sum(h)^3*s2(h).^3 + 3*skew_b*sum(h)^2*s1*s2(h).^2 - 3*skew_a*sum(h)*s1^2*s2(h) + skew1*s1^3))/(2*(sum(h)^2*s2(h).^2 + 2*rho*sum(h)*s1*s2(h) + s1^2)^(5/2)));
port_kurt = @(h)((kurt1*s1^4-4*sum(h)*kurt_a*s1^3*s2(h)+6*sum(h)^2*kurt_b*s1^2*s2(h).^2-4*sum(h)^3*kurt_c*s1*s2(h).^3+sum(h)^4*kurt2*s2(h).^4)/((1-sum(h))^2*s1^2+sum(h)^2*s2(h).^2+2*sum(h)*rho*s1*s2(h)));
port_kurt_diff = @(h)(- (- 4*kurt2*sum(h)^3*s2(h).^4 + 12*kurt_c*sum(h)^2*s1*s2(h).^3 - 12*kurt_b*sum(h)*s1^2*s2(h).^2 + 4*kurt_a*s1^3*s2(h))/(s1^2*(sum(h) - 1)^2 + sum(h)^2*s2(h).^2 + 2*sum(h)*rho*s1*s2(h)) - (((2*sum(h) - 2)*s1^2 + 2*rho*s1*s2(h) + 2*sum(h)*s2(h).^2)*(kurt2*sum(h)^4*s2(h).^4 - 4*kurt_c*sum(h)^3*s1*s2(h).^3 + 6*kurt_b*sum(h)^2*s1^2*s2(h).^2 - 4*kurt_a*sum(h)*s1^3*s2(h) + kurt1*s1^4))/(s1^2*(sum(h) - 1)^2 + sum(h)^2*s2(h).^2 + 2*sum(h)*rho*s1*s2(h))^2);
m1 = @(h)(1/var_p* sqrt(port_var(h))*(2*sqrt(port_var(h))*(exp(-(max_pctl(h)-port_ret(h))^2/(2*port_var(h)))-exp(-(var_pctl(h)-port_ret(h))^2/(2*port_var(h))))-sqrt(2*pi)*port_ret(h)*erf((max_pctl(h)-port_ret(h))/(sqrt(2)*sqrt(port_var(h))))+sqrt(2*pi)*port_ret(h)*erf((var_pctl(h)-port_ret(h))/(sqrt(2)*sqrt(port_var(h)))))/(2*sqrt(2*pi)*sqrt(port_var(h))));
m2 = @(h)(1/var_p* (1/sqrt(2*pi*port_var(h)))*(.5*sqrt(port_var(h))*exp(-(var_pctl(h)^2+port_ret(h)^2)/(2*port_var(h)))*(sqrt(2*pi)*(port_var(h)+port_ret(h)^2)*exp((var_pctl(h)^2+port_ret(h)^2)/(2*port_var(h)))*erf((var_pctl(h)-port_ret(h))/(sqrt(2)*sqrt(port_var(h))))-2*sqrt(port_var(h))*(var_pctl(h)+port_ret(h))*exp((var_pctl(h)*port_ret(h))/port_var(h)))-.5*sqrt(port_var(h))*exp(-(max_pctl(h)^2+port_ret(h)^2)/(2*port_var(h)))*(sqrt(2*pi)*(port_var(h)+port_ret(h)^2)*exp((max_pctl(h)^2+port_ret(h)^2)/(2*port_var(h)))*erf((max_pctl(h)-port_ret(h))/(sqrt(2)*sqrt(port_var(h))))-2*sqrt(port_var(h))*(max_pctl(h)+port_ret(h))*exp((max_pctl(h)*port_ret(h))/port_var(h)))));
m3 = @(h)(1/var_p* (1/sqrt(2*pi*port_var(h)))*(.5*sqrt(port_var(h))*exp(-(var_pctl(h)^2+port_ret(h)^2)/(2*port_var(h)))*(sqrt(2*pi)*port_ret(h)*(3*port_var(h)+port_ret(h)^2)*exp((var_pctl(h)^2+port_ret(h)^2)/(2*port_var(h)))*erf((var_pctl(h)-port_ret(h))/(sqrt(2)*sqrt(port_var(h))))-2*sqrt(port_var(h))*(var_pctl(h)^2+var_pctl(h)*port_ret(h)+2*port_var(h)+port_ret(h)^2)*exp((var_pctl(h)*port_ret(h))/port_var(h)))-.5*sqrt(port_var(h))*exp(-(max_pctl(h)^2+port_ret(h)^2)/(2*port_var(h)))*(sqrt(2*pi)*port_ret(h)*(3*port_var(h)+port_ret(h)^2)*exp((max_pctl(h)^2+port_ret(h)^2)/(2*port_var(h)))*erf((max_pctl(h)-port_ret(h))/(sqrt(2)*sqrt(port_var(h))))-2*sqrt(port_var(h))*(max_pctl(h)^2+max_pctl(h)*port_ret(h)+2*port_var(h)+port_ret(h)^2)*exp((max_pctl(h)*port_ret(h))/port_var(h)))));
b1 = @(h)(m1(h) -(1/8)*(m3(h)-3*m1(h)));
b2 = @(h)((1/6)*(m2(h)-1));
b3 = @(h)((1/24)*(m3(h)-3*m1(h)));
b4 = @(h)(-(1/36)*(2*m3(h)-5*m1(h)));

%Establish formula to calculate hedge ratio
target = @(h)(port_var_diff(h).*(b1(h)+b2(h).*port_skew(h)+b3(h).*port_kurt(h)+b4(h).*port_skew(h).^2)+sqrt(port_var(h)).*(b2(h).*port_skew_diff(h)+b3(h).*port_kurt_diff(h)+2.*b4(h).*port_skew(h).*port_skew_diff(h))).^2;

%Write function to calculate nonlinear constraints
function [c,ceq] = nonlcon(h)
c = [];
ceq = (vol_tgt^2)/12-(s1^2*(1-sum(h))^2 + sum(h)^2*s2(h).^2 - 2*sum(h)*rho*s1*s2(h).*(1-sum(h)));
end

%Input dummy value of hedge ratio for fsolve
h0 = repmat(1/size(hedge_returns,2),1,size(hedge_returns,2));

%Set options for optimization
options = psoptimset( 'MaxFunEvals', 25000,'MaxIter',5000,'TolFun',10e-250);

[weights]= patternsearch(target,h0,[], [], [], [], 0, 1, @(h)nonlcon(h),options)

s1 and s2 are toward the top; I'm calling target in patternsearch - the optimization seems to be what's triggering the error because the error code shows the function error occuring in the optimization function file....
```
 0

```On 11-01-04 02:41 PM, J. wrote:
> Here is the code:
>
> r2 = @(h)(h*mean(hedge_returns_1)');
> s2 = @(h)(h*cov(hedge_returns_1)*h');

I do not see an initialization for s1 ?

> skew_a = @(h)((r1^2*r2(h))/(s1^2*s2));
> skew_b = @(h)((r1*r2(h).^2)/(s1*s2^2));

But those two lines clearly multiply by s2, which is a function handle. You
probably need s2(h) in those two lines.

I did not bother looking further.
```
 0

```Walter Roberson <roberson@hushmail.com> wrote in message <ig01u9\$4vg\$1@nrc-news.nrc.ca>...
> On 11-01-04 02:41 PM, J. wrote:
> > Here is the code:
> >
> > r2 = @(h)(h*mean(hedge_returns_1)');
> > s2 = @(h)(h*cov(hedge_returns_1)*h');
>
> I do not see an initialization for s1 ?
>
> > skew_a = @(h)((r1^2*r2(h))/(s1^2*s2));
> > skew_b = @(h)((r1*r2(h).^2)/(s1*s2^2));
>
> But those two lines clearly multiply by s2, which is a function handle. You
> probably need s2(h) in those two lines.
>
> I did not bother looking further.

Thank you for pointing that out. s1 is actually not a function, it's a scalar value calculated and assigned to the variable s1. I am going to print out m-file and go through to identify the other places where I've called out s2 without treating as a function.
```
 0

```I just wanted to thank you both for the help; I was modifying an existing file and hadn't gone through it with a fine-toothed comb (sorry - second night in a row on less than 4 hours sleep). Modified all functions and used scalar multipliers, etc, and function works just fine.

Thank you.
```
 0

```it could be that you dont have the liecence of the tool required
print the command :
which -all times
to see if you have the liecence for it
good luck
:)
```
 0