calculating Kernel density for each column

Hi
Please how do I need a short code that will calculate the KDE of each column in the R.length data below
the KDE is given as = I/n*h sum ( K * (( v - i )/h) which is computed for each column
where h = 1.06 * variance * (n^(-0.2)) for each colum
n is the number of each column
i = first, second, third, fourth, fifth, sixth number of each column
v =pv is given as 3, 4, 5, 6 for each column
Thanks in advance
jonathan
R = [ 0.6164 3.4161 0.9950 3.4117;
3.1654 0.4123 4.2391 1.0198;
0.5745 3.0364 1.3191 3.1129;
2.9883 0.7348 3.8730 0.4123;
0.9381 3.3749 2.0421 3.5014;
2.1817 1.0630 3.0643 0.9487];

5 commentaires

Rik
Rik le 24 Avr 2019
What have you tried so far? Have a read here and here. It will greatly improve your chances of getting an answer.
David Wilson
David Wilson le 24 Avr 2019
Would ksdensity work? (From stats toolbox)
'Answer' by Jonathan Etumusei moved to comment and formatted:
This is what I have done so far and the answers are below
thanks
n = 6;
K = 3;
h1 = 1.06 * var(Z(:,1)) * (n ^ 0.2);
h2 = 1.06 * var(Z(:,2)) * (n ^ 0.2);
h3 = 1.06 * var(Z(:,3)) * (n ^ 0.2);
h4 = 1.06 * var(Z(:,4)) * (n ^ 0.2);
z1 = 1/ (n * h1);
z2 = 1/ (n * h2);
z3 = 1/ (n * h3);
z4 = 1/ (n * h4);
Ec11 = (K * (v1 - Z(1,1))/h1) ;
Ec12 = (K * (v1 - Z(2,1))/h1) ;
Ec13 = (K * (v1 - Z(3,1))/h1) ;
Ec14 = (K * (v1 - Z(4,1))/h1) ;
Ec15 = (K * (v1 - Z(5,1))/h1) ;
Ec16 = (K * (v1 - Z(6,1))/h1) ;
e1 = [Ec11; Ec12; Ec13; Ec14; Ec15; Ec16];
e1 = sum (e1);
Ec21 = K * (v2 - Z(1,2)/h2);
Ec22 = (K * (v2 - Z(2,2))/h2) ;
Ec23 = (K * (v2 - Z(3,2))/h2) ;
Ec24 = (K * (v2 - Z(4,2))/h2) ;
Ec25 = (K * (v2 - Z(4,2))/h2) ;
Ec26 = (K * (v2 - Z(4,2))/h2) ;
e2 = [ Ec21; Ec22; Ec23; Ec24; Ec25; Ec26];
e2 = sum (e2);
Ec31 = K * (v3 - Z(1,3)/h3);
Ec32 = (K * (v3 - Z(2,3))/h3) ;
Ec33 = (K * (v3 - Z(3,3))/h3) ;
Ec34 = (K * (v3 - Z(4,3))/h3) ;
Ec35 = (K * (v3 - Z(5,3))/h3) ;
Ec36 = (K * (v3 - Z(6,3))/h3) ;
e3 = [Ec31; Ec32; Ec33; Ec34; Ec35; Ec36];
e3 = sum (e3);
Ec41 = K * (v4 - Z(1,4)/h4);
Ec42 = (K * (v4 - Z(2,4))/h4) ;
Ec43 = (K * (v4 - Z(3,4))/h4) ;
Ec44 = (K * (v4 - Z(4,4))/h4) ;
Ec45 = (K * (v4 - Z(5,4))/h4) ;
Ec46 = (K * (v4 - Z(6,4))/h4) ;
e4 = [Ec41; Ec42; Ec43; Ec44; Ec45; Ec46];
e4 = sum(e4);
k = e1;
l = e2;
m = e3;
b = e4;
% the kernal density estimation
KDE1 = k * z1;
KDE2 = l * z2;
KDE3 = m * z3;
KDE4 = b * z4;
answer
-0.4881
-0.1668
-0.7734
-0.3972
Rik
Rik le 24 Avr 2019
Instead of using numbered variables, why don't you process the columns in a loop?
Tino
Tino le 24 Avr 2019
Yes but how do I do that ?

Connectez-vous pour commenter.

 Réponse acceptée

Rik
Rik le 24 Avr 2019
I am assuming the v values are the same as the column index, and that you made a mistake with the code for the second column.
Z = [ 0.6164 3.4161 0.9950 3.4117;
3.1654 0.4123 4.2391 1.0198;
0.5745 3.0364 1.3191 3.1129;
2.9883 0.7348 3.8730 0.4123;
0.9381 3.3749 2.0421 3.5014;
2.1817 1.0630 3.0643 0.9487];
n = 6;
K = 3;
z=zeros(1,size(Z,2));e=zeros(size(z));
for col=1:size(Z,2)
v=col;%is this what you mean?
h = 1.06 * var(Z(:,col)) * (n ^ 0.2);
z(col) = 1/ (n * h);
E = K * (v - Z(:,col))/h;
if col~=1
%did you mean for this to be different?
E(1)= K * (v - Z(1,col)/h);
end
e(col) = sum(E) ;
end
% the kernal density estimation
KDE = e .* z;

2 commentaires

Tino
Tino le 24 Avr 2019
Hi Rik
The v values are numbers obtained from a different computation below
strangeness1 = 0.25541
strangeness2 = 4.4465
strangeness3 = 0.38976
strangeness4 = 4.2112
using Si = [strangeness1,strangeness2, strangeness3, strangeness4];
% find the v-values
fnP=@(a,i)(sum(a(i)>a(1:i))+0.5*sum(a(i)==a(1:i)))/i;
Thanks in advance
Regards
Jonathan
Rik
Rik le 24 Avr 2019
Well, you know the inputs, you have working code, you should be able to integrate the calculation in my code. What issues are you having with that integration?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Produits

Version

R2019a

Tags

Question posée :

le 24 Avr 2019

Commenté :

Rik
le 24 Avr 2019

Community Treasure Hunt

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

Start Hunting!

Translated by