如何产生满足二元正态分布的随机数点
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何产生满足二元正态分布的随机数点相关的知识,希望对你有一定的参考价值。
现在我需要一个算法来产生二元正态分布,现在表示为一个函数,这个函数的输入为产生随机数的个数N,正态分布的两个参数μ和σ^2,还有一个随机数的产生区间横坐标的[a,b]和纵坐标的[c,d](其实就是一个长方形区间),输出为N个满足二元正太分布的随机点集<x1,y1><x2,y2>...<xn,yn>。
有没有源代码无所谓,但算法一定要表述清楚,谢谢。注意,不要用MATLAB的东西。
%
% [data1, data2] = twogaussian(n1,mu1,sigma1,n2,mu2,sigma2);
%
% Function to simulate data from 2 Gaussian densities in d dimensions
% and to plot the data in the first 2 dimensions
%
% INPUTS:
% n1, n2: two integers, size of data set 1 and 2 respectively
% mu1, mu2: two vectors of dimension 1 x d, means
% for data set 1 and 2
% cov1, cov2: two matrices of dimension d x d, covariance
% matrices for data set 1 and 2 respectively
%
% OUTPUTS:
% data1: n1 x d matrix of data for data set 1
% data2: n2 x d matrix of data for data set 2% check that the dimensionality of the mu's and sigma's are consistent
d1 = length(mu1);
d2 = length(mu2);
if (d1~=d2)
error('means are of different lengths');
end;
d = length(mu1); % d is the dimensionality of the data
[d1 d2] = size(cov1);
if (d1~=d2)
error('cov1 is a non-square covariance matrix');
end;
if (d1~=d)
error('cov1 is of different dimensionality to mu1');
end;
[d1 d2] = size(cov2);
if (d1~=d2)
error('cov2 is a non-square covariance matrix');
end;
if (d1~=d)
error('cov2 is of different dimensionality to mu2');
end;% Call the function mvnrnd.m to generate the two data sets
data1 = mvnrnd(mu1,cov1,n1);
data2 = mvnrnd(mu2,cov2,n2);% Now plot the two data sets as a two-dimensional scatter plot
% if d = 2: plot dimension1 on the xaxis and dimension 2 on the
% yaxis. Plot the points from data1 as green circles 'o', and the
% points from data2 as red crosses 'x'.
if ....
figure % open a figure window
plot(data1(:,1),data1(:,2),'b.');.... % now plot data1
axis([-6 6 -6 6]); % fix the lengths of the axes
hold % hold the figure to overlay a 2nd plot
plot(data2(:,1),data2(:,2),'rx');% now plot data 2
xlabel('Dimension 1');
ylabel('Dimension 2');
title('Simulation of two-class Gaussian data in two dimensions');
endfunction r = mvnrnd(mu,sigma,cases);
%MVNRND Random matrices from the multivariate normal distribution.
% R = MVNRND(MU,SIGMA,CASES) returns a matrix of random numbers
% chosen from the multivariate normal distribution with mean vector,
% MU, and covariance matrix, SIGMA. CASES is the number of rows in R.
%
% SIGMA is a square positive definite matrix with size equal to
% the length of MU% Adapted from Mathworks function[m1 n1] = size(mu);
c = max([m1 n1]);
if m1 .* n1 ~= c
error('Mu must be a vector.');
end[m n] = size(sigma);
if m ~= n
error('Sigma must be square');
endif m ~= c
error('The length of mu must equal the number of rows in sigma.');
end[T p] = chol(sigma);
if p ~= 0
error('Sigma must be a positive definite matrix.');
endif m1 == c
mu = mu';
endmu = mu(ones(cases,1),:);r = randn(cases,c) * T + mu; 参考技术A http://www.cppblog.com/tanky-woo/archive/2010/12/10/136023.html 看看 貌似这里面就是生成了 正态分布的随机数
matlab 如何产生power law分布的随机数
例子: 我要产生一组随机数数量为1000个,随机数分布符合y=x^-a。 a是常数。求解答。
参考技术A 看帮助中的random函数,有一个参数是指数分布:'exp'追问我要做的幂函数分布,不是指数分布。
以上是关于如何产生满足二元正态分布的随机数点的主要内容,如果未能解决你的问题,请参考以下文章