--> Skip to main content
Please leave a comment below if you have any questions or suggestions!

Stats 2 Extra activity 2

Google Colab Notebook Activity

📚

Extra activity 2

Release Date : 4 October 2024
Submission due date : 16 October 2024
Peer Review Due Date : 20 October 2024

  1. Copy the code in Google Colab:
    • Open the provided Google Colab notebook available in the supplementary content. Make a copy of it and save it to your Google Drive. Before running any code, press Ctrl+F9 to run all cells. Once this is done, you can start experimenting with different values as demonstrated by Andrew Sir in Week 0 Part 2. If you run the code directly without this step, it may show errors.

  2. Select an Experiment and Event:
    • Choose an experiment and event from the examples discussed in your lectures or pick any other problem that interests you. This could be anything from a simple probability problem to a more complex statistical experiment.
  3. Add the Experiment and Event to the Notebook:
    • Incorporate your chosen experiment and event into the Google Colab notebook. Ensure you clearly define the parameters and the setup for your experiment.
  4. Perform a Monte Carlo Simulation:
    • Use Monte Carlo simulation techniques to verify the computed probability of your experiment. This involves running a large number of simulations to approximate the probability of the event.
    • Write the code to perform these simulations in the notebook. Make sure to document your steps and explain your code for clarity.
  5. Share the Notebook:
    • Once your notebook is ready, set the sharing permissions to “view” for everyone in the Onlinedegree domain. This ensures that others can access and review your work.
    • Add the notebook to the designated site under the heading ‘Activity 2’.
  6. Take a Screenshot:
    • Finally, take a screenshot of the website with the embedded Colab Notebook. This serves as proof of completion and can be submitted as required.

HTML and Python Code Snippets

HTML Code Snippet


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Coin Toss Probability Activity</title>
    <script src="https://polyfill.io/v3/polyfill.min.js?features=es6"></script>
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js"></script>
    <style>
        body {
            color: white;
        }
    </style>
</head>
<body>
    <h1>Coin Toss Probability Activity</h1>
    <p>For this activity, I conducted a series of experiments to determine the probability of getting a specific number of heads in a series of coin tosses. Specifically, I focused on three scenarios: tossing a coin 10 times, 50 times, and 100 times. For each scenario, I calculated the probability of getting exactly 5 heads in 10 tosses, 25 heads in 50 tosses, and 50 heads in 100 tosses.</p>
    
    <h2>Generate Sequences</h2>
    <p>First, I generated sequences of outcomes for each number of tosses using Python. This involved simulating the coin tosses and recording the number of heads obtained in each experiment. I printed all possible outcomes to provide a comprehensive view of the results.</p>
    
    <h2>Calculate Theoretical Probability</h2>
    <p>Use the binomial distribution formula to calculate the theoretical probability of getting the specified number of heads:</p>
    <pre><code>
    <p>\[ P(X = k) = \binom{n}{k} \left( \frac{1}{2} \right)^k \left( \frac{1}{2} \right)^{n-k} \]</p>
    <p>For 10 tosses, \( k = 5 \), the theoretical probability is:</p>
    <p>\[ P(X = 5) = \binom{10}{5} \left( \frac{1}{2} \right)^5 \left( \frac{1}{2} \right)^5 = \binom{10}{5} \left( \frac{1}{2} \right)^{10} \approx 0.2461 \]</p>
    <p>For 50 tosses, \( k = 25 \), the theoretical probability is:</p>
    <p>\[ P(X = 25) = \binom{50}{25} \left( \frac{1}{2} \right)^{25} \left( \frac{1}{2} \right)^{25} = \binom{50}{25} \left( \frac{1}{2} \right)^{50} \approx 0.1123 \]</p>
    <p>For 100 tosses, \( k = 50 \), the theoretical probability is:</p>
    <p>\[ P(X = 50) = \binom{100}{50} \left( \frac{1}{2} \right)^{50} \left( \frac{1}{2} \right)^{50} = \binom{100}{50} \left( \frac{1}{2} \right)^{100} \approx 0.0796 \]</p>
    </code></pre>
</body>
</html>
        

Python Code Snippet

Python code to generate sequence in form 1 and 2 where 1 is head and 2 is tail


import numpy as np

# Function to generate sequences of 1s and 2s
def generate_sequences(n_tosses, n_experiments):
    sequences = []
    for _ in range(n_experiments):
        sequence = np.random.choice([1, 2], size=n_tosses)
        sequences.append(sequence)
    return sequences

# Parameters
n_experiments = 1

# Generate sequences for 10, 50, and 100 tosses
tosses_list = [10, 50, 100]
sequences_dict = {}

for n_tosses in tosses_list:
    sequences = generate_sequences(n_tosses, n_experiments)
       sequences_dict[n_tosses] = sequences

# Display results
for n_tosses, sequences in sequences_dict.items():
    print(f"Results for {n_tosses} tosses:")
    for sequence in sequences:
        print(sequence)
    print("\n")
        

This python code will give you Monte Carlo simulation result


import numpy as np

