4.0

4.0 | 1 rating Rate this file 6 Downloads (last 30 days) File Size: 1.06 KB File ID: #23525

Conditional Nonparametric Kernel Density

by Taesam Lee

 

02 Apr 2009

Condtional Kernel Density Estimate with normal kernel and LSCV bandwidth selection

| Watch this File

File Information
Description

Ref: Sharma A 2000 J. of Hydrology (Part3-nonparametric probablistic forecast model)

MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (2)
03 Apr 2009 Taesam Lee

function [fcond,Pcond,lambda,yi]=NPcond(y,X,Xc,yi)
%
% Code written by Taesam Lee and Dominique Fasbender 04/03/2009 : INRS-ETE Quebec
% Nonparametric Condtional Distribution
% X : data of condtion variable (numvar*length), y : current variable(length)
% Xc: condtion values
% yi: estimated set for fcond
% example
% t=mvnrnd([0,0],[.9 .4; .4 .3],100);
% X=t(:,1)';y=t(:,2)';yi=-2:0.01:2;Xc=[1];
% [fcond,Pcond,yi]=NPcond(y,X,Xc,yi);
% plot(yi,fcond);plot(yi,Pcond);

Xall=[y;X];
sz=size(Xall);d=sz(1);n=sz(2);

% Estimate the bandwidth ratio
lambda_ref = (4/(d+2))^(1/(d+4))*n^(-1/(d+4));
%min_lambda = 0.25*lambda_ref;max_lambda = 1.1*lambda_ref;
%lambda = fminbnd(@(lambda)LSCV(lambda,Xall',n,d),min_lambda,max_lambda);
lambda=lambda_ref;

% Covariance
Sall=cov(Xall');
Syy=Sall(1,1);Sxy=Sall(2:d,1);Sxx=Sall(2:d,2:d);
Sd=Syy-Sxy'*inv(Sxx)*Sxy;

% Conditional Distribution
difX=repmat(Xc,1,n)-X;% need to be changed
s1=inv(Sxx)
%for in=1:n
% dmat1(in)=(difX(:,in))'*s1*(difX(:,in));
%end
%es=exp(-1/2*lambda_ref^2*dmat1);

 es2 = 0;
 for i=size(s1,1)
     for j=size(s1,2)
         es2 = es2 + exp(-1/2*lambda_ref^2*difX(i,:).*difX(i,:)*s1(i,j));
     end
 end
 
wi=es2./sum(es2);
bi=Sxy'*inv(Sxx)*difX;

for it=1:length(yi)
    fcond(it)=sum(1/sqrt(2*pi*lambda^2*Sd)*wi.*exp(-(yi(it)-bi).^2./(2*lambda^2*Sd)));
    Pcond(it)=sum(wi.*normcdf(yi(it),bi,sqrt(lambda^2*Sd)));
end

03 Apr 2009 Matt Fig

Please don't rate your own files.
Everyone assumes the author likes the file.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
statistics Taesam Lee 02 Apr 2009 10:14:10
mathematics Taesam Lee 02 Apr 2009 10:14:10

Contact us at files@mathworks.com