Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Introduction
In the world of time series analysis, Random Walks, Brownian Motion, and Geometric Brownian Motion are fundamental concepts used in various fields, including finance, physics, and biology. Today, we’ll explore these concepts using functions from the healthyR.ts
package.
Random Walks
A Random Walk is a path that consists of a series of random steps. It’s a simple but powerful concept used to model seemingly unpredictable paths, such as stock prices or animal movements.
Let’s generate and plot some Random Walks using the ts_random_walk()
function from healthyR.ts
.
Function Syntax
ts_random_walk( .mean = 0, .sd = 0.1, .num_walks = 100, .periods = 100, .initial_value = 1000 )
.mean
: The desired mean of the random walks..sd
: The standard deviation of the random walks..num_walks
: The number of random walks you want to generate..periods
: The length of the random walk(s) you want to generate..initial_value
: The initial value where the random walks should start.
Example
library(ggplot2) library(healthyR.ts) random_walk_data <- ts_random_walk( .mean = 0, .sd = 0.1, .num_walks = 10, .periods = 100, .initial_value = 1000 ) head(random_walk_data)
# A tibble: 6 × 4 run x y cum_y <dbl> <dbl> <dbl> <dbl> 1 1 1 0.0725 1072. 2 1 2 0.182 1267. 3 1 3 0.110 1407. 4 1 4 0.0275 1445. 5 1 5 -0.0546 1367. 6 1 6 0.0712 1464.
random_walk_plot <- random_walk_data |> ggplot( mapping = aes( x = x, y = cum_y, color = factor(run), group = factor(run) ) ) + geom_line(alpha = 0.8) + ts_random_walk_ggplot_layers(random_walk_data) print(random_walk_plot)
This code generates 10 random walks over 100 periods, starting from an initial value of 1000. The resulting plot visualizes the paths of these random walks, each represented by a different color.
< section id="brownian-motion" class="level2">Brownian Motion
Brownian Motion, also known as Wiener Process, is a continuous-time stochastic process that is often used to model random movements in physics and finance.
< section id="function-syntax-1" class="level3">Function Syntax
ts_brownian_motion( .time = 100, .num_sims = 10, .delta_time = 1, .initial_value = 0, .return_tibble = TRUE )
.time
: Total time of the simulation..num_sims
: Total number of simulations..delta_time
: Time step size..initial_value
: Initial value of the simulation..return_tibble
: Return a tibble (TRUE) or a matrix (FALSE).
Example
brownian_data <- ts_brownian_motion( .time = 100, .num_sims = 10, .delta_time = 1, .initial_value = 0, .return_tibble = TRUE ) head(brownian_data)
# A tibble: 6 × 3 sim_number t y <fct> <int> <dbl> 1 sim_number 1 1 0 2 sim_number 2 1 0 3 sim_number 3 1 0 4 sim_number 4 1 0 5 sim_number 5 1 0 6 sim_number 6 1 0
brownian_plot <- ts_brownian_motion_plot( .data = brownian_data, .date_col = t, .value_col = y, .interactive = TRUE ) brownian_plot
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.