Code covered by the BSD License  

Highlights from
Log Density Plot

image thumbnail

Log Density Plot

by

 

This program accepts a vector of stock market prices and calculates the Log density plot.

log_density_v2.m
% Log Density Plot Program
% Written by: Vasileios Pappas
% This Version: 25/10/2011

% This program accepts a vector of stock market prices and calculates the 
% Log density plot. The produced graph also plots a normal curve with mean
% and standard deviation from the provided data. The Log-pdf clearly
% displays the fat-tail property of stock market returns. An application of
% the log-pdf can be found in Andersen, Bollerslev and Dobrev (2006) "No
% Arbitrage Semi-Martingale Restrictions for Continuous-Time Volatility
% Models subject to Leverage Effects, Jumps and i.i.d. Noise: Theory and
% Testable Distributional Implications".

% To use, you can either read data from an excel file (Section A) or read
% data from the Matlab workspace (Section B). Make sure file names and 
% directory is correct for your installation. 


% Section A: Load test data data (mark section with comment to use with
% your own data. Download to Matlab folder or change directory accordingly.

cd('C:\Program Files\MATLAB')
Ftse=xlsread('data.xlsx','1','C2:C2606');
ret_ftse=price2ret(Ftse);

% Section B: Enter series to test
sers = ret_ftse;

% Clearing previously generated variables
clearvars YH XH XG XH2 YH2 Plog Pnorm brks cnts d incrm i j mu sigma h1 h2

% n=frequency counts (counts-yh); xout=bin locations (breaks-xh)
[cnts, brks]=hist(sers,150);    % Return Series, number of bins (number of 
% bins should be much lower than observations. Default setting is 150 bins
% for about 2600 observations
brks = brks(1:length(brks)-1)+diff(brks)/2;
YH = cnts';
XH = brks';

% This part is used to delete those observations from xh that their
% corresponding yh is zero. It is substituting the command xh<-xh[yh>0]
% where the brackets indicate conditioning i.e. if or when

j=1;
for i=1:length(YH)
    if YH(i)>0
        if i==1
            XH2(j)=XH(i);
        else
            XH2(j)=XH(i-1);
        end
        j=j+1;
    end
end
XH2=XH2';

j=1;
for i=1:length(YH)
if YH(i)>0
    YH2(j)=log(YH(i));
    j=j+1;
end
end
YH2=YH2';

% Section C: Comparison with normal plot
start=XH2(1);                %first number
stop=XH2(length(XH2));       %last number
d=abs(stop-start);
incrm=d/length(XH2);
for i=0:length(XH2)-1       % this is to ensure same length of vectors
    XG(i+1)= start+i*incrm;
end
XG=XG';

% Means and SDs of return vectors
mu = mean(sers);               
sigma = std(sers);
Plog = log(normpdf([XG],mu,sigma));
Pnorm = normpdf([XG],mu,sigma);

% Plotting command
plot(XH2, YH2(1:length(XH2)));
xlabel('Log Returns');
ylabel('Log Empirical PDF');
title('Log PDF Dell Daily Returns');
legend1=legend('Log PDF');
set(legend1,'Location','NorthWest');
set(legend1,'Box','off');
h1=gca;
% Section D: Plotting normal plot - Comment section to display only log
% density
h2 = axes('Position', get(h1,'Position'));
plot(XG,Pnorm,'LineWidth',1,'Color','red','LineStyle','-.');
set(h2,'YAxisLocation','right','Color','none','XTickLabel',[]);
set(h2,'XLim',get(h1,'XLim'),'Layer','top');
legend1=legend('Normal PDF');
set(legend1,'Location','NorthEast');
set(legend1,'Box','off');
ylabel('Normal Empirical PDF');





Contact us