Marginal distributions of a bivariate function

版本 1.4.0.0 (3.3 KB) 作者: Shoaibur Rahman
This function computes the marginal distributions of each variable in a bivariate function.
359.0 次下载
更新时间 2014/12/18

查看许可证

function [fx, fy, MeanVar] = marginaldist(f,x,y,distributionType)

f is a bivariate function, which can be a normalized or unnormalized distribution function. x and y are the two independent variables of f. The variable values can be taken as either row or column vectors. fx and fy are the marginal distributions of x and y, respectively.
distributionType defines whether the marginal distributions have to be computed on continuous or discrete domain. Default is continuous. The strings that can be assigned to distributionType as an input may include:
(for continuous) 'Continuous','continuous','Con', or 'con'
and (for discrete) 'Discrete','discrete','Dis', or 'dis'

MeanVar is optional. This is a vector output, whose elements are mean of fx, variance of fx, mean of fy, and variance of fy, respectively.

Define function f in a separate m-file. In the example below, we take a two-dimensional Gaussian function as our test function, whose m-file is saved as testFunction.m

function f = testFunction(x,y)
sx = 2; sy = 0.5; mx = 2; my = -1; % s stands for variance, and m for mean
f = 1/(2*pi*sx*sy)*exp(-(x-mx).^2/(2*sx^2)-(y-my).^2/(2*sy^2));

Example 1
x = -10:0.1:10; y = -10:0.1:10;
[fx, fy, MV] = marginaldist(@testFunction,x,y,'continuous');
subplot(211), plot(x,fx), xlabel('x'), ylabel('f_x(x)')
title('Marginal distribution of x','FontSize',12,'Color','r')
subplot(212), plot(y,fy), xlabel('y'), ylabel('f_y(y)')
title('Marginal distribution of y','FontSize',12,'Color','r')

Example 2
x = -10:10; y = -10:10;
[fx, fy] = marginaldist(@testFunction,x,y,'discrete');
subplot(211), plot(x,fx), xlabel('x'), ylabel('f_x(x)')
title('Marginal distribution of x','FontSize',12,'Color','r')
subplot(212), plot(y,fy), xlabel('y'), ylabel('f_y(y)')
title('Marginal distribution of y','FontSize',12,'Color','r')

引用格式

Shoaibur Rahman (2024). Marginal distributions of a bivariate function (https://www.mathworks.com/matlabcentral/fileexchange/48770-marginal-distributions-of-a-bivariate-function), MATLAB Central File Exchange. 检索时间: .

MATLAB 版本兼容性
创建方式 R2014b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Random Number Generation 的更多信息

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.4.0.0

This revised version includes the flexibility of getting means and variances of two marginal distributions computed.

1.3.0.0

includes test function m-file

1.2.0.0

Image File

1.1.0.0

Title modification on 16/12/2014

1.0.0.0