Hi, I would like to creat a function that calculate fn. I would like to assign 1 to s when x>v and 0 otherwise.
[fn]=lillietest1(daily_logreturns);
function [fn] = lillietest1(x)
n=lenght(x);
x=sort(x);
for i=1:n
v(i)=i/n;
if x(i)>=v(i)
s(i)=1;
else
s(i)=0;
end
end
fn=sum(s)/n;
end

 채택된 답변

Alan Stevens
Alan Stevens 2020년 11월 11일

0 개 추천

I don't have your daily log returns vector, so I can't check your result. You are the only person who can decide if the function makes sense!!

댓글 수: 5

I created test function and it doesn't work properly.
clear;
clc;
bd=[2,3,4,5,3,1,4,5,6];
[fn]=lillietest1(bd);
function [fn] = lillietest1(x)
n=length(x);
x=sort(x);
for i=1:n
v(i)=i/n;
if x(i)>=v(i)
s(i)=1;
else
s(i)=0;
end
end
fn=sum(s)/n;
end
Alan Stevens
Alan Stevens 2020년 11월 11일
Sure it works!
first it sorts bd in order, so x = [1 2 3 3 4 4 5 5 6]; then it compares x(1) = 1 with 1/9, and since 1>1/9 them s(1) = 1. Then it compares x(2) = 2 with 2/9 and since 2>2/9 then s(2) = 1; and so on through the list. With this list every s is 1, so the final result, fn = 1.
If you change, let's say one of the 4s to -4, then you would find fn = 8/9 (0.8889 approx).
So the function works. However, it might not be doing what you thought you were asking it to do - but only you know what you thought you were asking it to do!
Julian Wzorek
Julian Wzorek 2020년 11월 11일
Thanks a lot for all the time. Could you just check if this matrix works for you? I still get 0 for fn :C.
Alan Stevens
Alan Stevens 2020년 11월 11일
편집: Alan Stevens 2020년 11월 11일
I've just done that and the value of 0 is correct for daily_logreturns, as you can see by the graph plotted by the modified code below. However, if you look at daily_prices instead, the value returned is 0.7305.
load daily_logreturns
[fn,c]=lillietest1(daily_logreturns);
% [fn,c]=lillietest1(daily_prices);
disp(fn)
plot(c(:,1),c(:,2),c(:,1),c(:,1))
xlabel('x'),ylabel('v'),grid
legend('v vs x','x vs x')
function [fn,c] = lillietest1(x)
n=length(x);
x=sort(x);
s = zeros(n,1);
v = zeros(n,1);
for i=1:n
v(i)=i/n;
if x(i)>=v(i)
s(i)=1;
else
s(i)=0;
end
end
fn=sum(s)/n;
c = [x v];
end
Julian Wzorek
Julian Wzorek 2020년 11월 11일
danke schön! :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Alan Stevens
Alan Stevens 2020년 11월 11일

0 개 추천

Try changing
n=lenght(x);
to
n=length(x);
(Notice the spelling).

댓글 수: 4

Julian Wzorek
Julian Wzorek 2020년 11월 11일
Still I get : Unrecognized function or variable 'lillietest1'.
Alan Stevens
Alan Stevens 2020년 11월 11일
You must be running it from the command window. Save it as a script and run the script.
Julian Wzorek
Julian Wzorek 2020년 11월 11일
편집: Julian Wzorek 2020년 11월 11일
Hmm, it works but the fn is 0 and don't think this is right
Julian Wzorek
Julian Wzorek 2020년 11월 11일
Does this function make even sense?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Agriculture에 대해 자세히 알아보기

태그

질문:

2020년 11월 11일

댓글:

2020년 11월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by