天天看點

用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.

   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.

============================================================================================

     Section A. 第一種方法

     今天(2012-10-17)有一些資料需要處理,這些資料好不容易從檔案中剝離了出來,然後自己寫了一個function,計算并控制 plot 這些資料的 CDF 圖。因為第一種方法用到的例子的資料檔案太大,就沒有貼上來。如果有想親自試驗一下這個過程的同學,請參照下文中第二個方法中的完整用例。

% ----------------------- 自實作 CDF 計算 function:  funcCDF.m

[plain] view plaincopy

用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.
用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.
  1. % [email protected]: CNT_pnts, the number of points to denote the CDF;  
  2. % [email protected]: Range_low, the lower bound of variable;  
  3. % [email protected]: Range_up, the upper bound of variable;  
  4. % [email protected] : arr_Vals, array of the values to be processed.  
  5. function [x, CDF_Vals] = funcCDF(CNT_pnts, Range_low, Range_up, arr_Vals)  
  6. data = sort( arr_Vals' ); % T', horizon arrays of T.  
  7. N = length(data);  
  8. stepLen = (Range_up-Range_low)/CNT_pnts;  
  9. Counter = zeros(1,CNT_pnts);  
  10. for i = 1:1:N  
  11.     for j = 1:1:CNT_pnts  
  12.         if ( data(1,i) <= (Range_low + j*stepLen) )  
  13.             Counter(1,j) = Counter(1,j) + 1;  
  14.         end  
  15.     end  
  16. end  
  17. CDF = Counter(1,:)./N;  
  18. CDF_Vals = CDF(1,:)';  
  19. x = (Range_low+stepLen):stepLen:Range_up;  
  20. % ---- end of func.  

% --------------------- 2 use cases:

[plain] view plaincopy

用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.
用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.
  1. CNT_pnts = 100;  
  2. deadline_N500r1 = 550;  
  3. deadline_N500r3 = 270;  
  4. deadline_N500r5 = 240;  
  5. PntVal_N500Tau100r1 = textread('N500Tau100r1.tr','%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %.2f');  
  6. [x_r1,cdf_r1] = funcCDF(CNT_pnts, 0, deadline_N500r1, PntVal_N500Tau100r1);  
  7. plot(x_r1, cdf_r1, 'ob')  
  8. hold on  
  9. PntVal_N500Tau100r3 = textread('N500Tau100r3.tr','%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %.2f');  
  10. [x_r3,cdf_r3] = funcCDF(CNT_pnts, 0, deadline_N500r3, PntVal_N500Tau100r3);  
  11. plot(x_r3, cdf_r3, 'or')  
  12. hold on  
  13. PntVal_N500Tau100r5 = textread('N500Tau100r5.tr','%*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %*s %.2f');  
  14. [x_r5,cdf_r5] = funcCDF(CNT_pnts, 0, deadline_N500r5, PntVal_N500Tau100r5);  
  15. plot(x_r5, cdf_r5, 'oc')  
  16. grid  

% --------------------- 3 效果圖:

Fig.1 CDF_N200r3--Tau-60-80-100-100Pnts

用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.

Fig.2 CDF_N500Tau100--r-1-3-5-100Pnts

用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.

      當把參數 CNT_pnts = 100; 調為 CNT_pnts = 50; 後,顯示在圖中的點就會減少一半,shows as follow:

Fig.3 CDF_N500Tau100--r-1-3-5-50Pnts

用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.

Davy_H (2012-10-17)

============================================================================================

    Section B. 第二種方法

    今天(2014-10-15) 回過頭來看這篇blog,前邊貼的圖太醜,而且其實第一種方法有不完美的地方,即資料少的時候,曲線有時不會從原點開始畫。後來尋到更好的方法來畫 CDF 圖,為了對得起2000+的通路量,是以,今日我決定花些時間,把更好的例子分享出來。

    廢話不多說:1)效果圖;2)部分資料檔案;3)畫圖的腳本。

1) ------------------ 

用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.

3) ------------------ Codes:

[python] view plaincopy

