欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

離散型隨機變量的聯合分布率怎么算

阮建安2年前39瀏覽0評論

離散型隨機變量的聯合分布率怎么算?

給定至少兩個隨機變量X,Y,…, 它們的聯合概率分布(Joint probability distribution)指的是每一個隨機變量的值落入特定范圍或者離散點集合內的概率. 對于只有兩個隨機變量的情況, 稱為二元分布(bivariate distribution).

聯合概率分布可以使用聯合累計分布函數(joint cumulative distribution function), 連續隨機變量的聯合概率密度函數(joint probability density function)或者離散變量的聯合概率質量函數(joint probability mass function)來描述. 由此又衍生出兩個概念: 邊緣分布(marginal distribution)和條件概率分布(conditional probability distribution).

二. 離散變量的聯合概率質量函數公式

公式:

?

?是給定X=xX=x的Y=yY=y的條件概率.

而且有:

?

如果XX和YY相互獨立:

?

如果XX和YY條件不獨立(conditionally dependent):

P(X=x and Y=y)=P(X=x)?P(Y=y|X=x)P(X=x and Y=y)=P(X=x)·P(Y=y|X=x)

也可以使用聯合累計分布函數的差分來計算:

聯合累計分布函數定義是:

?

所以F(x,y)F(x,y)的導數(差分)就是P(X=x and Y=y)P(X=x and Y=y)

三. 使用Matlab計算離散2D聯合分布

參考: Calculating a 2D joint probability distribution

離散2D聯合分布可用于計算兩張圖片的互信息MI.

0. 定義兩個離散的隨機變量.

有N個點分布在邊長為1的正方形區域內. 把正方形分為K1*K2的小矩形. 統計每個小矩形內的點的個數.

% Data

N = 1e5; % number of points

xy = rand(N, 2); % coordinates of points

xy(randi(2*N, 100, 1)) = 0; % add some points on one side

xy(randi(2*N, 100, 1)) = 1; % add some points on the other side

xy(randi(N, 100, 1), :) = 0; % add some points on one corner

xy(randi(N, 100, 1), :) = 1; % add some points on one corner

inds= unique(randi(N, 100, 1));

xy(inds, :) = repmat([0 1], numel(inds), 1); % add some points on one corner

inds= unique(randi(N, 100, 1));

xy(inds, :) = repmat([1 0], numel(inds), 1); % add some points on one corner

% Intervals for rectangles

K1 = ceil(sqrt(N/5)); % number of intervals along x

K2 = K1; % number of intervals along y

int_x = [0:(1 / K1):1]; % intervals along x

int_y = [0:(1 / K2):1]; % intervals along y

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

1. 從定義出發, 使用for循環:

tic

count_cells = zeros(K1, K2);

for k1 = 1:K1

inds1 = (xy(:, 1) >= int_x(k1)) & (xy(:, 1) < int_x(k1 + 1));

for k2 = 1:K2

inds2 = (xy(:, 2) >= int_y(k2)) & (xy(:, 2) < int_y(k2 + 1));

count_cells(k1, k2) = sum(inds1 .* inds2);% 布爾相乘得到交集點的個數

end

end

toc

% Elapsed time is 39.357691 seconds.

1

2

3

4

5

6

7

8

9

10

11

1

2

3

4

5

6

7

8

9

10

11

可見使用兩重循環的計算時間非常長.

2. 使用hist3函數

N=hist3(X,'Edges',edges)是matlab中專門計算二元分布的函數.

edges是包含兩個遞增array的cell. 第一維分組edge1是edges{1}, 第二維分組edge2是edges{2}.

也就是:

edges1(i)<=X(k,1)<edges1(i+1)edges1(i)<=X(k,1)<edges1(i+1)

edges2(j)<=X(k,2)<edges2(j+1)edges2(j)<=X(k,2)<edges2(j+1)

正好落在edges1(i+1)edges1(i+1)或者edges2(j+1)edges2(j+1)上的點的個數放在N的最后一行或者最后一列.

hist3不統計edges范圍外的部分.

N是一個二維矩陣, 統計的落到每個單元格內的點的個數.

tic

count_cells_hist = hist3(xy, 'Edges', {int_x int_y});

% 注意hist3得到的矩陣是K1+1*K2+1的, 所以把最后一行和一列去掉.

% 最后一行或一列表示的是 X(k,1)= edges{1}(end)或者X(k,2) = edges{2}(end)的點數

count_cells_hist(end, :) = []; count_cells_hist(:, end) = [];

toc

all(count_cells(:) == count_cells_hist(:))

% Elapsed time is 0.017995 seconds.

1

2

3

4

5

6

7

8

1

2

3

4

5

6

7

8

顯然比用兩重for循環快多了.

3. 使用矩陣二元操作bsxfun

C = bsxfun(fun,A,B)對A和B做逐個元素的二元操作, 操作由函數 fun指定.

返回的C中, 1表示滿足條件, 0 表示不滿足條件. 可用的fun有:

fun operation

@plus Plus