# Function to simulate coin toss
def coin_toss_simulation(n_tosses, n_experiments):
    results = []
    for _ in range(n_experiments):
        tosses = np.random.choice(['H', 'T'], size=n_tosses)
        heads_count = np.sum(tosses == 'H')
        results.append(heads_count)
    return results

# Function to calculate theoretical probability of exactly k heads
def theoretical_probability(n_tosses, k):
    from math import comb
    probability = comb(n_tosses, k) * (0.5 ** k) * (0.5 ** (n_tosses - k))
    return probability

# Function to calculate probability from simulation results
def calculate_probability(results, k):
    count_k_heads = results.count(k)
    probability = count_k_heads / len(results)
    return probability

# Parameters
n_experiments = 1000

# Simulate for 10, 50, and 100 tosses
tosses_list = [10, 50, 100]
results_dict = {}

for n_tosses in tosses_list:
    results = coin_toss_simulation(n_tosses, n_experiments)
    results_dict[n_tosses] = results

# Display results and calculate probabilities
for n_tosses, results in results_dict.items():
    print(f"Results for {n_tosses} tosses (first 10 outcomes):")
    print(results[:10])  # Display first 10 outcomes for brevity
    
    if n_tosses == 10:
        k = 5
    elif n_tosses == 50:
        k = 25
    elif n_tosses == 100:
        k = 50
    
    # Theoretical probability
    theoretical_prob = theoretical_probability(n_tosses, k)
    print(f"Theoretical probability of getting exactly {k} heads in {n_tosses} tosses: {theoretical_prob:.4f}")
    
    # Monte Carlo probability
    monte_carlo_prob = calculate_probability(results, k)
    print(f"Monte Carlo probability of getting exactly {k} heads in {n_tosses} tosses: {monte_carlo_prob:.4f}")
    print("\n")
        

These code snippets are provided for practice purposes. Feel free to experiment with different values and run the codes to see the results. Please use different values for your activity.

If you have any issue with code please leave a comment below.

Comments

Popular post

IITM Notes

Honoring Ratan Tata Please take a moment to read this tribute. (Click the box to view the full article) “These handwritten notes encompass topics in data science and civil services. The beauty of knowledge is that you don’t need to belong to any specific group; simply maintain your curiosity, and knowledge will find its way to you. I hope these notes are helpful. If they are, please consider leaving a comment below and follow my blog for updates.” “I’ve started a qualifier series where I’ll summarize maths and stats topics for the qualifier exam and discuss previous year questions (PYQs). Please subscribe to my YouTube channel for updates!” SUBSCRIBE Mathematics 1 👉 Select Week Week 1 Week 2 Week 3 Week 4 Week 5 Week 6 Week 7 Week 8 Week 9

Maths 1 Week 4 Summary

Polynomial Summary 📚 Polynomial Summary Polynomial A polynomial is an expression consisting of variables (also called indeterminates) and coefficients, that involves only the operations of addition, subtraction, multiplication, and non-negative integer exponents of variables. For example, \(3x^2 + 2x - 5\) is a polynomial. Degree of Polynomial The degree of a polynomial is the highest power of the variable in the polynomial. For example, in the polynomial \(4x^3 + 3x^2 + 2x + 1\), the degree is 3. Multiplication of Polynomials To multiply two polynomials, each term in the first polynomial is multiplied by each term in the second polynomial. For example: \((2x + 3)(x - 4) = 2x \cdot x + 2x \cdot (-4) + 3 \cdot x + 3 \cdot (-4) = 2x^2 - 8x + 3x - 12 = 2x^2 - 5x - 12\) Division of Polynomials To divide polynomials, we use polynomial long division or synthetic division. For example, dividing \(2x^3 + 3x^2 - x - 5\) by \(x - 1\)

Previous year question paper

Maths PYQs For qualifier exam preparation, complete all your graded assignments and practice assignments for all four weeks, and thoroughly solve 3-4 previous years’ questions (PYQs). This should be more than enough. Many people find math challenging, so I have provided detailed solutions for four PYQs. These questions cover the entire syllabus and include all types of questions. Try to solve them, as it will be very beneficial. Maths Previous Year Questions PYQ1 PYQ2 PYQ3 PYQ4 Maths PYQ Solutions Maths PYQ Detailed Solutions PYQ1 PYQ2 PYQ3 PYQ4 PYQs Statistics1 PYQ1 PYQ2 PYQ3 PYQ4 CT PYQ1 PYQ2 PYQ3 PYQ4 English PYQ1 PYQ2 PYQ3 PYQ4 For quiz 2 and end term Quiz 2 End t

Maths 1 week 1 Summary

Number System and Set Theory 📚 Set Theory Sets Explained👉 Watch here This week, our teacher covered the basics of the number system. We were instructed to consider 0 as part of the natural numbers, as it will be treated as such in future subjects like Python. However, in exams, it will be explicitly stated whether 0 should be considered a natural number. The key topics from this week include set theory and the relationship between two sets. In set theory, we focused on three Venn diagram problems. In the context of relations, we discussed the concepts of reflexive, symmetric, transitive, and equivalence relations. Detailed Explanation 1.Union of Two Sets The union of two sets A and B is the set of elements that are in either A , B , or both. It is denoted as A ∪ B . 2.Intersection of Two Sets The intersection of two sets A and B is the set of elements that are in both A and B . It is denoted as A ∩ B .

