From 4fd289f38a4158c919ab5e196e20c1884083ff9b Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 07:19:53 +1000 Subject: [PATCH 01/33] Create Image_Classification_Example_MNIST_CNN.py Image Classification Example --- .../Image_Classification_Example_MNIST_CNN.py | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 computer_vision/Image_Classification_Example_MNIST_CNN.py diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/Image_Classification_Example_MNIST_CNN.py new file mode 100644 index 000000000000..b80707a11978 --- /dev/null +++ b/computer_vision/Image_Classification_Example_MNIST_CNN.py @@ -0,0 +1,141 @@ +""" +This Script contains the default EMNIST code for comparison. + +Execution Details are available here: +https://www.kaggle.com/code/dipuk0506/mnist-cnn + +The code is modified from: + nextjournal.com/gkoehler/pytorch-mnist + +@author: Dipu Kabir +""" + +import torch, torchvision +import torch.nn as nn +import torch.nn.functional as F +import torch.optim as optim + +n_epochs = 8 +batch_size_train = 64 +batch_size_test = 1000 +learning_rate = 0.01 +momentum = 0.5 +log_interval = 100 + + +torch.backends.cudnn.enabled = False + + +train_loader = torch.utils.data.DataLoader( + torchvision.datasets.MNIST('/files/', train=True, download=True, + transform=torchvision.transforms.Compose([ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize( + (0.1307,), (0.3081,)) + ])), + batch_size=batch_size_train, shuffle=True) + +test_loader = torch.utils.data.DataLoader( + torchvision.datasets.MNIST('/files/', train=False, download=True, + transform=torchvision.transforms.Compose([ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize( + (0.1307,), (0.3081,)) + ])), + batch_size=batch_size_test, shuffle=True) + +examples = enumerate(test_loader) +batch_idx, (example_data, example_targets) = next(examples) + +print(example_data.shape) + +import matplotlib.pyplot as plt + +fig = plt.figure() +for i in range(6): + plt.subplot(2,3,i+1) + plt.tight_layout() + plt.imshow(example_data[i][0], cmap='gray', interpolation='none') + plt.title("Ground Truth: {}".format(example_targets[i])) + plt.xticks([]) + plt.yticks([]) +fig + + +class Net(nn.Module): + def __init__(self): + super(Net, self).__init__() + self.conv1 = nn.Conv2d(1, 10, kernel_size=5) + self.conv2 = nn.Conv2d(10, 20, kernel_size=5) + self.conv2_drop = nn.Dropout2d() + self.fc1 = nn.Linear(320, 50) + self.fc2 = nn.Linear(50, 10) + + def forward(self, x): + x = F.relu(F.max_pool2d(self.conv1(x), 2)) + x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) + x = x.view(-1, 320) + x = F.relu(self.fc1(x)) + x = F.dropout(x, training=self.training) + x = self.fc2(x) + return F.log_softmax(x) + + + +network = Net() +optimizer = optim.SGD(network.parameters(), lr=learning_rate, + momentum=momentum) + + +train_losses = [] +train_counter = [] +test_losses = [] +test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)] + + +def train(epoch): + network.train() + for batch_idx, (data, target) in enumerate(train_loader): + optimizer.zero_grad() + output = network(data) + loss = F.nll_loss(output, target) + loss.backward() + optimizer.step() + if batch_idx % log_interval == 0: + print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( + epoch, batch_idx * len(data), len(train_loader.dataset), + 100. * batch_idx / len(train_loader), loss.item())) + train_losses.append(loss.item()) + train_counter.append( + (batch_idx*64) + ((epoch-1)*len(train_loader.dataset))) + +def test(): + network.eval() + test_loss = 0 + correct = 0 + with torch.no_grad(): + for data, target in test_loader: + output = network(data) + test_loss += F.nll_loss(output, target, size_average=False).item() + pred = output.data.max(1, keepdim=True)[1] + correct += pred.eq(target.data.view_as(pred)).sum() + test_loss /= len(test_loader.dataset) + test_losses.append(test_loss) + print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( + test_loss, correct, len(test_loader.dataset), + 100. * correct / len(test_loader.dataset))) + + +test() +for epoch in range(1, n_epochs + 1): + train(epoch) + test() +#%% + +fig = plt.figure() +plt.plot(train_counter, train_losses, color='blue') +plt.scatter(test_counter, test_losses, color='red') +plt.legend(['Train Loss', 'Test Loss'], loc='upper right') +plt.xlabel('number of training examples seen') +plt.ylabel('negative log likelihood loss') +fig From 29671f5efd92fd35abc684565415566125ef291e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 10 Jun 2024 21:28:16 +0000 Subject: [PATCH 02/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../Image_Classification_Example_MNIST_CNN.py | 157 ++++++++++-------- 1 file changed, 91 insertions(+), 66 deletions(-) diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/Image_Classification_Example_MNIST_CNN.py index b80707a11978..d9e34f7cf28e 100644 --- a/computer_vision/Image_Classification_Example_MNIST_CNN.py +++ b/computer_vision/Image_Classification_Example_MNIST_CNN.py @@ -1,7 +1,7 @@ """ This Script contains the default EMNIST code for comparison. -Execution Details are available here: +Execution Details are available here: https://www.kaggle.com/code/dipuk0506/mnist-cnn The code is modified from: @@ -27,22 +27,36 @@ train_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST('/files/', train=True, download=True, - transform=torchvision.transforms.Compose([ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize( - (0.1307,), (0.3081,)) - ])), - batch_size=batch_size_train, shuffle=True) + torchvision.datasets.MNIST( + "/files/", + train=True, + download=True, + transform=torchvision.transforms.Compose( + [ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize((0.1307,), (0.3081,)), + ] + ), + ), + batch_size=batch_size_train, + shuffle=True, +) test_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST('/files/', train=False, download=True, - transform=torchvision.transforms.Compose([ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize( - (0.1307,), (0.3081,)) - ])), - batch_size=batch_size_test, shuffle=True) + torchvision.datasets.MNIST( + "/files/", + train=False, + download=True, + transform=torchvision.transforms.Compose( + [ + torchvision.transforms.ToTensor(), + torchvision.transforms.Normalize((0.1307,), (0.3081,)), + ] + ), + ), + batch_size=batch_size_test, + shuffle=True, +) examples = enumerate(test_loader) batch_idx, (example_data, example_targets) = next(examples) @@ -53,12 +67,12 @@ fig = plt.figure() for i in range(6): - plt.subplot(2,3,i+1) - plt.tight_layout() - plt.imshow(example_data[i][0], cmap='gray', interpolation='none') - plt.title("Ground Truth: {}".format(example_targets[i])) - plt.xticks([]) - plt.yticks([]) + plt.subplot(2, 3, i + 1) + plt.tight_layout() + plt.imshow(example_data[i][0], cmap="gray", interpolation="none") + plt.title("Ground Truth: {}".format(example_targets[i])) + plt.xticks([]) + plt.yticks([]) fig @@ -79,63 +93,74 @@ def forward(self, x): x = F.dropout(x, training=self.training) x = self.fc2(x) return F.log_softmax(x) - - - + + network = Net() -optimizer = optim.SGD(network.parameters(), lr=learning_rate, - momentum=momentum) +optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum) train_losses = [] train_counter = [] test_losses = [] -test_counter = [i*len(train_loader.dataset) for i in range(n_epochs + 1)] +test_counter = [i * len(train_loader.dataset) for i in range(n_epochs + 1)] def train(epoch): - network.train() - for batch_idx, (data, target) in enumerate(train_loader): - optimizer.zero_grad() - output = network(data) - loss = F.nll_loss(output, target) - loss.backward() - optimizer.step() - if batch_idx % log_interval == 0: - print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format( - epoch, batch_idx * len(data), len(train_loader.dataset), - 100. * batch_idx / len(train_loader), loss.item())) - train_losses.append(loss.item()) - train_counter.append( - (batch_idx*64) + ((epoch-1)*len(train_loader.dataset))) - + network.train() + for batch_idx, (data, target) in enumerate(train_loader): + optimizer.zero_grad() + output = network(data) + loss = F.nll_loss(output, target) + loss.backward() + optimizer.step() + if batch_idx % log_interval == 0: + print( + "Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}".format( + epoch, + batch_idx * len(data), + len(train_loader.dataset), + 100.0 * batch_idx / len(train_loader), + loss.item(), + ) + ) + train_losses.append(loss.item()) + train_counter.append( + (batch_idx * 64) + ((epoch - 1) * len(train_loader.dataset)) + ) + + def test(): - network.eval() - test_loss = 0 - correct = 0 - with torch.no_grad(): - for data, target in test_loader: - output = network(data) - test_loss += F.nll_loss(output, target, size_average=False).item() - pred = output.data.max(1, keepdim=True)[1] - correct += pred.eq(target.data.view_as(pred)).sum() - test_loss /= len(test_loader.dataset) - test_losses.append(test_loss) - print('\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format( - test_loss, correct, len(test_loader.dataset), - 100. * correct / len(test_loader.dataset))) - - + network.eval() + test_loss = 0 + correct = 0 + with torch.no_grad(): + for data, target in test_loader: + output = network(data) + test_loss += F.nll_loss(output, target, size_average=False).item() + pred = output.data.max(1, keepdim=True)[1] + correct += pred.eq(target.data.view_as(pred)).sum() + test_loss /= len(test_loader.dataset) + test_losses.append(test_loss) + print( + "\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n".format( + test_loss, + correct, + len(test_loader.dataset), + 100.0 * correct / len(test_loader.dataset), + ) + ) + + test() for epoch in range(1, n_epochs + 1): - train(epoch) - test() -#%% + train(epoch) + test() +# %% fig = plt.figure() -plt.plot(train_counter, train_losses, color='blue') -plt.scatter(test_counter, test_losses, color='red') -plt.legend(['Train Loss', 'Test Loss'], loc='upper right') -plt.xlabel('number of training examples seen') -plt.ylabel('negative log likelihood loss') +plt.plot(train_counter, train_losses, color="blue") +plt.scatter(test_counter, test_losses, color="red") +plt.legend(["Train Loss", "Test Loss"], loc="upper right") +plt.xlabel("number of training examples seen") +plt.ylabel("negative log likelihood loss") fig From 4e3484a3cf8b34a4166f96d4f3448785dff6e50d Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 14:18:40 +1000 Subject: [PATCH 03/33] Update and rename Image_Classification_Example_MNIST_CNN.py to image_classification_example_mnist_cnn.py --- ...image_classification_example_mnist_cnn.py} | 21 ++++++++++--------- 1 file changed, 11 insertions(+), 10 deletions(-) rename computer_vision/{Image_Classification_Example_MNIST_CNN.py => image_classification_example_mnist_cnn.py} (88%) diff --git a/computer_vision/Image_Classification_Example_MNIST_CNN.py b/computer_vision/image_classification_example_mnist_cnn.py similarity index 88% rename from computer_vision/Image_Classification_Example_MNIST_CNN.py rename to computer_vision/image_classification_example_mnist_cnn.py index d9e34f7cf28e..5aa8761ca588 100644 --- a/computer_vision/Image_Classification_Example_MNIST_CNN.py +++ b/computer_vision/image_classification_example_mnist_cnn.py @@ -4,15 +4,16 @@ Execution Details are available here: https://www.kaggle.com/code/dipuk0506/mnist-cnn -The code is modified from: +The code is improved from: nextjournal.com/gkoehler/pytorch-mnist @author: Dipu Kabir """ -import torch, torchvision +import torch +import torchvision import torch.nn as nn -import torch.nn.functional as F +import torch.nn.functional as functional import torch.optim as optim n_epochs = 8 @@ -86,13 +87,13 @@ def __init__(self): self.fc2 = nn.Linear(50, 10) def forward(self, x): - x = F.relu(F.max_pool2d(self.conv1(x), 2)) - x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) + x = functional.relu(functional.max_pool2d(self.conv1(x), 2)) + x = functional.relu(functional.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) x = x.view(-1, 320) - x = F.relu(self.fc1(x)) - x = F.dropout(x, training=self.training) + x = functional.relu(self.fc1(x)) + x = functional.dropout(x, training=self.training) x = self.fc2(x) - return F.log_softmax(x) + return functional.log_softmax(x) network = Net() @@ -110,7 +111,7 @@ def train(epoch): for batch_idx, (data, target) in enumerate(train_loader): optimizer.zero_grad() output = network(data) - loss = F.nll_loss(output, target) + loss = functional.nll_loss(output, target) loss.backward() optimizer.step() if batch_idx % log_interval == 0: @@ -136,7 +137,7 @@ def test(): with torch.no_grad(): for data, target in test_loader: output = network(data) - test_loss += F.nll_loss(output, target, size_average=False).item() + test_loss += functional.nll_loss(output, target, size_average=False).item() pred = output.data.max(1, keepdim=True)[1] correct += pred.eq(target.data.view_as(pred)).sum() test_loss /= len(test_loader.dataset) From 7b70f64d5adf1e5717a17af167856b92f2287913 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 15:57:55 +1000 Subject: [PATCH 04/33] Update image_classification_example_mnist_cnn.py --- computer_vision/image_classification_example_mnist_cnn.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/computer_vision/image_classification_example_mnist_cnn.py b/computer_vision/image_classification_example_mnist_cnn.py index 5aa8761ca588..85fee01d45fe 100644 --- a/computer_vision/image_classification_example_mnist_cnn.py +++ b/computer_vision/image_classification_example_mnist_cnn.py @@ -10,6 +10,8 @@ @author: Dipu Kabir """ +! pip install torch + import torch import torchvision import torch.nn as nn From a4b73300d8d76045b708ae653bfca7dd07047a6e Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Tue, 11 Jun 2024 16:07:14 +1000 Subject: [PATCH 05/33] Delete computer_vision/image_classification_example_mnist_cnn.py --- .../image_classification_example_mnist_cnn.py | 169 ------------------ 1 file changed, 169 deletions(-) delete mode 100644 computer_vision/image_classification_example_mnist_cnn.py diff --git a/computer_vision/image_classification_example_mnist_cnn.py b/computer_vision/image_classification_example_mnist_cnn.py deleted file mode 100644 index 85fee01d45fe..000000000000 --- a/computer_vision/image_classification_example_mnist_cnn.py +++ /dev/null @@ -1,169 +0,0 @@ -""" -This Script contains the default EMNIST code for comparison. - -Execution Details are available here: -https://www.kaggle.com/code/dipuk0506/mnist-cnn - -The code is improved from: - nextjournal.com/gkoehler/pytorch-mnist - -@author: Dipu Kabir -""" - -! pip install torch - -import torch -import torchvision -import torch.nn as nn -import torch.nn.functional as functional -import torch.optim as optim - -n_epochs = 8 -batch_size_train = 64 -batch_size_test = 1000 -learning_rate = 0.01 -momentum = 0.5 -log_interval = 100 - - -torch.backends.cudnn.enabled = False - - -train_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST( - "/files/", - train=True, - download=True, - transform=torchvision.transforms.Compose( - [ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize((0.1307,), (0.3081,)), - ] - ), - ), - batch_size=batch_size_train, - shuffle=True, -) - -test_loader = torch.utils.data.DataLoader( - torchvision.datasets.MNIST( - "/files/", - train=False, - download=True, - transform=torchvision.transforms.Compose( - [ - torchvision.transforms.ToTensor(), - torchvision.transforms.Normalize((0.1307,), (0.3081,)), - ] - ), - ), - batch_size=batch_size_test, - shuffle=True, -) - -examples = enumerate(test_loader) -batch_idx, (example_data, example_targets) = next(examples) - -print(example_data.shape) - -import matplotlib.pyplot as plt - -fig = plt.figure() -for i in range(6): - plt.subplot(2, 3, i + 1) - plt.tight_layout() - plt.imshow(example_data[i][0], cmap="gray", interpolation="none") - plt.title("Ground Truth: {}".format(example_targets[i])) - plt.xticks([]) - plt.yticks([]) -fig - - -class Net(nn.Module): - def __init__(self): - super(Net, self).__init__() - self.conv1 = nn.Conv2d(1, 10, kernel_size=5) - self.conv2 = nn.Conv2d(10, 20, kernel_size=5) - self.conv2_drop = nn.Dropout2d() - self.fc1 = nn.Linear(320, 50) - self.fc2 = nn.Linear(50, 10) - - def forward(self, x): - x = functional.relu(functional.max_pool2d(self.conv1(x), 2)) - x = functional.relu(functional.max_pool2d(self.conv2_drop(self.conv2(x)), 2)) - x = x.view(-1, 320) - x = functional.relu(self.fc1(x)) - x = functional.dropout(x, training=self.training) - x = self.fc2(x) - return functional.log_softmax(x) - - -network = Net() -optimizer = optim.SGD(network.parameters(), lr=learning_rate, momentum=momentum) - - -train_losses = [] -train_counter = [] -test_losses = [] -test_counter = [i * len(train_loader.dataset) for i in range(n_epochs + 1)] - - -def train(epoch): - network.train() - for batch_idx, (data, target) in enumerate(train_loader): - optimizer.zero_grad() - output = network(data) - loss = functional.nll_loss(output, target) - loss.backward() - optimizer.step() - if batch_idx % log_interval == 0: - print( - "Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}".format( - epoch, - batch_idx * len(data), - len(train_loader.dataset), - 100.0 * batch_idx / len(train_loader), - loss.item(), - ) - ) - train_losses.append(loss.item()) - train_counter.append( - (batch_idx * 64) + ((epoch - 1) * len(train_loader.dataset)) - ) - - -def test(): - network.eval() - test_loss = 0 - correct = 0 - with torch.no_grad(): - for data, target in test_loader: - output = network(data) - test_loss += functional.nll_loss(output, target, size_average=False).item() - pred = output.data.max(1, keepdim=True)[1] - correct += pred.eq(target.data.view_as(pred)).sum() - test_loss /= len(test_loader.dataset) - test_losses.append(test_loss) - print( - "\nTest set: Avg. loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n".format( - test_loss, - correct, - len(test_loader.dataset), - 100.0 * correct / len(test_loader.dataset), - ) - ) - - -test() -for epoch in range(1, n_epochs + 1): - train(epoch) - test() -# %% - -fig = plt.figure() -plt.plot(train_counter, train_losses, color="blue") -plt.scatter(test_counter, test_losses, color="red") -plt.legend(["Train Loss", "Test Loss"], loc="upper right") -plt.xlabel("number of training examples seen") -plt.ylabel("negative log likelihood loss") -fig From 158c1feaf3908b4cf80c44032d145374d6d8641d Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 22:23:49 +1000 Subject: [PATCH 06/33] Create probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 43 ++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 maths/probability_of_n_heads_in_m_tossing.py diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py new file mode 100644 index 000000000000..e8a39fc2d647 --- /dev/null +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -0,0 +1,43 @@ +import numpy + +def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: + """ + Calculate the factorial of specified number (n!). + + >>> import math + >>> all(factorial(i) == math.factorial(i) for i in range(20)) + True + >>> Probability_of_n_heads_in_m_tossing(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> Probability_of_n_heads_in_m_tossing(-1,5) + Traceback (most recent call last): + ... + ValueError: The function is not defined for negative values + >>> Probability_of_n_heads_in_m_tossing(3,2) + Traceback (most recent call last): + ... + ValueError: Head count should be smaller than toss count + + >>> Probability_of_n_heads_in_m_tossing(1,1) + 0.5 + >>> Probability_of_n_heads_in_m_tossing(0,2) + 0.25 + >>> Probability_of_n_heads_in_m_tossing(2,3) + 0.375 + """ + if head_count != int(head_count) or toss_count != int(toss_count): + raise ValueError("The function only accepts integer values") + if head_count < 0 or toss_count < 0: + raise ValueError("The function only accepts positive values") + if head_count > toss_count: + raise ValueError("Head count should be smaller than toss count") + + value = numpy.ones(1) + + for iter1 in range(toss_count): + value = numpy.append(value, [0], axis=0) + numpy.append([0], value, axis=0) + value = value/2 + + return value[head_count] From d8b68ba00027f6124fffbf1c9270fcaa6a63935f Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 22:51:17 +1000 Subject: [PATCH 07/33] Update probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index e8a39fc2d647..ac48cba605f1 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,3 +1,27 @@ + + """ +Algorithm Explanation: +If you toss 0 time -> 0 head +Distribution [1] -> Meaning: 1 in the 0-index + +If you toss 1 time -> 0 or 1 head +Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes + +If you toss 2 times -> 0 to 2 heads +Distribution [0.25 0.5 0.25] -> Meaning: probability of n heads from the distribution {HH, HT, TH, TT} + +If you toss 3 times -> 0 to 3 heads +Distribution [0.125 0.375 0.375 0.125] -> Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} + +Therefore, +Probability_distribution(N+1) = [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 + +I used that method in my paper +Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions +Journal: ACM Computing Surveys (CSUR) +""" + + import numpy def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: From c49619c98124052c931d5c724a7bdca844a33ba4 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 12:54:04 +0000 Subject: [PATCH 08/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/probability_of_n_heads_in_m_tossing.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index ac48cba605f1..c15acd0cc8f6 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -8,16 +8,16 @@ Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes If you toss 2 times -> 0 to 2 heads -Distribution [0.25 0.5 0.25] -> Meaning: probability of n heads from the distribution {HH, HT, TH, TT} +Distribution [0.25 0.5 0.25] -> Meaning: probability of n heads from the distribution {HH, HT, TH, TT} If you toss 3 times -> 0 to 3 heads -Distribution [0.125 0.375 0.375 0.125] -> Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} +Distribution [0.125 0.375 0.375 0.125] -> Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} Therefore, Probability_distribution(N+1) = [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 -I used that method in my paper -Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions +I used that method in my paper +Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions Journal: ACM Computing Surveys (CSUR) """ @@ -43,7 +43,7 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int Traceback (most recent call last): ... ValueError: Head count should be smaller than toss count - + >>> Probability_of_n_heads_in_m_tossing(1,1) 0.5 >>> Probability_of_n_heads_in_m_tossing(0,2) @@ -57,11 +57,11 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int raise ValueError("The function only accepts positive values") if head_count > toss_count: raise ValueError("Head count should be smaller than toss count") - + value = numpy.ones(1) - + for iter1 in range(toss_count): value = numpy.append(value, [0], axis=0) + numpy.append([0], value, axis=0) value = value/2 - + return value[head_count] From b62a77452eaf27b2672fd09c73b0618cfd1ea9ee Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 22:58:29 +1000 Subject: [PATCH 09/33] Update probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index c15acd0cc8f6..fb1a1b3c3f85 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,5 +1,5 @@ - """ +""" Algorithm Explanation: If you toss 0 time -> 0 head Distribution [1] -> Meaning: 1 in the 0-index @@ -8,16 +8,22 @@ Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes If you toss 2 times -> 0 to 2 heads -Distribution [0.25 0.5 0.25] -> Meaning: probability of n heads from the distribution {HH, HT, TH, TT} +Distribution [0.25 0.5 0.25] -> +Meaning: probability of n heads from the distribution +{HH, HT, TH, TT} If you toss 3 times -> 0 to 3 heads -Distribution [0.125 0.375 0.375 0.125] -> Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} +Distribution [0.125 0.375 0.375 0.125] -> +Meaning: probability of n heads from the distribution +{HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} Therefore, -Probability_distribution(N+1) = [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 +Probability_distribution(N+1) = + [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 I used that method in my paper -Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions +Titled: Uncertainty-aware Decisions in Cloud Computing: +Foundations and Future Directions Journal: ACM Computing Surveys (CSUR) """ From 707b446bcf08be50dae93df93c7d85449caf3a0a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:01:19 +0000 Subject: [PATCH 10/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/probability_of_n_heads_in_m_tossing.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index fb1a1b3c3f85..92613fc68c1c 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,4 +1,3 @@ - """ Algorithm Explanation: If you toss 0 time -> 0 head @@ -8,28 +7,28 @@ Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes If you toss 2 times -> 0 to 2 heads -Distribution [0.25 0.5 0.25] -> -Meaning: probability of n heads from the distribution +Distribution [0.25 0.5 0.25] -> +Meaning: probability of n heads from the distribution {HH, HT, TH, TT} If you toss 3 times -> 0 to 3 heads -Distribution [0.125 0.375 0.375 0.125] -> -Meaning: probability of n heads from the distribution +Distribution [0.125 0.375 0.375 0.125] -> +Meaning: probability of n heads from the distribution {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} Therefore, -Probability_distribution(N+1) = +Probability_distribution(N+1) = [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 I used that method in my paper -Titled: Uncertainty-aware Decisions in Cloud Computing: +Titled: Uncertainty-aware Decisions in Cloud Computing: Foundations and Future Directions Journal: ACM Computing Surveys (CSUR) """ - import numpy + def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: """ Calculate the factorial of specified number (n!). @@ -68,6 +67,6 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int for iter1 in range(toss_count): value = numpy.append(value, [0], axis=0) + numpy.append([0], value, axis=0) - value = value/2 + value = value / 2 return value[head_count] From df400a70009ecfe457cc061b14a9834c0729fbd7 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 23:08:23 +1000 Subject: [PATCH 11/33] Update probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index 92613fc68c1c..910deb228075 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,4 +1,6 @@ """ +The probability of getting exactly n heads in m tossing. + Algorithm Explanation: If you toss 0 time -> 0 head Distribution [1] -> Meaning: 1 in the 0-index @@ -26,7 +28,7 @@ Journal: ACM Computing Surveys (CSUR) """ -import numpy +import numpy as np def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: @@ -63,10 +65,12 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int if head_count > toss_count: raise ValueError("Head count should be smaller than toss count") - value = numpy.ones(1) - - for iter1 in range(toss_count): - value = numpy.append(value, [0], axis=0) + numpy.append([0], value, axis=0) + value = np.ones(1) + + iter1 = 0 + while iter1 < toss_count : + value = np.append(value, [0], axis=0) + np.append([0], value, axis=0) value = value / 2 + iter1 = iter1 +1 return value[head_count] From 929301c2d6f6e19a22969046beabaf82a8c97a3e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:10:58 +0000 Subject: [PATCH 12/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/probability_of_n_heads_in_m_tossing.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index 910deb228075..4f72e6dc0d08 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -66,11 +66,11 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int raise ValueError("Head count should be smaller than toss count") value = np.ones(1) - + iter1 = 0 - while iter1 < toss_count : + while iter1 < toss_count: value = np.append(value, [0], axis=0) + np.append([0], value, axis=0) value = value / 2 - iter1 = iter1 +1 + iter1 = iter1 + 1 return value[head_count] From b84ac598d2180cee9d03d7e91fe5e2337c25476b Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 23:17:25 +1000 Subject: [PATCH 13/33] Update probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index 4f72e6dc0d08..d9323ce2d5a6 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -64,6 +64,8 @@ def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int raise ValueError("The function only accepts positive values") if head_count > toss_count: raise ValueError("Head count should be smaller than toss count") + if toss_count > 100: + raise ValueError("Limited to 100 tossing to avoid memory issues") value = np.ones(1) From b79e0c15f398f2a41911ee27c5bacafadf7ad870 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 23:29:08 +1000 Subject: [PATCH 14/33] Update probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 56 +++++++++----------- 1 file changed, 26 insertions(+), 30 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index d9323ce2d5a6..8ab2bea3b62d 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,43 +1,39 @@ -""" -The probability of getting exactly n heads in m tossing. -Algorithm Explanation: -If you toss 0 time -> 0 head -Distribution [1] -> Meaning: 1 in the 0-index -If you toss 1 time -> 0 or 1 head -Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes +import numpy as np -If you toss 2 times -> 0 to 2 heads -Distribution [0.25 0.5 0.25] -> -Meaning: probability of n heads from the distribution -{HH, HT, TH, TT} -If you toss 3 times -> 0 to 3 heads -Distribution [0.125 0.375 0.375 0.125] -> -Meaning: probability of n heads from the distribution -{HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} +def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: + """ + The probability of getting exactly n heads in m tossing. -Therefore, -Probability_distribution(N+1) = - [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 + Algorithm Explanation: + If you toss 0 time -> 0 head + Distribution [1] -> Meaning: 1 in the 0-index -I used that method in my paper -Titled: Uncertainty-aware Decisions in Cloud Computing: -Foundations and Future Directions -Journal: ACM Computing Surveys (CSUR) -""" + If you toss 1 time -> 0 or 1 head + Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes -import numpy as np + If you toss 2 times -> 0 to 2 heads + Distribution [0.25 0.5 0.25] -> + Meaning: probability of n heads from the distribution + {HH, HT, TH, TT} + If you toss 3 times -> 0 to 3 heads + Distribution [0.125 0.375 0.375 0.125] -> + Meaning: probability of n heads from the distribution + {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} -def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: - """ - Calculate the factorial of specified number (n!). + Therefore, + Probability_distribution(N+1) = + [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 + + I used that method in my paper + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) - >>> import math - >>> all(factorial(i) == math.factorial(i) for i in range(20)) - True + >>> import numpy as np >>> Probability_of_n_heads_in_m_tossing(.2,.5) Traceback (most recent call last): ... From 205cb8731bb45c4286a2f930a235dea80ee9604a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:31:12 +0000 Subject: [PATCH 15/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/probability_of_n_heads_in_m_tossing.py | 82 ++++++++++---------- 1 file changed, 40 insertions(+), 42 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index 8ab2bea3b62d..d3c913dd4ccf 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,58 +1,56 @@ - - import numpy as np def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: """ - The probability of getting exactly n heads in m tossing. + The probability of getting exactly n heads in m tossing. - Algorithm Explanation: - If you toss 0 time -> 0 head - Distribution [1] -> Meaning: 1 in the 0-index + Algorithm Explanation: + If you toss 0 time -> 0 head + Distribution [1] -> Meaning: 1 in the 0-index - If you toss 1 time -> 0 or 1 head - Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes + If you toss 1 time -> 0 or 1 head + Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes - If you toss 2 times -> 0 to 2 heads - Distribution [0.25 0.5 0.25] -> - Meaning: probability of n heads from the distribution - {HH, HT, TH, TT} + If you toss 2 times -> 0 to 2 heads + Distribution [0.25 0.5 0.25] -> + Meaning: probability of n heads from the distribution + {HH, HT, TH, TT} - If you toss 3 times -> 0 to 3 heads - Distribution [0.125 0.375 0.375 0.125] -> - Meaning: probability of n heads from the distribution - {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} + If you toss 3 times -> 0 to 3 heads + Distribution [0.125 0.375 0.375 0.125] -> + Meaning: probability of n heads from the distribution + {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} - Therefore, - Probability_distribution(N+1) = - [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 + Therefore, + Probability_distribution(N+1) = + [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 - I used that method in my paper - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) + I used that method in my paper + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) - >>> import numpy as np - >>> Probability_of_n_heads_in_m_tossing(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> Probability_of_n_heads_in_m_tossing(-1,5) - Traceback (most recent call last): - ... - ValueError: The function is not defined for negative values - >>> Probability_of_n_heads_in_m_tossing(3,2) - Traceback (most recent call last): - ... - ValueError: Head count should be smaller than toss count + >>> import numpy as np + >>> Probability_of_n_heads_in_m_tossing(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> Probability_of_n_heads_in_m_tossing(-1,5) + Traceback (most recent call last): + ... + ValueError: The function is not defined for negative values + >>> Probability_of_n_heads_in_m_tossing(3,2) + Traceback (most recent call last): + ... + ValueError: Head count should be smaller than toss count - >>> Probability_of_n_heads_in_m_tossing(1,1) - 0.5 - >>> Probability_of_n_heads_in_m_tossing(0,2) - 0.25 - >>> Probability_of_n_heads_in_m_tossing(2,3) - 0.375 + >>> Probability_of_n_heads_in_m_tossing(1,1) + 0.5 + >>> Probability_of_n_heads_in_m_tossing(0,2) + 0.25 + >>> Probability_of_n_heads_in_m_tossing(2,3) + 0.375 """ if head_count != int(head_count) or toss_count != int(toss_count): raise ValueError("The function only accepts integer values") From c9879497ddb1633575ba183e21a2cbe1599f3102 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Wed, 12 Jun 2024 23:37:07 +1000 Subject: [PATCH 16/33] Update probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 81 ++++++++++---------- 1 file changed, 41 insertions(+), 40 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index d3c913dd4ccf..ba5f47c18d0d 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,56 +1,57 @@ + import numpy as np def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: """ - The probability of getting exactly n heads in m tossing. + The probability of getting exactly n heads in m tossing. - Algorithm Explanation: - If you toss 0 time -> 0 head - Distribution [1] -> Meaning: 1 in the 0-index + Algorithm Explanation: + If you toss 0 time -> 0 head + Distribution [1] -> Meaning: 1 in the 0-index - If you toss 1 time -> 0 or 1 head - Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes + If you toss 1 time -> 0 or 1 head + Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes - If you toss 2 times -> 0 to 2 heads - Distribution [0.25 0.5 0.25] -> - Meaning: probability of n heads from the distribution - {HH, HT, TH, TT} + If you toss 2 times -> 0 to 2 heads + Distribution [0.25 0.5 0.25] -> + Meaning: probability of n heads from the distribution + {HH, HT, TH, TT} - If you toss 3 times -> 0 to 3 heads - Distribution [0.125 0.375 0.375 0.125] -> - Meaning: probability of n heads from the distribution - {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} + If you toss 3 times -> 0 to 3 heads + Distribution [0.125 0.375 0.375 0.125] -> + Meaning: probability of n heads from the distribution + {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} - Therefore, - Probability_distribution(N+1) = - [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 + Therefore, + Probability_distribution(N+1) = + [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 - I used that method in my paper - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) + I used that method in my paper + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) - >>> import numpy as np - >>> Probability_of_n_heads_in_m_tossing(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> Probability_of_n_heads_in_m_tossing(-1,5) - Traceback (most recent call last): - ... - ValueError: The function is not defined for negative values - >>> Probability_of_n_heads_in_m_tossing(3,2) - Traceback (most recent call last): - ... - ValueError: Head count should be smaller than toss count + >>> import numpy as np + >>> probability_of_n_heads_in_m_tossing(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> probability_of_n_heads_in_m_tossing(-1,5) + Traceback (most recent call last): + ... + ValueError: The function is not defined for negative values + >>> probability_of_n_heads_in_m_tossing(3,2) + Traceback (most recent call last): + ... + ValueError: Head count should be smaller than toss count - >>> Probability_of_n_heads_in_m_tossing(1,1) - 0.5 - >>> Probability_of_n_heads_in_m_tossing(0,2) - 0.25 - >>> Probability_of_n_heads_in_m_tossing(2,3) - 0.375 + >>> probability_of_n_heads_in_m_tossing(1,1) + 0.5 + >>> probability_of_n_heads_in_m_tossing(0,2) + 0.25 + >>> probability_of_n_heads_in_m_tossing(2,3) + 0.375 """ if head_count != int(head_count) or toss_count != int(toss_count): raise ValueError("The function only accepts integer values") From 366f1ffd29a0841b9d2e3c5c307f84d8e668701e Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 12 Jun 2024 13:40:13 +0000 Subject: [PATCH 17/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/probability_of_n_heads_in_m_tossing.py | 81 ++++++++++---------- 1 file changed, 40 insertions(+), 41 deletions(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index ba5f47c18d0d..7f9606d4bdaa 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,57 +1,56 @@ - import numpy as np def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: """ - The probability of getting exactly n heads in m tossing. + The probability of getting exactly n heads in m tossing. - Algorithm Explanation: - If you toss 0 time -> 0 head - Distribution [1] -> Meaning: 1 in the 0-index + Algorithm Explanation: + If you toss 0 time -> 0 head + Distribution [1] -> Meaning: 1 in the 0-index - If you toss 1 time -> 0 or 1 head - Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes + If you toss 1 time -> 0 or 1 head + Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes - If you toss 2 times -> 0 to 2 heads - Distribution [0.25 0.5 0.25] -> - Meaning: probability of n heads from the distribution - {HH, HT, TH, TT} + If you toss 2 times -> 0 to 2 heads + Distribution [0.25 0.5 0.25] -> + Meaning: probability of n heads from the distribution + {HH, HT, TH, TT} - If you toss 3 times -> 0 to 3 heads - Distribution [0.125 0.375 0.375 0.125] -> - Meaning: probability of n heads from the distribution - {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} + If you toss 3 times -> 0 to 3 heads + Distribution [0.125 0.375 0.375 0.125] -> + Meaning: probability of n heads from the distribution + {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} - Therefore, - Probability_distribution(N+1) = - [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 + Therefore, + Probability_distribution(N+1) = + [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 - I used that method in my paper - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) + I used that method in my paper + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) - >>> import numpy as np - >>> probability_of_n_heads_in_m_tossing(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> probability_of_n_heads_in_m_tossing(-1,5) - Traceback (most recent call last): - ... - ValueError: The function is not defined for negative values - >>> probability_of_n_heads_in_m_tossing(3,2) - Traceback (most recent call last): - ... - ValueError: Head count should be smaller than toss count + >>> import numpy as np + >>> probability_of_n_heads_in_m_tossing(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> probability_of_n_heads_in_m_tossing(-1,5) + Traceback (most recent call last): + ... + ValueError: The function is not defined for negative values + >>> probability_of_n_heads_in_m_tossing(3,2) + Traceback (most recent call last): + ... + ValueError: Head count should be smaller than toss count - >>> probability_of_n_heads_in_m_tossing(1,1) - 0.5 - >>> probability_of_n_heads_in_m_tossing(0,2) - 0.25 - >>> probability_of_n_heads_in_m_tossing(2,3) - 0.375 + >>> probability_of_n_heads_in_m_tossing(1,1) + 0.5 + >>> probability_of_n_heads_in_m_tossing(0,2) + 0.25 + >>> probability_of_n_heads_in_m_tossing(2,3) + 0.375 """ if head_count != int(head_count) or toss_count != int(toss_count): raise ValueError("The function only accepts integer values") From ac01ab05c829bcd8777271925dc4f30cfa5db177 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Thu, 13 Jun 2024 10:19:50 +1000 Subject: [PATCH 18/33] Update probability_of_n_heads_in_m_tossing.py --- maths/probability_of_n_heads_in_m_tossing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py index 7f9606d4bdaa..329e721f90f3 100644 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ b/maths/probability_of_n_heads_in_m_tossing.py @@ -1,7 +1,7 @@ import numpy as np -def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> int: +def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> float: """ The probability of getting exactly n heads in m tossing. From e4129b981849d49cce7d2c5266a2cfad88706854 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Fri, 14 Jun 2024 20:36:37 +1000 Subject: [PATCH 19/33] Update and rename probability_of_n_heads_in_m_tossing.py to outcome_of_rolling_n_sided_dice_k_time.py --- .../outcome_of_rolling_n_sided_dice_k_time.py | 126 ++++++++++++++++++ maths/probability_of_n_heads_in_m_tossing.py | 72 ---------- 2 files changed, 126 insertions(+), 72 deletions(-) create mode 100644 maths/outcome_of_rolling_n_sided_dice_k_time.py delete mode 100644 maths/probability_of_n_heads_in_m_tossing.py diff --git a/maths/outcome_of_rolling_n_sided_dice_k_time.py b/maths/outcome_of_rolling_n_sided_dice_k_time.py new file mode 100644 index 000000000000..1305d62daf64 --- /dev/null +++ b/maths/outcome_of_rolling_n_sided_dice_k_time.py @@ -0,0 +1,126 @@ +import numpy as np + +def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: + """ + Outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for each index value. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the index + position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + [range(2, 9), + array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,2) + [range(2, 13), + array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, + 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, + 0.02777778])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,3) + [range(3, 19), + array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, + 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, + 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, + 0.00462963])] + """ + + if n_side != int(n_side) or k_time != int(k_time): + raise ValueError("The function only accepts integer values") + if n_side < 2: + raise ValueError("Side count should be more than 1") + if k_time < 1: + raise ValueError("Roll count should be more than 0") + if k_time > 100 or n_side > 100: + raise ValueError("Limited to 100 sides or rolling to avoid memory issues") + + probability_distribution = 1 + distribution_step = np.ones(n_side)/n_side + + iter1 = 0 + while iter1 < k_time: + probability_distribution =np.convolve(probability_distribution, distribution_step) + iter1 = iter1+1 + + output = [] + index = range(k_time,k_time*n_side+1) + output.append(index) + output.append(probability_distribution) + return output + + +''' +# Extra code for the verification + +index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) + +print("Indexes:",index_dist[0]) +print("Distribution:",index_dist[1], "Their summation:",np.sum(index_dist[1])) + +import matplotlib.pyplot as plt +plt.bar(index_dist[0], index_dist[1]) + +''' diff --git a/maths/probability_of_n_heads_in_m_tossing.py b/maths/probability_of_n_heads_in_m_tossing.py deleted file mode 100644 index 329e721f90f3..000000000000 --- a/maths/probability_of_n_heads_in_m_tossing.py +++ /dev/null @@ -1,72 +0,0 @@ -import numpy as np - - -def probability_of_n_heads_in_m_tossing(head_count: int, toss_count: int) -> float: - """ - The probability of getting exactly n heads in m tossing. - - Algorithm Explanation: - If you toss 0 time -> 0 head - Distribution [1] -> Meaning: 1 in the 0-index - - If you toss 1 time -> 0 or 1 head - Distribution [0.5 0.5] -> Meaning: 0.5 in both indexes - - If you toss 2 times -> 0 to 2 heads - Distribution [0.25 0.5 0.25] -> - Meaning: probability of n heads from the distribution - {HH, HT, TH, TT} - - If you toss 3 times -> 0 to 3 heads - Distribution [0.125 0.375 0.375 0.125] -> - Meaning: probability of n heads from the distribution - {HHH, HHT, HTH, HTT, THH, THT, TTH, TTT} - - Therefore, - Probability_distribution(N+1) = - [Probability_distribution(N) 0]/2 + [0 Probability_distribution(N)]/2 - - I used that method in my paper - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - - >>> import numpy as np - >>> probability_of_n_heads_in_m_tossing(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> probability_of_n_heads_in_m_tossing(-1,5) - Traceback (most recent call last): - ... - ValueError: The function is not defined for negative values - >>> probability_of_n_heads_in_m_tossing(3,2) - Traceback (most recent call last): - ... - ValueError: Head count should be smaller than toss count - - >>> probability_of_n_heads_in_m_tossing(1,1) - 0.5 - >>> probability_of_n_heads_in_m_tossing(0,2) - 0.25 - >>> probability_of_n_heads_in_m_tossing(2,3) - 0.375 - """ - if head_count != int(head_count) or toss_count != int(toss_count): - raise ValueError("The function only accepts integer values") - if head_count < 0 or toss_count < 0: - raise ValueError("The function only accepts positive values") - if head_count > toss_count: - raise ValueError("Head count should be smaller than toss count") - if toss_count > 100: - raise ValueError("Limited to 100 tossing to avoid memory issues") - - value = np.ones(1) - - iter1 = 0 - while iter1 < toss_count: - value = np.append(value, [0], axis=0) + np.append([0], value, axis=0) - value = value / 2 - iter1 = iter1 + 1 - - return value[head_count] From 00e23894f6466f8f7cc1bc8a37e84bba005ef068 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Fri, 14 Jun 2024 20:45:14 +1000 Subject: [PATCH 20/33] Update outcome_of_rolling_n_sided_dice_k_time.py --- maths/outcome_of_rolling_n_sided_dice_k_time.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/maths/outcome_of_rolling_n_sided_dice_k_time.py b/maths/outcome_of_rolling_n_sided_dice_k_time.py index 1305d62daf64..241fe3bb3ff6 100644 --- a/maths/outcome_of_rolling_n_sided_dice_k_time.py +++ b/maths/outcome_of_rolling_n_sided_dice_k_time.py @@ -7,7 +7,8 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: This function returns a list. The first element is an array. That array contains indexes. The second element is another array. - That array contains probabilities for each index value. + That array contains probabilities for getting each index values + as the sum of obtained side values. Algorithm Explanation: @@ -47,8 +48,6 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: with the sum. The same sum can come in many combinations. Finding all of those combinations and applying Bernoulli is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. I used that method in my paper to draw the distribution Titled: Uncertainty-aware Decisions in Cloud Computing: @@ -75,12 +74,12 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: >>> outcome_of_rolling_n_sided_dice_k_time(4,2) [range(2, 9), array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,2) + >>> probability_of_n_heads_in_m_tossing(6,2) [range(2, 13), array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, 0.02777778])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,3) + >>> probability_of_n_heads_in_m_tossing(2,3) [range(3, 19), array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, From ce9e8358ef2246cc9663b858a4cb7962b15ecfb7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 10:47:53 +0000 Subject: [PATCH 21/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- .../outcome_of_rolling_n_sided_dice_k_time.py | 191 +++++++++--------- 1 file changed, 97 insertions(+), 94 deletions(-) diff --git a/maths/outcome_of_rolling_n_sided_dice_k_time.py b/maths/outcome_of_rolling_n_sided_dice_k_time.py index 241fe3bb3ff6..51b8639b87b5 100644 --- a/maths/outcome_of_rolling_n_sided_dice_k_time.py +++ b/maths/outcome_of_rolling_n_sided_dice_k_time.py @@ -1,117 +1,120 @@ import numpy as np + def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ - Outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the index - position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - [range(2, 9), - array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] - >>> probability_of_n_heads_in_m_tossing(6,2) - [range(2, 13), - array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, - 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, - 0.02777778])] - >>> probability_of_n_heads_in_m_tossing(2,3) - [range(3, 19), - array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, - 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, - 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, - 0.00462963])] + Outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the index + position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + [range(2, 9), + array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] + >>> probability_of_n_heads_in_m_tossing(6,2) + [range(2, 13), + array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, + 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, + 0.02777778])] + >>> probability_of_n_heads_in_m_tossing(2,3) + [range(3, 19), + array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, + 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, + 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, + 0.00462963])] """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side)/n_side - + distribution_step = np.ones(n_side) / n_side + iter1 = 0 while iter1 < k_time: - probability_distribution =np.convolve(probability_distribution, distribution_step) - iter1 = iter1+1 - + probability_distribution = np.convolve( + probability_distribution, distribution_step + ) + iter1 = iter1 + 1 + output = [] - index = range(k_time,k_time*n_side+1) + index = range(k_time, k_time * n_side + 1) output.append(index) output.append(probability_distribution) return output -''' +""" # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -122,4 +125,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -''' +""" From c7d9ea43d65400634d24316100f5df42a3582f41 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Fri, 14 Jun 2024 20:52:19 +1000 Subject: [PATCH 22/33] Update and rename outcome_of_rolling_n_sided_dice_k_time.py to sum_of_outcomes_for_rolling_n_sided_dice_k_time.py --- ...utcomes_for_rolling_n_sided_dice_k_time.py} | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) rename maths/{outcome_of_rolling_n_sided_dice_k_time.py => sum_of_outcomes_for_rolling_n_sided_dice_k_time.py} (95%) diff --git a/maths/outcome_of_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py similarity index 95% rename from maths/outcome_of_rolling_n_sided_dice_k_time.py rename to maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 51b8639b87b5..d400e472069c 100644 --- a/maths/outcome_of_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -3,8 +3,7 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ - Outcomes for rolling an N-sided dice K times. - + Sum of outcomes for rolling an N-sided dice K times. This function returns a list. The first element is an array. That array contains indexes. The second element is another array. @@ -12,7 +11,6 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: as the sum of obtained side values. Algorithm Explanation: - 1. Explanation of range: When we are rolling a six-sided dice the range becomes 1 to 6. @@ -37,11 +35,11 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: 3 for two outcomes (1,2) and (2,1). Link: - https://www.thoughtco.com/probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the index - position is different. Therefore, we adjust the index. + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. NB: a) We are assuming a fair dice b) Bernoulli's theory works with getting the probability of @@ -57,7 +55,6 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: link: https://dl.acm.org/doi/abs/10.1145/3447583 The PDF version of the paper is available on Google Scholar. - >>> import numpy as np >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) Traceback (most recent call last): @@ -71,7 +68,6 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: Traceback (most recent call last): ... ValueError: Roll count should be more than 0 - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) [range(2, 9), array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] @@ -116,13 +112,9 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ # Extra code for the verification - index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) - print("Indexes:",index_dist[0]) print("Distribution:",index_dist[1], "Their summation:",np.sum(index_dist[1])) - import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) - """ From d8f511c688a07c5a948234f05d24ba224c0078a5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 10:55:06 +0000 Subject: [PATCH 23/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index d400e472069c..bc783d79cf58 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -38,7 +38,7 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: https://www.thoughtco.com/ probabilities-of-rolling-two-dice-3126559 - That phenomenon is the same as the convolution. However, the + That phenomenon is the same as the convolution. However, the index position is different. Therefore, we adjust the index. NB: a) We are assuming a fair dice From fcf30bca5c3dad1e95e8784018211b40b8202a54 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Fri, 14 Jun 2024 21:23:37 +1000 Subject: [PATCH 24/33] Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 192 +++++++++--------- 1 file changed, 99 insertions(+), 93 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index bc783d79cf58..ebf7b9bbf8fc 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,120 +1,126 @@ import numpy as np - def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ - Sum of outcomes for rolling an N-sided dice K times. - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/ - probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the - index position is different. Therefore, we adjust the index. - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - [range(2, 9), - array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] - >>> probability_of_n_heads_in_m_tossing(6,2) - [range(2, 13), - array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, - 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, - 0.02777778])] - >>> probability_of_n_heads_in_m_tossing(2,3) - [range(3, 19), - array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, - 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, - 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, - 0.00462963])] + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the index + position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + [range(2, 9), array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,2) + [range(2, 13), + array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, + 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, + 0.02777778])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,3) + [range(3, 19), + array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, + 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, + 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, + 0.00462963])] """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side) / n_side - + distribution_step = np.ones(n_side)/n_side + iter1 = 0 while iter1 < k_time: - probability_distribution = np.convolve( - probability_distribution, distribution_step - ) - iter1 = iter1 + 1 - + probability_distribution =np.convolve(probability_distribution, distribution_step) + iter1 = iter1+1 + output = [] - index = range(k_time, k_time * n_side + 1) + index = range(k_time,k_time*n_side+1) output.append(index) output.append(probability_distribution) return output -""" +''' # Extra code for the verification + index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) + print("Indexes:",index_dist[0]) print("Distribution:",index_dist[1], "Their summation:",np.sum(index_dist[1])) + import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -""" + +''' From 360ef80ac20a4510bc4a7d520234879de713fecb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 11:25:06 +0000 Subject: [PATCH 25/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 193 +++++++++--------- 1 file changed, 98 insertions(+), 95 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index ebf7b9bbf8fc..67430b1d0237 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,118 +1,121 @@ import numpy as np + def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the index - position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - [range(2, 9), array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,2) - [range(2, 13), - array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, - 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, - 0.02777778])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,3) - [range(3, 19), - array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, - 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, - 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, - 0.00462963])] + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the index + position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + [range(2, 9), array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,2) + [range(2, 13), + array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, + 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, + 0.02777778])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,3) + [range(3, 19), + array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, + 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, + 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, + 0.00462963])] """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side)/n_side - + distribution_step = np.ones(n_side) / n_side + iter1 = 0 while iter1 < k_time: - probability_distribution =np.convolve(probability_distribution, distribution_step) - iter1 = iter1+1 - + probability_distribution = np.convolve( + probability_distribution, distribution_step + ) + iter1 = iter1 + 1 + output = [] - index = range(k_time,k_time*n_side+1) + index = range(k_time, k_time * n_side + 1) output.append(index) output.append(probability_distribution) return output -''' +""" # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -123,4 +126,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -''' +""" From 749499a8c4a47bd6cc65db6ce4d6ed92ba438ffb Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Fri, 14 Jun 2024 21:41:42 +1000 Subject: [PATCH 26/33] Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 195 +++++++++--------- 1 file changed, 97 insertions(+), 98 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 67430b1d0237..05d478eaae1f 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,121 +1,120 @@ import numpy as np - def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the index - position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - [range(2, 9), array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,2) - [range(2, 13), - array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, - 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, - 0.02777778])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,3) - [range(3, 19), - array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, - 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, - 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, - 0.00462963])] + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + [[2, 3, 4, 5, 6, 7, 8], + array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,2) + [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], + array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, + 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, + 0.02777778])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,3) + [[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18], + array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, + 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, + 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, + 0.00462963])] """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side) / n_side - + distribution_step = np.ones(n_side)/n_side + iter1 = 0 while iter1 < k_time: - probability_distribution = np.convolve( - probability_distribution, distribution_step - ) - iter1 = iter1 + 1 - + probability_distribution =np.convolve(probability_distribution, distribution_step) + iter1 = iter1+1 + output = [] - index = range(k_time, k_time * n_side + 1) + index = list(range(k_time,k_time*n_side+1)) output.append(index) output.append(probability_distribution) return output -""" +''' # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -126,4 +125,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -""" +''' From 7bea0d9b17098c1c8c2199b7e8ab274bc4e76799 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 11:43:34 +0000 Subject: [PATCH 27/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 197 +++++++++--------- 1 file changed, 100 insertions(+), 97 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 05d478eaae1f..7bd30db07d07 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,120 +1,123 @@ import numpy as np + def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/ - probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the - index position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - [[2, 3, 4, 5, 6, 7, 8], - array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,2) - [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], - array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, - 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, - 0.02777778])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,3) - [[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18], - array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, - 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, - 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, - 0.00462963])] + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + [[2, 3, 4, 5, 6, 7, 8], + array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,2) + [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], + array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, + 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, + 0.02777778])] + >>> outcome_of_rolling_n_sided_dice_k_time(6,3) + [[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18], + array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, + 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, + 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, + 0.00462963])] """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side)/n_side - + distribution_step = np.ones(n_side) / n_side + iter1 = 0 while iter1 < k_time: - probability_distribution =np.convolve(probability_distribution, distribution_step) - iter1 = iter1+1 - + probability_distribution = np.convolve( + probability_distribution, distribution_step + ) + iter1 = iter1 + 1 + output = [] - index = list(range(k_time,k_time*n_side+1)) + index = list(range(k_time, k_time * n_side + 1)) output.append(index) output.append(probability_distribution) return output -''' +""" # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -125,4 +128,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -''' +""" From 164a51dd42a1ab4812cdb330f229e9e3d20bee08 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Fri, 14 Jun 2024 21:59:51 +1000 Subject: [PATCH 28/33] Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 195 ++++++++---------- 1 file changed, 90 insertions(+), 105 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 7bd30db07d07..4b43eaeca967 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,123 +1,108 @@ import numpy as np - -def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: +def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int): """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/ - probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the - index position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - [[2, 3, 4, 5, 6, 7, 8], - array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,2) - [[2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], - array([0.02777778, 0.05555556, 0.08333333, 0.11111111, 0.13888889, - 0.16666667, 0.13888889, 0.11111111, 0.08333333, 0.05555556, - 0.02777778])] - >>> outcome_of_rolling_n_sided_dice_k_time(6,3) - [[3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18], - array([0.00462963, 0.01388889, 0.02777778, 0.0462963 , 0.06944444, - 0.09722222, 0.11574074, 0.125 , 0.125 , 0.11574074, - 0.09722222, 0.06944444, 0.0462963 , 0.02777778, 0.01388889, - 0.00462963])] + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(2,2) + ([2, 3, 4], array([0.25, 0.5 , 0.25])) + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + ([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side) / n_side - + distribution_step = np.ones(n_side)/n_side + iter1 = 0 while iter1 < k_time: - probability_distribution = np.convolve( - probability_distribution, distribution_step - ) - iter1 = iter1 + 1 - - output = [] - index = list(range(k_time, k_time * n_side + 1)) - output.append(index) - output.append(probability_distribution) - return output + probability_distribution =np.convolve(probability_distribution, distribution_step) + iter1 = iter1+1 + + index = list(range(k_time,k_time*n_side+1)) + return index, probability_distribution -""" +''' # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -128,4 +113,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -""" +''' From b8cb3bc06e720b4440ce075a9cc0445bec37042d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:00:52 +0000 Subject: [PATCH 29/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 179 +++++++++--------- 1 file changed, 91 insertions(+), 88 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 4b43eaeca967..4632fe7d28d4 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,108 +1,111 @@ import numpy as np + def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int): """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/ - probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the - index position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(2,2) - ([2, 3, 4], array([0.25, 0.5 , 0.25])) - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - ([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(2,2) + ([2, 3, 4], array([0.25, 0.5 , 0.25])) + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + ([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side)/n_side - + distribution_step = np.ones(n_side) / n_side + iter1 = 0 while iter1 < k_time: - probability_distribution =np.convolve(probability_distribution, distribution_step) - iter1 = iter1+1 - - index = list(range(k_time,k_time*n_side+1)) + probability_distribution = np.convolve( + probability_distribution, distribution_step + ) + iter1 = iter1 + 1 + + index = list(range(k_time, k_time * n_side + 1)) return index, probability_distribution -''' +""" # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -113,4 +116,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int): import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -''' +""" From ccf5053f8dc6c8f0bf3760f1e5cf79e6fe27a0d9 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Fri, 14 Jun 2024 22:02:58 +1000 Subject: [PATCH 30/33] Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 181 +++++++++--------- 1 file changed, 89 insertions(+), 92 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 4632fe7d28d4..6f64b8252dc1 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,111 +1,108 @@ import numpy as np - -def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int): +def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list | list: """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/ - probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the - index position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(2,2) - ([2, 3, 4], array([0.25, 0.5 , 0.25])) - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - ([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(2,2) + ([2, 3, 4], array([0.25, 0.5 , 0.25])) + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + ([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side) / n_side - + distribution_step = np.ones(n_side)/n_side + iter1 = 0 while iter1 < k_time: - probability_distribution = np.convolve( - probability_distribution, distribution_step - ) - iter1 = iter1 + 1 - - index = list(range(k_time, k_time * n_side + 1)) + probability_distribution =np.convolve(probability_distribution, distribution_step) + iter1 = iter1+1 + + index = list(range(k_time,k_time*n_side+1)) return index, probability_distribution -""" +''' # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -116,4 +113,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int): import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -""" +''' From 5d232144027cc27a4494f051ccd5ff9d1e07ef03 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:05:16 +0000 Subject: [PATCH 31/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 179 +++++++++--------- 1 file changed, 91 insertions(+), 88 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 6f64b8252dc1..2a45d42443a3 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,108 +1,111 @@ import numpy as np + def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list | list: """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/ - probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the - index position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(2,2) - ([2, 3, 4], array([0.25, 0.5 , 0.25])) - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - ([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(2,2) + ([2, 3, 4], array([0.25, 0.5 , 0.25])) + + >>> outcome_of_rolling_n_sided_dice_k_time(4,2) + ([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side)/n_side - + distribution_step = np.ones(n_side) / n_side + iter1 = 0 while iter1 < k_time: - probability_distribution =np.convolve(probability_distribution, distribution_step) - iter1 = iter1+1 - - index = list(range(k_time,k_time*n_side+1)) + probability_distribution = np.convolve( + probability_distribution, distribution_step + ) + iter1 = iter1 + 1 + + index = list(range(k_time, k_time * n_side + 1)) return index, probability_distribution -''' +""" # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -113,4 +116,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list | l import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -''' +""" From 592c68595e8baf1d504ad911c99d88029c817a46 Mon Sep 17 00:00:00 2001 From: H M Dipu Kabir Date: Fri, 14 Jun 2024 22:18:22 +1000 Subject: [PATCH 32/33] Update sum_of_outcomes_for_rolling_n_sided_dice_k_time.py --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 179 +++++++++--------- 1 file changed, 87 insertions(+), 92 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 2a45d42443a3..493215fbc47f 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,111 +1,106 @@ import numpy as np - -def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list | list: +def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/ - probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the - index position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(2,2) - ([2, 3, 4], array([0.25, 0.5 , 0.25])) - - >>> outcome_of_rolling_n_sided_dice_k_time(4,2) - ([2, 3, 4, 5, 6, 7, 8], array([0.0625, 0.125 , 0.1875, 0.25 , 0.1875, 0.125 , 0.0625])) + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(2,2) + ([2, 3, 4], array([0.25, 0.5 , 0.25])) + """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side) / n_side - + distribution_step = np.ones(n_side)/n_side + iter1 = 0 while iter1 < k_time: - probability_distribution = np.convolve( - probability_distribution, distribution_step - ) - iter1 = iter1 + 1 - - index = list(range(k_time, k_time * n_side + 1)) + probability_distribution =np.convolve(probability_distribution, distribution_step) + iter1 = iter1+1 + + index = list(range(k_time,k_time*n_side+1)) return index, probability_distribution -""" +''' # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -116,4 +111,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list | l import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -""" +''' From ea842d62b06fbab648b4b4b829be70336c1a9103 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Jun 2024 12:19:38 +0000 Subject: [PATCH 33/33] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- ...utcomes_for_rolling_n_sided_dice_k_time.py | 175 +++++++++--------- 1 file changed, 89 insertions(+), 86 deletions(-) diff --git a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py index 493215fbc47f..659d90a36197 100644 --- a/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py +++ b/maths/sum_of_outcomes_for_rolling_n_sided_dice_k_time.py @@ -1,106 +1,109 @@ import numpy as np + def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: """ - Sum of outcomes for rolling an N-sided dice K times. - - This function returns a list. The first element is an array. - That array contains indexes. - The second element is another array. - That array contains probabilities for getting each index values - as the sum of obtained side values. - - Algorithm Explanation: - - 1. Explanation of range: - When we are rolling a six-sided dice the range becomes - 1 to 6. - While rolling 5 times range becomes 2 to 12. - The sum outcomes becomes 5 when all rolling finds 1. - 30 happens when all rolling finds 6. - 1 is the minimum and 6 is the maximum of side values - for a 6 sided dice. Therefore, the range is 5 to 30. - Therefore, the range is k to n*k. - - 2. Explanation of probability distribution: - Say we are rolling a six-sided dice 2 times. - for 0 roll, the outcome is 0 with probability 1. - For the first roll, the outcome is 1 to 6 equally distributed. - - For the second roll, each previous outcome (1-6) will face - an addition from the second rolling (1-6). - If the first outcome is (known) 3, then the probability of - getting each of 4 to 9 will be 1/6. - - The sum becomes 2 for two 1 outcomes. But the sum becomes - 3 for two outcomes (1,2) and (2,1). - - Link: - https://www.thoughtco.com/ - probabilities-of-rolling-two-dice-3126559 - - That phenomenon is the same as the convolution. However, the - index position is different. Therefore, we adjust the index. - - - NB: a) We are assuming a fair dice - b) Bernoulli's theory works with getting the probability of - exactly 3 sixes while rolling 5 times. It does not work directly - with the sum. The same sum can come in many combinations. - Finding all of those combinations and applying Bernoulli - is more computationally extensive. - c) The algorithm can be used in playing games where the sum of - multiple dice throwing is needed. - - I used that method in my paper to draw the distribution - Titled: Uncertainty-aware Decisions in Cloud Computing: - Foundations and Future Directions - Journal: ACM Computing Surveys (CSUR) - link: https://dl.acm.org/doi/abs/10.1145/3447583 - The PDF version of the paper is available on Google Scholar. - - - >>> import numpy as np - >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) - Traceback (most recent call last): - ... - ValueError: The function only accepts integer values - >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) - Traceback (most recent call last): - ... - ValueError: Side count should be more than 1 - >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) - Traceback (most recent call last): - ... - ValueError: Roll count should be more than 0 - - >>> outcome_of_rolling_n_sided_dice_k_time(2,2) - ([2, 3, 4], array([0.25, 0.5 , 0.25])) - + Sum of outcomes for rolling an N-sided dice K times. + + This function returns a list. The first element is an array. + That array contains indexes. + The second element is another array. + That array contains probabilities for getting each index values + as the sum of obtained side values. + + Algorithm Explanation: + + 1. Explanation of range: + When we are rolling a six-sided dice the range becomes + 1 to 6. + While rolling 5 times range becomes 2 to 12. + The sum outcomes becomes 5 when all rolling finds 1. + 30 happens when all rolling finds 6. + 1 is the minimum and 6 is the maximum of side values + for a 6 sided dice. Therefore, the range is 5 to 30. + Therefore, the range is k to n*k. + + 2. Explanation of probability distribution: + Say we are rolling a six-sided dice 2 times. + for 0 roll, the outcome is 0 with probability 1. + For the first roll, the outcome is 1 to 6 equally distributed. + + For the second roll, each previous outcome (1-6) will face + an addition from the second rolling (1-6). + If the first outcome is (known) 3, then the probability of + getting each of 4 to 9 will be 1/6. + + The sum becomes 2 for two 1 outcomes. But the sum becomes + 3 for two outcomes (1,2) and (2,1). + + Link: + https://www.thoughtco.com/ + probabilities-of-rolling-two-dice-3126559 + + That phenomenon is the same as the convolution. However, the + index position is different. Therefore, we adjust the index. + + + NB: a) We are assuming a fair dice + b) Bernoulli's theory works with getting the probability of + exactly 3 sixes while rolling 5 times. It does not work directly + with the sum. The same sum can come in many combinations. + Finding all of those combinations and applying Bernoulli + is more computationally extensive. + c) The algorithm can be used in playing games where the sum of + multiple dice throwing is needed. + + I used that method in my paper to draw the distribution + Titled: Uncertainty-aware Decisions in Cloud Computing: + Foundations and Future Directions + Journal: ACM Computing Surveys (CSUR) + link: https://dl.acm.org/doi/abs/10.1145/3447583 + The PDF version of the paper is available on Google Scholar. + + + >>> import numpy as np + >>> outcome_of_rolling_n_sided_dice_k_time(.2,.5) + Traceback (most recent call last): + ... + ValueError: The function only accepts integer values + >>> outcome_of_rolling_n_sided_dice_k_time(-1,5) + Traceback (most recent call last): + ... + ValueError: Side count should be more than 1 + >>> outcome_of_rolling_n_sided_dice_k_time(3,-2) + Traceback (most recent call last): + ... + ValueError: Roll count should be more than 0 + + >>> outcome_of_rolling_n_sided_dice_k_time(2,2) + ([2, 3, 4], array([0.25, 0.5 , 0.25])) + """ - + if n_side != int(n_side) or k_time != int(k_time): raise ValueError("The function only accepts integer values") if n_side < 2: raise ValueError("Side count should be more than 1") - if k_time < 1: + if k_time < 1: raise ValueError("Roll count should be more than 0") if k_time > 100 or n_side > 100: raise ValueError("Limited to 100 sides or rolling to avoid memory issues") - + probability_distribution = 1 - distribution_step = np.ones(n_side)/n_side - + distribution_step = np.ones(n_side) / n_side + iter1 = 0 while iter1 < k_time: - probability_distribution =np.convolve(probability_distribution, distribution_step) - iter1 = iter1+1 - - index = list(range(k_time,k_time*n_side+1)) + probability_distribution = np.convolve( + probability_distribution, distribution_step + ) + iter1 = iter1 + 1 + + index = list(range(k_time, k_time * n_side + 1)) return index, probability_distribution -''' +""" # Extra code for the verification index_dist = outcome_of_rolling_n_sided_dice_k_time(6, 5) @@ -111,4 +114,4 @@ def outcome_of_rolling_n_sided_dice_k_time(n_side: int, k_time: int) -> list: import matplotlib.pyplot as plt plt.bar(index_dist[0], index_dist[1]) -''' +"""