利用Matlab中的dfittool工具實現(xiàn)概率密度函數(shù)pdf和累積概率密度函數(shù)CDF繪圖

difittool工具

  • matlab命令行輸入dfittool調(diào)出工具窗口
  • 加載數(shù)據(jù)后直接選擇PDF或CDF函數(shù)就可以。具體使用可參考幫助文檔
    效果圖如下:
概率密度函數(shù)
累積概率密度函數(shù)
  • 由界面生成代碼
    在界面上調(diào)整坐標(biāo)軸不是很方便,我至今未找到在哪調(diào)整。轉(zhuǎn)換成figure再進(jìn)行調(diào)整,一次還行,如果有很多的數(shù)據(jù),每次都要去調(diào)就會顯得很麻煩。解決辦法:點擊界面文件中的自動生成代碼。通過代碼調(diào)整,不僅方便,還可以查看它到底是怎么實現(xiàn)的,另外還能學(xué)到一些設(shè)置坐標(biāo)軸的辦法等,用處多多。
生成代碼

下面以一個簡單的實例來一探究竟。


結(jié)果示例1

上圖對應(yīng)生成的代碼如下:

function createFit(arg_1,arg_2)
%CREATEFIT    Create plot of datasets and fits
%   CREATEFIT(ARG_1,ARG_2)
%   Creates a plot, similar to the plot in the main distribution fitting
%   window, using the data that you provide as input.  You can
%   apply this function to the same data you used with dfittool
%   or with different data.  You may want to edit the function to
%   customize the code and this help message.
%
%   Number of datasets:  2
%   Number of fits:  0
%
%   See also FITDIST.

% This function was automatically generated on 31-Jul-2019 10:21:33

% Data from dataset "Mode1":
%    Y = arg_1 (originally DMLT20M1(:,5))

% Data from dataset "Mode2":
%    Y = arg_2 (originally DMLT20M2(:,5))

% Force all inputs to be column vectors
arg_1 = arg_1(:);
arg_2 = arg_2(:);

% Prepare figure
clf;
hold on;
LegHandles = []; LegText = {};


% --- Plot data originally in dataset "Mode1"
[CdfF,CdfX] = ecdf(arg_1,'Function','cdf');  % compute empirical cdf
BinInfo.rule = 1;
[~,BinEdge] = internal.stats.histbins(arg_1,[],[],BinInfo,CdfF,CdfX);
[BinHeight,BinCenter] = ecdfhist(CdfF,CdfX,'edges',BinEdge);
hLine = bar(BinCenter,BinHeight,'hist');
set(hLine,'FaceColor','none','EdgeColor',[0.333333 0 0.666667],...
    'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
LegHandles(end+1) = hLine;
LegText{end+1} = 'Mode1';

% --- Plot data originally in dataset "Mode2"
[CdfF,CdfX] = ecdf(arg_2,'Function','cdf');  % compute empirical cdf
BinInfo.rule = 1;
[~,BinEdge] = internal.stats.histbins(arg_2,[],[],BinInfo,CdfF,CdfX);
[BinHeight,BinCenter] = ecdfhist(CdfF,CdfX,'edges',BinEdge);
hLine = bar(BinCenter,BinHeight,'hist');
set(hLine,'FaceColor','none','EdgeColor',[0.333333 0.666667 0],...
    'LineStyle','-', 'LineWidth',1);
xlabel('Data');
ylabel('Density')
LegHandles(end+1) = hLine;
LegText{end+1} = 'Mode2';

% Create grid where function will be computed
XLim = get(gca,'XLim');
XLim = XLim + [-1 1] * 0.01 * diff(XLim);
XGrid = linspace(XLim(1),XLim(2),100);


% Adjust figure
box on;
hold off;

% Create legend from accumulated handles and labels
hLegend = legend(LegHandles,LegText,'Orientation', 'vertical', 'FontSize', 9, 'Location', 'northeast');
set(hLegend,'Interpreter','none');

其中的坐標(biāo)標(biāo)簽、圖例、標(biāo)題、線條顏色設(shè)置都顯而易見,按自己的需求改正后把它保存下來,下次調(diào)用直接調(diào)用函數(shù)即可。另外附上顏色對照表(0-1取值)
https://wenku.baidu.com/view/111e6c47773231126edb6f1aff00bed5b8f3734e.html

ksdensity函數(shù)繪制線型的pdf

[f,xi] = ksdensity(x)

計算樣本向量x的概率密度估計,返回在xi點的概率密度f,此時我們使用plot(xi,f)就可以繪制出概率密度曲線。該函數(shù),首先統(tǒng)計樣本x在各個區(qū)間的概率(與hist有些相似),再自動選擇xi,計算對應(yīng)的xi點的概率密度
繪制示例

clear;
clc;
%讀數(shù)據(jù)
filename='E:\data\Mode2.xlsx';
data= xlsread(filename);

%選取指定表格中指定的列數(shù)據(jù)
%index是第幾列,varname是對應(yīng)的變量名字
varname={'Tu','Td','Te','Dst','MDst'};
index=[5,8,11,12,13];
for i=1:length(index)
    [f(i,:),x(i,:)] = ksdensity(data(:,index(i)));
    %繪制概率密度函數(shù)在不同的圖形界面上
    figure (i);
    plot(x(i,:),f(i,:));
    title(['figure',num2str(i),': pdf of ',char(varname(i))]); 
    axis([0 max(x(i,:)) 0 max(f(i,:))]);
end

運行效果:一下子彈出五個圖,分別對應(yīng)五列數(shù)據(jù)的pdf。這里只展示其中一張圖

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容