CT Week 4

Summary 📚 Selecting cards: Iterator with filtering This section discusses how to use iterators with filtering to select specific cards from a collection. It involves creating an iterator that can traverse through a collection and apply a filter to select only the desired elements. Examples Total spend of one person This will calculates the total amount spent by a single person. It involves iterating through a list of transactions and summing up the amounts associated with the specified person. Number of verbs This will counts the number of verbs in a given text. It involves iterating through the words in the text and applying a filter to select only the verbs. Sum of both girls’ marks and boys’ marks This will calculates the total marks obtained by girls and boys separately. It involves iterating through a list of students' marks and summing up the marks based on gender. Total revenues of each shop This will calculates the total revenu

Maths 1 week 2 Summary

Coordinate Geometry Formulas 📚 Coordinate Geometry Formulas Distance Formula Between Two Points Explanation: Calculates the distance between two points \((x_1, y_1)\) and \((x_2, y_2)\). Formula: \(d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}\) Section Formula Explanation: Determines the coordinates of a point dividing a line segment internally in the ratio \(m:n\). Formula: \(\left(\frac{mx_2 + nx_1}{m+n}, \frac{my_2 + ny_1}{m+n}\right)\) Area of Triangle When Three Coordinates Given Explanation: Calculates the area of a triangle with vertices at \((x_1, y_1)\), \((x_2, y_2)\), and \((x_3, y_3)\). Formula: \(\text{Area} = \frac{1}{2} \left| x_1(y_2 - y_3) + x_2(y_3 - y_1) + x_3(y_1 - y_2) \right|\) Parallel Lines Explanation: Lines with the same slope. Formula: If \(m_1 = m_2\), the lines ar

Statistics 1 Week 4

Statistical Concepts 📚 Statistical Concepts Summary and Association Between Two Categorical Variables When analyzing the relationship between two categorical variables, we often use a contingency table. This table displays the frequency distribution of the variables, allowing us to observe any potential association between them. Contingency Table A contingency table, also known as a cross-tabulation or crosstab, is a matrix format that displays the frequency distribution of variables. Each cell in the table represents the frequency count of occurrences for a specific combination of the variables. | | Category 1 | Category 2 | Total | |-----------|------------|------------|-------| | Variable A| 10 | 20 | 30 | | Variable B| 15 | 25 | 40 | | Total | 25 | 45 | 70 | Row

Supplementary content

Book Collection Marking pattern in exam: Suppose one question is worth 6 marks and there are three correct options, say B, D, and E. Then each option will be worth 2 marks. If you mark only B and D, you will get 4 marks. If you mark any incorrect option, say B, C, D, you will get 0 marks as C was the wrong answer. So if you are not sure of the option, better don’t mark it, or else you will end up getting 0 marks. 👇 Supplementary Material Provided by Instructor Student book Spending some time reading it will be highly beneficial. Student Book Statistics These two books are written by Prashant sir. These two books are more than enough to cover Statistics 1 week 1 to 12. I have provided a short summary which will help you memorize terms and terminology. If you don't understand, refer to these books. These two books were made available online by IIT Madras, and their dist

Maths 1 Week 3 Summary

📚 Quadratic Functions Summary A quadratic function is a polynomial function of degree 2, generally represented as \( f(x) = ax^2 + bx + c \) , where a, b , and c are constants, and a \neq 0 . Graph of Quadratic Function The graph of a quadratic function is a parabola. It can open either upward or downward depending on the sign of the coefficient a . If a > 0 , the parabola opens upward. If a < 0 , it opens downward. Axis of Symmetry The axis of symmetry of a parabola is a vertical line that divides the parabola into two mirror images. It can be found using the formula \( x = -\frac{b}{2a} \) . Coordinates of the Vertex The vertex of the parabola is the highest or lowest point on the graph, depending on whether it opens downward or upward. The coordinates of the vertex can be found using the formula \( (x, y) = \left(-\frac{b}{2a}, f\left(-\frac{b}{2a}\right)\right) \) . Parabola Opening Upward and Downwa

English Week 1 to 12 summary

Vowels and Consonants 📚 Vowels and Consonants English 1 playlist English 2 playlist English Graded Assignment Vowels Vowels are speech sounds produced without any significant constriction or blockage of airflow in the vocal tract. The primary vowels in English are a, e, i, o, u , and sometimes y . Consonants Consonants are speech sounds produced with some degree of constriction or closure at one or more points along the vocal tract. The consonants in English include b, c, d, f, g, h, j, k, l, m, n, p, q, r, s, t, v, w, x, y, z . Sounding Vowels and Consonants Sounding vowels are produced with an open vocal tract, allowing the air to flow freely. Examples include the sounds in "cat" (/æ/), "see" (/iː/), and "moon" (/uː/). Sounding consonants involve some form of obstruction in the vocal tract. For example, the sound /b/ in "bat" involves closing the lips, w