@minus Minus

@timesArray multiply

@rdivideRight array divide

@ldivideLeft array divide

@power Array power

@max Binary maximum

@min Binary minimum

@rem Remainder after division

@mod Modulus after division

@atan2 Four-quadrant inverse tangent; result in radians

@atan2d Four-quadrant inverse tangent; result in degrees

@hypot Square root of sum of squares

@eq Equal

@neNot equal

@ltLess than

@le Less than or equal to

@gt Greater than

@ge Greater than or equal to

@andElement-wise logical AND

@orElement-wise logical OR

@xorLogical exclusive OR

使用bsxfun的matlab代碼:

%% bsxfun

tic

xcomps = single(bsxfun(@ge,xy(:,1),int_x));% 10000*143矩陣

ycomps = single(bsxfun(@ge,xy(:,2),int_y));% 10000*143矩陣

% 相當于求CDF

count_again = xcomps.' * ycomps; %' 143x143 = 143x1e5 * 1e5x143

% 差分后是142*142

count_again_fix = diff(diff(count_again')');

toc

% Elapsed time is 0.178316 seconds.

all(count_cells_hist(:) == count_again_fix(:))

1

2

3

4

5

6

7

8

9

10

11

1

2

3

4

5

6

7

8

9

10

11

bsxfun稍遜于hist3, 可以針對沒有statistics toolbox的情況下使用.

4. 使用accumarray

A= accumarray(subs,val)使用subs的元素值作為索引. subs和val是一一對應的. 將subs中相同值對應的val值累加. 也就是說, subs中元素的位置決定了val哪些元素相加, subs中元素的值決定了累加值在輸出中的位置. 看matlab help中示例:

Example 1

Create a 5-by-1 vector and sum values for repeated 1-D subscripts:

val = 101:105;

subs = [1; 2; 4; 2; 4];

A = accumarray(subs, val)

A =

101 % A(1) = val(1) = 101

206 % A(2) = val(2)+val(4) = 102+104 = 206

0 % A(3) = 0

208 % A(4) = val(3)+val(5) = 103+105 = 208

subs中元素值必須是正整數值. 所以在表示分組時, 可以把[0,1]區間變為[1,K1]區間. matlab代碼:

%%%%% 第五種方法Using accumarray

% Another approach is to use accumarray to make the joint histogram after we bin the data.

% Starting with int_x, int_y, K1, xy, etc.:

tic

% take (0,1) data onto [1 K1], following A.Dondas approach for easy comparison

ii = floor(xy(:,1)*(K1-eps))+1;

ii(ii<1) = 1; ii(ii>K1) = K1;

jj = floor(xy(:,2)*(K1-eps))+1;

jj(jj<1) = 1; jj(jj>K1) = K1;

% create the histogram and normalize

H = accumarray([ii jj],ones(1,size(ii,1)));

PDF = H / size(xy,1); % for probabilities summing to 1

toc

% Elapsed time is 0.006356 seconds.

all(count_cells_hist(:) == count_again_fix(:))

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

ms級別! 真是快!

5. 使用mex編譯

mex混合編程參考: 在Matlab中使用mex函數進行C/C++混合編程

#include "mex.h"

// http://stackoverflow.com/questions/19745917/calculating-a-2d-joint-probability-distribution

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])

{

unsigned long int hh, ctrl; /* counters */

unsigned long int N, m, n; /* size of matrices */

unsigned long int *xy; /* data */

unsigned long int *count_cells; /* joint frequencies */

/* matrices needed */

mxArray *count_cellsArray;

/* Now we need to get the data */

if (nrhs == 3) {

xy = (unsigned long int*) mxGetData(prhs[0]);

N = (unsigned long int) mxGetM(prhs[0]);//取矩陣的行數

m = (unsigned long int) mxGetScalar(prhs[1]);

n = (unsigned long int) mxGetScalar(prhs[2]);

}

/* Then build the matrices for the output */

count_cellsArray = mxCreateNumericMatrix(m + 1, n + 1, mxUINT32_CLASS, mxREAL);

count_cells = mxGetData(count_cellsArray);

plhs[0] = count_cellsArray;

hh = 0; /* counter for elements of xy */

/* for all points from 1 to N */

for(hh=0; hh<N; hh++) {

ctrl = (m + 1) * xy[N + hh] + xy[hh];

count_cells[ctrl] = count_cells[ctrl] + 1;

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

將代碼保存為: joint_dist_points_2D.c. 在matlab cmd中運行:

mex joint_dist_points_2D.c

1

1

生成joint_dist_points_2D.mexw32文件.

matlab調用代碼:

% Use mex function

tic

xy2 = uint32(floor(xy ./ repmat([1 / K1, 1 / K2], N, 1)));

count_cells = joint_dist_points_2D(xy2, uint32(K1), uint32(K2));

toc

% Elapsed time is 0.011696 seconds.

1

2

3

4

5

6

1

2

3

4

5

6

也是非常快的.

php數組生成pdf,離散型隨機變量的聯合分布率怎么算