• C++ Programming for Financial Engineering
    Highly recommended by thousands of MFE students. Covers essential C++ topics with applications to financial engineering. Learn more Join!
    Python for Finance with Intro to Data Science
    Gain practical understanding of Python to read, understand, and write professional Python code for your first day on the job. Learn more Join!
    An Intuition-Based Options Primer for FE
    Ideal for entry level positions interviews and graduate studies, specializing in options trading arbitrage and options valuation models. Learn more Join!

Matlab question, invoking functions

Joined
1/1/09
Messages
111
Points
28
I'm writting a simple program that finds outliers in data and removes them.
There's this main function named out1out2, that calls two functions; outlier1, outlier2. Function out1out2 is called from command window. For some reason I keep getting a warning that there's an error in outlier1,

C++:
Error in ==> outlier1 at 4
num=length(data(1,:));

??? Output argument "S" (and maybe others) not
assigned during call to
"whatever\functions,scripts\outlier1.m>outlier1".

Error in ==> out1out2 at 14
[S,m]=outlier1(data1);
.

But a quick look at the code shows that there isn't really an error. Here's the code for outlier1;

C++:
function [S,m] = outlier1(data)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
num=length(data(1,:));
W=ones(1,num)/num;
W1=W;
pom=1;
while norm((W1-W),2)>0.05 || pom==1
    pom=2;
    W=W1;
    m=zeros(num,1);
    S=zeros(num,num);
    for i=1:num
        m=m+W(i)*data(:,i);
    end
    m=m/sum(W);
    for i=1:num    
        S=S+W(i)*((data(:,i)-m)*(data(:,i)-m)');
    end
    Ma2=zeros(num,1);
    for i=1:num
        Ma2(i)=(data(:,i)-m)'*inv(S)*(data(:,i)-m);
        if Ma2(i)>1
            W1(i)=W(i)*Ma2(i);
        end
    end

end
end

and out1out2

C++:
function [ data, S, m ] = out1out2(data1)
%UNTITLED3 Summary of this function goes here
%   Detailed explanation goes here
num=length(data1(1,:));
temp=num;
l=1;
detcov=zeros(num,1);
detS=zeros(num,1);
while temp>num/2 
Cov=cov(data1');
detcov(l)=det(Cov);
[S,m]=outlier1(data1);
detS(l)=det(S);
[data,c]=outlier2(data1);
temp=length(data(1,:));
l=l+1;
end
figure
subplot(4,2,[1 2 3 4])
bar(1:l,detcov)
subplot(4,2,[5 6 7 8])
bar(1:l,detS)
end
Do I need to "connect" functions outlier1m, outlier2 to out1out2 or something else? Maybe this is a simple question, but somehow I'm not getting what's wrong with the code. I'm grateful for any help.
 
I haven't figured out what was wrong...I decided to merge the three functions into one (huge!) function. As I suspected the code is working fine now (I tweaked the code a bit, just to make it clearer). If anyone is interested, here's the code;

C++:
function [ data, S, m ] = out1out2(data1)
%UNTITLED3 Summary of this function goes here
%   Detailed explanation goes here
clear detcov detS;

num=length(data1(1,:));
num1=length(data1(:,1));
fixtemp=length(data1(1,:));
temp=num;
l=1;


while temp>450
Cov=cov(data1');
detcov(l)=det(Cov);
%outlier1
W=ones(1,num)/num;
W1=W;
pom=1;
while norm((W1-W),2)>0.1 || pom==1
    pom=2;
    W=W1;
    m=zeros(num1,1);
    S=zeros(num1,num1);
    for i=1:num
        m=m+W(i)*data1(:,i);
    end
    m=m/sum(W);
    for i=1:num    
        S=S+W(i)*((data1(:,i)-m)*(data1(:,i)-m)');
    end
    Ma2=zeros(num,1);
    for i=1:num
        Ma2(i)=((data1(:,i)-m)'/S)*(data1(:,i)-m);
        if Ma2(i)>1
            W1(i)=W(i)*Ma2(i);
        end
    end

end
%outlier1 end
detS(l)=det(S);
%outlier2
m=mean(data1')';
for i=1:num
    U(i,:)=data1(:,i)'-m';
end
[c,index]=max(diag(U/(U'*U)*U'));
data1(:,index)=[];
data=data1;
num=length(data1(1,:));
%outlier2 end
temp=length(data(1,:));
l=l+1;
end
figure
subplot(4,2,[1 2 3 4])
bar(detcov)
subplot(4,2,[5 6 7 8])
bar(detS)
end
Please note that the code is inspired by algorithm in Attilio Meucci's book, "Risk and Asset Allocation" (section 4.6.1. Detection of outliers, page 223-229, folks from ARPM '09 might remember it ;)). He suggested that the routine runs until the number of data left is less than half the original number, I ran into problems with inverting matrix S (named in the book), see line 34 of the code, so I decided to set that we remove 50 outliers (otherwise set fixtemp/2 instead of 450, see line 13 of the code) out of 500 data. If you have any comments, suggestions, please let me know.
 
I would like to help you analyzing the code, but not willing to spend 30 minutes deciphering it, without any commenting and with nondescript function and variable names like "outlier1" "outlier2" etc. I'm working at a small firm but even among a small team it is necessary for us to write code that is quickly readable by other people.

Maybe everyone else is able to read and understand uncommented code an instant, but for the moment it is beyond my abilities.

Also I don't have the book you are referring to so it would help if you explain a little bit about the algorithm.
 
Thank you, for even taking the time to read my posts.

I'll try to make the question clear (without refering to any specific functions, like outlier1, outlier2, out1out2 in my case).

Suppose I have three functions, named function1, function2, function3. Each function consists of some code, function1 being special by the fact that it calls function2, function3 somewhere in the code. Function2, function3 do not call (or in any way refer to) function1 or each other.
Do I need to write some code in function1, in order to recognize the fact that I'm refering to functions function2 and function3, that are not a part of standard set of functions?
 
if everything is in the same directory, you will be ok calling the other functions so long as the following things are equal:

1. filename
2. function name within the file
3. function name use when calling

so in your directory, if you have:
function1.m
function2.m
function3.m

the first line of function2.m would be this:
[output1,output2] = function2(input1,input2)
the first line of function3.m would be this:
[output1,output2] = function3(input1,input2)

when calling these from function1.m:
[x,y] = function2(a,b)
[x,y] = function3(a,b)
if you don't specify where to receive the outputs of each function then the output is stored as the variable 'ans'


if you have them in different directories, you will need to use the addpath command
for example, if you have function2 and function3 in c:\functions\

addpath c:\functions\
then you would be able to call them normally as if they were built in functions
 
Back
Top