Applying probability concept using matlab
2 ビュー (過去 30 日間)
古いコメントを表示
Hello Experts,
I have a question that has a little something to do with probability that I'd like to work on. So I'd like to understand it on a small scale so I can apply it to a bigger one.
I have a group with four numbers, and I need to select a number from this group, I used randperm(4) so the number will be a random permutation from 1 to 4,
How would you make such that there is 75% possibility for number 2 to pop out as when you choose a number from this random permutation? thank you!
0 件のコメント
回答 (4 件)
the cyclist
2012 年 6 月 17 日
I am not sure I understand your question, but I think you can do what you want using the randsample() function. (For example, you can select different elements with different probabilities by adjusting the weights).
Image Analyst
2012 年 6 月 17 日
It won't be, unless you change your array so that 3 out of the 4 elements have the value 2. If you don't do that the probability that you randomly select the one and only 2 would be 1/4 = 25%.
0 件のコメント
Image Analyst
2012 年 6 月 17 日
Based on your comment to the cyclist, try this:
personHist = zeros(1,4);
% Do a Monte Carlo simulation.
numberOfExperiments = 100000;
for experiment = 1 : numberOfExperiments
randomNumber = rand(1);
if randomNumber < 0.75
person = 2;
elseif randomNumber < 0.75 + 0.25/3
person = 1;
elseif randomNumber < 0.75 + 2*0.25/3
person = 3;
else
person = 4;
end
% Calculate histogram
personHist(person) = personHist(person) + 1;
end
% Normalize.
personHist = 100 * personHist / sum(personHist);
bar(personHist);
xlabel('Person Number');
ylabel('Percentage');
grid on;
% Print out to command window:
personHist
Essentially what you want is that "if" block in the inside of the Monte Carlo for loop.
0 件のコメント
Steven Lord
2021 年 5 月 13 日
This uses several functions that didn't exist when the question was originally asked in 2012, notably discretize, histogram, and yline. It also uses string arrays to create the horizontal lines.
values = [3 6 10 15];
probabilities = [0.75 0.19 0.05 0.01];
cumulativeProbabilities = cumsum([0 probabilities]);
% Discretize the uniform random numbers generated by rand into the bins in
% cumulativeProbabilities
generatedValues = discretize(rand(1, 1e5), cumulativeProbabilities, values);
% Display the results as a histogram
histogram(generatedValues, 'Normalization', 'probability')
% Show horizontal lines at each probability value to see how well the
% actual probabilities agree with the theoretical probabilities
for whichBin = 1:numel(values)
yline(probabilities(whichBin), ':', "value x = " + values(whichBin));
end
The heights of the bins are in close agreement to the probabilities in the probabilities vector.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!