-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
118 changed files
with
1,109,897 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function [probAnalitica] = analyticProbCoinToss(probCara, numLancamentos, numCaras) | ||
probAnalitica = nchoosek(numLancamentos, numCaras) * (probCara^numCaras) * (1 - probCara)^(numLancamentos - numCaras); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
probSimulacao = probCoinToss(0.5, 3, 2, 1e5) | ||
probAnalitico = analyticProbCoinToss(0.5, 3, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
%% Codigo 1 | ||
% Gerar uma matriz com 3 linhas e 10000 colunas de n´umeros aleat´orios | ||
% entre 0.0 e 1.0 (ou seja, cada coluna representa uma experiˆencia): | ||
experiencias = rand(3,10000); | ||
% Gerar uma matriz com 3 linhas e 10000 colunas com o valor 1 se o valor | ||
% da matriz anterior for superior a 0.5 (ou seja, se saiu cara) ou com o | ||
% valor 0 caso contr´ario (ou seja, saiu coroa): | ||
lancamentos = experiencias > 0.5; % 0.5 corresponde a 1 - prob. de caras | ||
% Gerar um vetor linha com 10000 elementos com a soma dos valores de cada | ||
% coluna da matriz anterior (ou seja, o n´umero de caras de cada experiˆencia): | ||
resultados = sum(lancamentos); | ||
% Gerar um vetor linha com 10000 elementos com o valor 1 quando o valor do | ||
% vetor anterior ´e 2 (ou seja, se a experiˆencia deu 2 caras) ou 0 quando ´e | ||
% diferente de 2: | ||
sucessos = resultados == 2; | ||
% Determinar o resultado final dividindo o n´umero de experiˆencias com 2 | ||
% caras pelo n´umero total de experiˆencias: | ||
probSimulacao = sum(sucessos)/10000; | ||
|
||
fprintf("probSimulacao = %f\n", probSimulacao); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
%% Codigo 2 | ||
N= 1e5; %n´umero de experiˆencias | ||
p = 0.5; %probabilidade de cara | ||
k = 2; %n´umero de caras | ||
n = 3; %n´umero de lanc¸amentos | ||
lancamentos = rand(n,N) > 1-p; | ||
sucessos= sum(lancamentos)==k; | ||
probSimulacao= sum(sucessos)/N; | ||
|
||
fprintf("probSimulacao = %f\n", probSimulacao); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
%% Codigo 3 | ||
N= 1e5; %n´umero de experiˆencias | ||
p = 0.5; %probabilidade de cara | ||
k = 6; %n´umero de caras | ||
n = 15; %n´umero de lanc¸amentos | ||
lancamentos = rand(n,N) > 1-p; | ||
sucessos= sum(lancamentos)==k; | ||
probSimulacao= sum(sucessos)/N; | ||
|
||
fprintf("probSimulacao = %f\n", probSimulacao); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
probSimulacao = probCoinToss(0.5, 15, 6, 1e5) | ||
probAnalitico = analyticProbCoinToss(0.5, 15, 6) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
%% Codigo 4 | ||
N= 1e5; %n´umero de experiˆencias | ||
p = 0.5; %probabilidade de cara | ||
k = 6; %n´umero de caras | ||
n = 15; %n´umero de lanc¸amentos | ||
lancamentos = rand(n,N) > 1-p; | ||
% Pelos menos k lançamentos, o que signficia que | ||
% mais do que k e um caso favoravel. | ||
sucessos= sum(lancamentos)>=k; | ||
probSimulacao= sum(sucessos)/N; | ||
|
||
fprintf("probSimulacao = %f\n", probSimulacao); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
probSimulacao = 0; | ||
|
||
for k = 6:15 | ||
probSimulacao = probSimulacao + probCoinToss(0.5, 15, k, 1e5); | ||
end | ||
|
||
fprintf("probSimulacao = %f\n", probSimulacao); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
throws = 20; | ||
probSimulacao20 = zeros(1, throws + 1); | ||
for k = 0:throws | ||
probSimulacao20(k + 1) = probCoinToss(0.5, throws, k, 1e4); | ||
end | ||
|
||
hold on | ||
stem(0:throws, probSimulacao20) | ||
stem(0:throws, arrayfun(@(k) analyticProbCoinToss(0.5, throws, k), 0:throws)) | ||
hold off | ||
|
||
%% | ||
|
||
throws = 40; | ||
probSimulacao40 = zeros(1, throws + 1); | ||
for k = 0:throws | ||
probSimulacao40(k + 1) = probCoinToss(0.5, throws, k, 1e4); | ||
end | ||
|
||
hold on | ||
stem(0:throws, probSimulacao40) | ||
stem(0:throws, arrayfun(@(k) analyticProbCoinToss(0.5, throws, k), 0:throws)) | ||
hold off | ||
|
||
%% | ||
|
||
throws = 100; | ||
probSimulacao100 = zeros(1, throws + 1); | ||
for k = 0:throws | ||
probSimulacao100(k + 1) = probCoinToss(0.5, throws, k, 1e4); | ||
end | ||
|
||
hold on | ||
stem(0:throws, probSimulacao100) | ||
stem(0:throws, arrayfun(@(k) analyticProbCoinToss(0.5, throws, k), 0:throws)) | ||
hold off |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
%% Parte a) | ||
probSimulacao = probCoinToss(0.3, 5, 3, 1e4) | ||
probAnalitico = analyticProbCoinToss(0.3, 5, 3) | ||
|
||
%% Parte b) | ||
|
||
probSimulacao = 0; | ||
probAnalitico = 0; | ||
|
||
for k = 0:2 | ||
probSimulacao = probSimulacao + probCoinToss(0.3, 5, k, 1e4) | ||
probAnalitico = probAnalitico + analyticProbCoinToss(0.3, 5, k) | ||
end | ||
|
||
probSimulacao | ||
probAnalitico | ||
|
||
%% Parte c) | ||
|
||
|
||
probSimulacao = zeros(1, 6); | ||
|
||
for k = 0:5 | ||
probSimulacao(k + 1) = probCoinToss(0.3, 5, k, 1e4); | ||
end | ||
|
||
bar(0:5, probSimulacao) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
function [probSimulacao] = probCoinToss(probCara, numLancamentos, numCaras, numExperiencias) | ||
lancamentos = rand(numLancamentos, numExperiencias) > (1 - probCara); | ||
sucessos= sum(lancamentos) == numCaras; | ||
probSimulacao= sum(sucessos) / numExperiencias; | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
%% a) | ||
N = 1e5; | ||
p = 0.5; | ||
|
||
nascimentos = rand(2, N); | ||
rapazes = nascimentos < p; | ||
numrapazes = sum(rapazes); | ||
|
||
A = numrapazes >= 1; % Pelo menos um rapaz | ||
|
||
prob = sum(A)/N | ||
|
||
%% b) | ||
|
||
% probabiliaded de nascer um filho | ||
% + | ||
% probabilidade de nascerem dois filhos | ||
prob = 0.5 + 0.5 * 0.5 | ||
|
||
%% c) | ||
|
||
N = 1e5; | ||
p = 0.5; | ||
|
||
nascimentos = rand(2, N); | ||
rapazes = nascimentos < p; | ||
numrapazes = sum(rapazes); | ||
|
||
A = numrapazes == 2; % Nasceram dois rapazes | ||
B = numrapazes >= 1; % Pelo menos um rapaz | ||
|
||
AB = A&B; | ||
|
||
prob = sum(AB)/sum(B) | ||
|
||
%% d) | ||
|
||
N = 1e5; | ||
p = 0.5; | ||
|
||
nascimentos = rand(2, N); | ||
rapazes = nascimentos < p; | ||
numrapazes = sum(rapazes); | ||
|
||
A = numrapazes == 2; % Nasceram dois rapazes | ||
B = rapazes(1,:) == 1; % Primeiro e rapaz | ||
|
||
AB = A&B; | ||
|
||
prob = sum(AB)/sum(B) | ||
|
||
%% e) | ||
|
||
N = 1e5; | ||
p = 0.5; | ||
|
||
nascimentos = rand(5, N); | ||
rapazes = nascimentos < p; | ||
numrapazes = sum(rapazes); | ||
|
||
A = numrapazes == 2; % Nasceram dois rapazes | ||
B = numrapazes >= 1; % Pelo menos um rapaz | ||
|
||
AB = A&B; | ||
|
||
prob = sum(AB)/sum(B) | ||
|
||
%% f) | ||
|
||
N = 1e5; | ||
p = 0.5; | ||
|
||
nascimentos = rand(5, N); | ||
rapazes = nascimentos < p; | ||
numrapazes = sum(rapazes); | ||
|
||
A = numrapazes >= 2; % Nasceram dois rapazes | ||
B = numrapazes >= 1; % Pelo menos um rapaz | ||
|
||
AB = A&B; | ||
|
||
prob = sum(AB)/sum(B) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
%% a) | ||
|
||
n = 20; | ||
m = 100; | ||
|
||
N = 1e5; | ||
|
||
lancamentos = randi(m, n, N); | ||
|
||
A = false(1, N); | ||
|
||
for i=1:N | ||
diferentes = unique(lancamentos(:,i)); | ||
A(i) = length(diferentes) == n; | ||
end | ||
|
||
prob = sum(A)/N | ||
|
||
%% b) | ||
|
||
n = 20; | ||
m = 100; | ||
|
||
N = 1e5; | ||
|
||
lancamentos = randi(m, n, N); | ||
|
||
A = false(1, N); | ||
|
||
for i=1:N | ||
diferentes = unique(lancamentos(:,i)); | ||
A(i) = length(diferentes) < n; | ||
end | ||
|
||
prob = sum(A)/N | ||
|
||
%% c) | ||
|
||
N = 1e5; | ||
x = 10:10:100; | ||
m = [1000 100000]; | ||
|
||
for m_idx = 1:length(m) | ||
alvos = m(m_idx); | ||
|
||
y = zeros(1, length(x)); | ||
|
||
for x_idx=1:length(x) | ||
n = x(x_idx); | ||
lancamentos = randi(alvos, n, N); | ||
|
||
A = false(1, N); | ||
|
||
for i=1:N | ||
diferentes = unique(lancamentos(:,i)); | ||
A(i) = length(diferentes) < n; | ||
end | ||
|
||
y(x_idx) = sum(A)/N; | ||
end | ||
|
||
subplot(1, length(m), m_idx); | ||
plot(x, y); | ||
title(sprintf("Alvos = %d", alvos)); | ||
end | ||
|
||
%% d) | ||
|
||
N = 1e5; | ||
n = 100; | ||
m = [200 500 1000 2000 5000 10000 20000 50000 100000]; | ||
|
||
y = zeros(1, length(m)); | ||
|
||
for x_idx=1:length(m) | ||
alvos = m(x_idx); | ||
lancamentos = randi(alvos, n, N); | ||
|
||
A = false(1, N); | ||
|
||
for i=1:N | ||
diferentes = unique(lancamentos(:,i)); | ||
A(i) = length(diferentes) < n; | ||
end | ||
|
||
y(x_idx) = sum(A)/N; | ||
end | ||
|
||
plot(m, y); |
Oops, something went wrong.