<jupyter_start><jupyter_text>Probabilities with Events and CodeFrom the videos, we see that an event is a subset of the sample space $Ω$. If you remember our table representation for a probability space, then an event could be thought of as a subset of the rows, and the probability of the event is just the sum of the probability values in those rows!The probability of an event $A⊆Ω$ is the sum of the probabilities of the possible outcomes in $A$:$$\mathbb {P}(\mathcal{A})\triangleq \sum _{\omega \in \mathcal{A}}\mathbb {P}(\text {outcome }\omega ),$$ where “$≜$" means “defined as".We can translate the above equation into Python code. In particular, we can compute the probability of an event encoded as a Python set event, where the probability space is encoded as a Python dictionary prob_space:```pythondef prob_of_event(event, prob_space):    total = 0    for outcome in event:        total += prob_space[outcome]    return total```Here's an example of how to use the above function:```pythonprob_space = {'sunny': 1/2, 'rainy': 1/6, 'snowy': 1/3}rainy_or_snowy_event = {'rainy', 'snowy'}print(prob_of_event(rainy_or_snowy_event, prob_space))```<jupyter_code>def prob_of_event(event, prob_space):
    """
    Gives the probability of event for a given sample space.
    
    >>> prob_of_event({'rainy', 'snowy'}, {'sunny': 1/2, 'rainy': 1/6, 'snowy': 1/3})
    0.5
    >>> prob_of_event({'benign', 'malignant'}, {'benign': 0.3, 'malignant': 0.5, 'not sure': 0.2})
    0.8
    """
    total = 0
    for outcome in event:
        total += prob_space[outcome]
    return total

if __name__ == "__main__":
    import doctest
    doctest.testmod()
prob_space = {'sunny': 1/2, 'rainy': 1/6, 'snowy': 1/3}
rainy_or_snowy_event = {'rainy', 'snowy'}
print(prob_of_event(rainy_or_snowy_event, prob_space))
model = {'benign': 0.3, 'malignant': 0.5, 'not sure': 0.2}
event = {'benign', 'malignant'}
prob_of_event(event, model)<jupyter_output><empty_output><jupyter_text>Exercise: Some Dice Rolling and Coin Flipping Events Consider rolling two six-sided dice with faces numbered $1$ through $6$. Again, we use the sample space from earlier $Ω=\{(1,1),(1,2),…,(6,5),(6,6)\}$. What is the event that the sum of the faces is $7$? Enter your answer as a Python set.<jupyter_code>two_dice = {}
for i in range(6):
    for j in range(6):
        two_dice[(i+1, j+1)] = 1/36
Ω = set(two_dice.keys())
E = {x for x in Ω if sum(x) == 7}

E<jupyter_output><empty_output><jupyter_text>We flip a coin. The coin landing with any face shown means that it's okay if either heads shows or tails shows. This corresponds to the event $\mathcal{A}_{\text {any-face-shows}}=\{ \text {heads},\text {tails}\}$, which happens to be equal to the whole sample space. Meanwhile, simultaneously having both heads and tails show face up refers to an impossible situation, corresponding to the “empty set" event $\mathcal{A}_{\text {both-faces-simultaneously-show}}=\{ \}$, also denoted as $\phi$.<jupyter_code>coin = {'heads': 1/2, 'tails': 1/2}
Ω = set(coin.keys())
E1 = set(['heads', 'tails'])
E2 = set()
print(prob_of_event(E1, coin), prob_of_event(E2, coin))<jupyter_output>1.0 0
{'fraction_non_alphanumeric': 0.14022842639593908, 'fraction_numerical': 0.020939086294416244, 'mean_word_length': 4.610320284697509, 'pattern_counts': {'":': 1, '<': 12, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '161432', 'license': 'MIT', 'n_tokens_mistral': 1108, 'n_tokens_neox': 1054, 'n_words': 380, 'path': 'week01/03 Probabilities with Events and Code.ipynb', 'repo_name': 'infimath/Computational-Probability-and-Inference', 'size': '3152'}