Want to learn more? Take the full course at at your own pace. More than a video, you’ll learn hands-on coding & quickly apply skills to your daily work.

—

Welcome to Probability Puzzles in R! In this course, you will gain practice with basic probability concepts and learn new techniques as well, by exploring probability puzzles and solving them with R. The puzzles come from a variety of sources, including games with dice, poker games, and classic problems that you would likely encounter in a standard probability course.

The puzzles are organized in this course thematically, with the first chapter containing puzzles that may already be familiar to you. The next chapter utilizes well-known games where dice are rolled. Chapter 3 contains puzzles posed by various educators on Twitter or elsewhere on the internet. Finally, in Chapter 4, we explore some questions in poker, including Texas Hold’em and other scenarios.

Each puzzle has different challenges, and our exercises will ask for a mixture of simulated and exact theoretical solutions. For example, in the Yahtzee exercises, we will use combinatorics to solve for the probabilities of achieving various dice combinations. Conversely, in the birthday problem, we will simulate random birthdays and observe how frequently a match is observed, to obtain an estimate of the true probability of a match among any given number of individuals.

To assist with our tasks, we will use some built-in combinatorics functions. The factorial function calculates the product of a given number and each integer below it, down to 1. The choose function will calculate combinations for us. For example, “choose 5 3”, also pronounced “5 choose 3”, is the number of ways to choose 3 objects out of 5, and follows this formula using factorials.

A simulation is a random generation of a process that mimics the actual situation of interest. For example, we can select an object at random using the sample function, or simulate a coinflip using the rbinom function. To repeat a process several times, we can use the replicate function or a loop, such as for or while. Finally, we will often set a seed using the set dot seed function. Computers generate pseudo-random values based on the seed as a starting point and a random number generator algorithm. The code will still run without setting the seed, but doing so leads to the same output each time the code is run. In this course, setting the seed is useful for answer checking, and in real-world applications, it ensures replicability.

Consider this for loop, which uses the sample function to draw two numbers at random from the set of integers one to six and then sum them, a process equivalent to rolling two ordinary 6-sided dice. With the loop, it is performed 10 times. Note that in the function call, we must specify the option replace equals true so that when a given number is selected on the first draw, it is still possible to be selected on the next draw, thus mimicking the behavior of two independent dice.

Additionally, we may want to store these results. Here, we define a variable called rolls as a vector of 10 NA values, and then the for loop fills them in as the index i goes from 1 to 10.

Some puzzles will require writing functions. Consider the following function, which takes an input value, cubes it, and returns the answer. Running this function on the value 10 produces the answer 1000.

Let’s get started!

#RTutorial #DataCamp #Probability #Puzzles