datacamp / authoring

https://authoring.datacamp.com/
Other
8 stars 9 forks source link

Add a running example for R #52

Closed gvwilson closed 6 years ago

gvwilson commented 6 years ago

From Dave Robinson's Stochastic Processes in R:

Stochastic Processes in R

Step 1: Brainstorming

  1. What problem(s) will student learn how to solve?
    • They'll learn the generative models behind several stochastic process models, and understand to simulate them with R functions.
  2. What techniques or concepts will students learn?
    • In learning three new distributions, the exponential, gamma, and uniform, they'll reinforce the concept that (unlike the binomial) distributions can be continuous
    • They'll become more familiar with the use of simulation to understand probability models and answer questions about them.
  3. What technologies, packages, or functions will students use?
    • The distribution-related functions around the exponential, gamma, and uniform functions
    • The sample, cumsum, replicate, and accumulate functions
  4. What terms or jargon will you define?
    • Random walk, Markov chain, transition matrix, waiting time, Poisson process, Poisson point process
  5. What analogies will you use to explain concepts?
    • Two people gambling repeatedly with a coin for a random walk
    • Randomly generated text in a Markov chain
    • A light bulb burning out for an exponential variable/Poisson process
    • Simulating trees in a forest for a spatial Poisson process

Step 2: Who is this course for?

Link to student profiles. If these don't match exactly, feel free to modify as needed in discussion:

Step 3: How far will this course get its students?

This could be similar to the last exercise of the course or a last exercise in a chapter.

Last exercise in Chapter 2: Write a function that simulates 100 steps from a Markov chain of words, given a transition_matrix with row and column names.

Skills:

Solution:

library(purrr)

simulate_step <- function(state) {
  state <- sample(nrow(transition_matrix), 1, prob = transition_matrix[state, ])
  colnames(transition_matrix)[state]
}

accumulate(1:100, simulate_step, .init = "the")

Last exercise in the course: Write a function that, given a number of points, the width of a region, and the height of a region, generate that many points in a Poisson point process.

Use it to plot 50 points in the space of 10 x 10.

Skills required:

Solution

simulate_points <- function(density, x_width, y_width) {
  number <- rpois(1, density * x_width * y_width)
  x <- runif(number, 0, x_width)
  y <- runif(number, 0, y_width)

  plot(x, y)
}

simulate_points(1, 10, 10)

Step 4: What will the student do along the way?

Middle of chapter 2: Generate three steps in a Markov chain given a transition matrix and starting state.

Solution:

step2 <- sample(nrow(transition_matrix), 1, transition_matrix[state, ])
step3 <- sample(nrow(transition_matrix), 1, transition_matrix[state2, ])
step4 <- sample(nrow(transition_matrix), 1, transition_matrix[state3, ])

Middle of chapter 4: Randomly generate 100 events in a one-dimensional Poisson process with a rate of 3 per second by simulating exponential waiting times. Find the distribution of how many events happen in the first 2 seconds.

Solution:

cumsum(rexp(100, 3))

replicate(1000, sum(cumsum(rexp(100, 3)) <= 2))

Step 5: How are the concepts connected? (Course outline)

Chapter 1: Random walks

Chapter 3: The exponential distribution

Chapter 4: Poisson processes

Step 6: Course overview

Course Description

Whether it's prices in the stock market, the number of visitors to a website, or the population of rabbits in a forest, many phenomena that we'd like to model with statistics involved numbers tracked over time. In this course, you'll be introduced to the field of stochastic processes, an area of probability studying systems that change over time. You'll learn about common statistical models such as random walks, Poisson processes, and Markov chains, as well as being introduced to the exponential and gamma distributions. These provide the fundamentals for many statistical methods common in finance, biology, and many other fields.

Learning Objectives

Prerequisites

richierocks commented 6 years ago

@gvwilson Do you still want this? There are examples for each step of the speccing process, so do you need to have a complete example as well?

gvwilson commented 6 years ago

I don't think so, especially now that we have an example of a complete R spec.