Randomness
and Simulation (II)
1
Data 6 Summer 2025
LECTURE 29
A quick dive into some statistics (with datascience)
Developed by students and faculty at UC Berkeley and Tuskegee University
Week 6
Announcements!
2
Today’s Roadmap
Lecture 29, Data 6 Summer 2025
3
Randomness
4
1. Randomness Recapped
2. tbl.sample
3. Repeated Simulations
➤
What Is Randomness?
5
np.random
np.random is a submodule of numpy that contains functions involving random numbers and random selection. Here are some useful functions.
6
Function | Behavior |
np.random.randint(start, stop, size) | Generates random integers between start and stop - 1. Each integer in the range is equally likely to be selected. If no size, then returns a single integer. If size (int) is provided, returns an array with size number of elements. |
np.random.choice(arr, size) | Randomly selects elements from the array arr. Each element is equally likely to be selected. If no size, then returns a single element. If size (int) is provided, returns an array with size number of elements. |
np.random.seed(n) | Sets the seed of the current cell. |
tbl.sample
7
1. Randomness
2. tbl.sample
3. Repeated Simulations
➤
tbl.sample Overview
The method in the datascience library for sampling rows from a table. The table must exist before we can sample from it.
8
8
Function | Behavior |
tbl.sample(k=tbl.num_rows, with_replacement=True, weights=None) | A new table where n rows are randomly sampled from the original table; by default, k=tbl.num_rows. Default is with replacement. For sampling without replacement, use argument with_replacement=False. For a non-uniform sample, provide a third argument weights=distribution where distribution is an array or list containing the probability of each row |
tbl.sample() vs. np.random
9
Choosing Our Sample Size
10
With or Without Replacement
11
Sampling Without Replacement Example
12
Face |
1 |
2 |
3 |
4 |
5 |
6 |
Face |
|
|
|
|
|
|
Sampling Without Replacement Example
13
Face |
1 |
|
3 |
4 |
5 |
6 |
Face |
2 |
|
|
|
|
|
Sampling Without Replacement Example
14
Face |
1 |
|
3 |
4 |
|
6 |
Face |
2 |
5 |
|
|
|
|
Sampling Without Replacement Example
15
Face |
1 |
|
|
4 |
|
6 |
Face |
2 |
5 |
3 |
|
|
|
Sampling Without Replacement Example
16
Face |
|
|
|
4 |
|
6 |
Face |
2 |
5 |
3 |
1 |
|
|
Sampling Without Replacement Example
17
Face |
|
|
|
|
|
6 |
Face |
2 |
5 |
3 |
1 |
4 |
|
Sampling Without Replacement Example
18
Face |
|
|
|
|
|
|
Face |
2 |
5 |
3 |
1 |
4 |
6 |
Sampling Without Replacement Example
19
Face |
|
|
|
|
|
|
Face |
2 |
5 |
3 |
1 |
4 |
6 |
Face |
1 |
2 |
3 |
4 |
5 |
6 |
Sampling With Weights
20
Sampling With Weights Example
21
Face |
5 |
5 |
6 |
3 |
6 |
5 |
Face |
|
|
3 |
4 |
5 |
6 |
Questions?
22
Quick Check 1
Assume the table cards contains a standard deck of cards where each row is a card. Fill in the blanks to shuffle the deck of cards.
23
Quick Check
Repetition
24
1. Randomness Recapped
2. tbl.sample
3. Repeated Simulations (with datascience)
➤
Coin Flips
Suppose you flip a fair coin 100 times. How many heads would you expect to see?
25
100x
54 heads
100x
43 heads
100x
51 heads
...
10,000 times
Idea:
In Conclusion…
26
Summary
27
Randomness underpins the field of statistics, which is an integral component of data science.
Recap
Next Time
28
Week 6
Announcements!
29