how to make a Joint PDF?

61 views (last 30 days)
Mukhtar Daud
Mukhtar Daud on 5 Nov 2020
Commented: Peter Perkins on 19 Nov 2020
%reading the Excel file and storing in variable k.
[~,~,k] = xlsread('Copy_of_SPX_data.xlsx');
%reading the .txt file and storing it in variable A.
A = importdata('Sunspot_data.txt');
DateString = k(2:5410,1);
formatIn = 'mm/dd/yyyy';
x = log(datenum(DateString,formatIn));
y = log(cell2mat(k(2:5410,2)));
y1=A(66109:66109+length(y)-1,5);
figure(1)
[f,x1] = hist(y,50);
bar(x1,f/trapz(x1,f));
title('StockMarket pdf')
ylabel('StockMarket')
xlabel('Daily stock value')
grid on
figure(2)
[f1,x2]=hist(y1,50);
bar(x2,f1/trapz(x2,f1))
title('Sunspot pdf')
ylabel('Sunspot porbaility')
xlabel('Sunspot daily value')
grid on
X=[y,y1];
[n,c]=hist3(X);
figure(3)
contour(c{1},c{2},n)
title('Sunspot and StockMarket joint pdf')
ylabel('Stockmarket')
xlabel('Sunspot daily probaility')
***Here is my code, I'm trying to add the first pdf to the second one and create a joint pdf, the pdf is wokring but looks very bad and wrong, im not an expered coder so i don't know what to do?
  1 Comment
Peter Perkins
Peter Perkins on 19 Nov 2020
Orthogonal to the actual question, importdata is probably not the thing to use. I suggest looking at readtable or readtimetable. I suspect that importdata is the reason why you had to use cell2mat. You shouldn't need to. In addition, this
x = log(datenum(DateString,formatIn));
seems odd. Using readtable would avoid datenums, which you want to avoid, but also you are taking the logs of numbers with a c ompletely arbitrary origin. Not sure why you'd need to use a log scale for a time axis.

Sign in to comment.

Answers (1)

Reshma Nerella
Reshma Nerella on 10 Nov 2020
Edited: Reshma Nerella on 10 Nov 2020
Hi,
I did not get the term joint pdf.
In case joint pdf means getting all pdfs on the same figure, you can try this out.
[f,x1] = hist(y,50);
bar(x1,f/trapz(x1,f));
title('StockMarket pdf')
ylabel('StockMarket')
xlabel('Daily stock value')
grid on
hold on %to add new plot to same axes i.e second and third pdf
[f1,x2]=hist(y1,50);
bar(x2,f1/trapz(x2,f1))
title('Sunspot pdf')
ylabel('Sunspot porbaility')
xlabel('Sunspot daily value')
grid on
[n,c]=hist3(X);
contour(c{1},c{2},n)
title('Sunspot and StockMarket joint pdf')
ylabel('Stockmarket')
xlabel('Sunspot daily probaility')
hold off %to prevent other plots from using the same axes
You can also add legend to the plots to differentiate each other

Categories

Find more on Polar Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!