用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.
用 Matlab 計算并畫出大量資料的CDF   這篇 blog 将展示用 matlab 計算并畫出大量資料的 CDF (累計分布函數)的兩種方法。第一種是我自己于2012年寫的,後來用的過程中發現有缺陷;後來2014年寫另一篇paper時,搜尋到第二種簡易又高效的方法。這裡我給出它們各自的用例,包括畫圖用的資料與腳本,以及效果圖。For your reference.
  1. clear;  
  2. % --------------- A. Read the Data.  
  3. X_ = textread('_Trace_file.tr','%*s%*s %*s%*s %*s%*s %*s%*s %*s%*s %*s%*s %*s%*s %*s%*s %*s%f');  
  4. CNT_resolve_times = textread('_Trace_file.tr','%*s%*s %*s%*s %*s%*s %*s%*s %*s%*s %*s%*s %*s%*s %*s%d %*s%*s' );  
  5. % --------------- B. Count the the Costs.  
  6. % --------- X_items is the "-Threshold"  
  7. X_items =[0.0,0.01,0.05,0.1,0.2,0.3];  
  8. CNT_X = length(X_items);  
  9. % --------- Define the range_x of the x_coordinate in the figure.  
  10. step = 1;  
  11. range_end = 50;  
  12. range_x = 0:step:range_end;  
  13. figure  
  14.     % ---------- Format of figure:  
  15.     TextFontSize=18;  
  16.     LegendFontSize = 16;  
  17.     set(0,'DefaultAxesFontName','Times',...  
  18.         'DefaultLineLineWidth',2,...  
  19.         'DefaultLineMarkerSize',8);  
  20.     set(gca,'FontName','Times New Roman','FontSize',TextFontSize);  
  21.     set(gcf,'Units','inches','Position',[0 0 6.0 4.0]);  
  22.     % ---------- Format of figure:~   
  23. % ------ Plot lines  
  24. for i = 1:1:CNT_X  
  25.     Val_item = X_items(i);    
  26.     idx_it_Lazy = find( X_ == Val_item );  
  27.     % --- 1 CNT_STimes  
  28.     CNT_Re_times_its = [];  
  29.     CNT_Re_times_its = CNT_resolve_times( idx_it_Lazy );  
  30.     % --- 2 Plot CDF of CNT_Resloving_times, i.e., the "CNT_STimes" in the trace file.  
  31.     if (i==1) linePoint_type = '-sk'; step = 5; range_x = 0:step:range_end;  
  32.     elseif (i==2) linePoint_type = '-^r';  
  33.     elseif (i==3) linePoint_type = '-+b'; step = 1; range_x = 0:step:range_end;  
  34.     elseif (i==4) linePoint_type = '-c';  step = 1; range_x = 0:step:range_end;  
  35.     elseif (i==5) linePoint_type = '--g'; step = 1; range_x = 0:step:range_end;  
  36.     elseif (i==6) linePoint_type = '-.m'; step = 1; range_x = 0:step:range_end;  
  37.     end  
  38.     %%% ====== Critical Code of CDF-Ploting :  
  39.     h_rtl = hist( CNT_Re_times_its, range_x );  
  40.     pr_approx_cdf = cumsum(h_rtl) / ( sum(h_rtl) );  
  41.     %%% ====== Critical Code of CDF-Ploting :~  
  42.     handler = plot( range_x, pr_approx_cdf, linePoint_type );  
  43.     if      (i==4) h4 = handler;  
  44.     elseif  (i==5) h5 = handler;  
  45.     elseif  (i==6) h6 = handler;  
  46.     end  
  47.     hold on  
  48. end  
  49. % --------- Set the other formats of the figure :  
  50.     grid off  
  51.     axis([0 range_end 0 1.0])  
  52.     ylabel('CDF')  
  53.     xlabel('Resolving times')  
  54.     % --------- Plot the multi-legends :  
  55.     hg1=legend('{\it \chi_0}=0', '{\it \chi_0}=0.01', '{\it \chi_0}=0.05',  0);  
  56.     set(hg1,'FontSize',LegendFontSize);  
  57.     ah1 = axes('position',get(gca,'position'), 'visible','off');    
  58.     hg2 = legend(ah1, [h4,h5,h6],  '{\it \chi_0}=0.10','{\it \chi_0}=0.20','{\it \chi_0}=0.30',  0);  
  59.     set(hg2,'FontSize',LegendFontSize);  
  60.     % --------- Plot the multi-legends :~  
  61. % --------- Set the other formats of the figure :~  

    關鍵代碼處,我已經做了注釋,此處再強調一下:

    1. 畫 CDF 的2句關鍵代碼,其中的3個 functions 請自己查詢。

    %%% ====== Critical operation of CDF-Ploting :

    h_rtl = hist ( CNT_Re_times_its, range_x );

    pr_approx_cdf = cumsum(h_rtl) / ( sum(h_rtl) );

    %%% ====== Critical operation of CDF-Ploting :~

    2. for 循環中的那一段 if else 語句,是為了設定各條曲線的點線型( linePoint type ) 與 各條線上的取樣點的密度。  

    3. 此外,從這個腳本裡,也可以額外擷取畫多個圖例 (plot multiple legends) 的方法。

y = evrnd(0,3,100,1);

cdfplot(y);

hold on;

x = -20:0.1:10;

f = evcdf(x,0,3);

plot(x,f,'m');

legend('Empirical','Theoretical','Location','NW')

繼續閱讀