random number generation with changing probability
Afficher commentaires plus anciens
hi all,
I want to generate either 0 or 1 randomly 50 times but with changing probability for 1 and 0. For example if i do 50 trails then for the first 10 trails 1 should definitely come means probability of occurrence of 1 is 1 and 0 is 0, for next 10 trails probability of occurrence of 1 becomes 0.75 and 0 becomes 0.25 means for this 10 trails 1 should come more often that 0, for next 30 trails probability of occurrence of 1 becomes 0.5 and 0 becomes 0.5. Can anyone suggest me how to do this ??
Réponse acceptée
Plus de réponses (2)
Sean de Wolski
le 26 Août 2011
This should do it:
function X = probVals(sz,vals,probs)
%Creates a matrix of values occurring at specified probabilities
%SCd 08/26/2011
%
% sz: size vector
% vals: values
% probs: probabilities (must sum to 1)
%
%Error checking
assert(nargin==3,'Three input arguments expected');
assert(isequal(size(vals),size(probs)),'vals and probs are expected to be the same size');
assert(all(probs>=0&probs<=1),'probs are expected to lie on the interval [0 1]');
assert(abs(1-sum(probs))<10^-6,'probs are expected to sum to 1');
%Engine:
probs = cumsum(probs);
X = rand(sz);
[jnk,bin] = histc(X,[-inf,(probs(:)'),inf]);
X = vals(bin);
Lucas García
le 26 Août 2011
If I understand correctly, you are looking for a random number generation of the binomial distribution, your outcome can either be 0 or 1. You can use the Statistics Toolbox for this:
N = [10,10,30];
P = 1:-0.25:0.5;
R = binornd(N,P);
You obtain in R the total number of ones for each N(i) trials with P(i) probability. The total number of zeros will be N-R.
Catégories
En savoir plus sur Correlation and Convolution dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!