## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 5
)

## ----setup, echo=FALSE, message=FALSE-----------------------------------------
library(RandomWalker)
library(dplyr)
library(ggplot2)
library(patchwork)

## ----normal_walk_basic, fig.alt="Line plot showing 10 random walks following a normal distribution, with steps on the x-axis and cumulative values on the y-axis. The walks fluctuate randomly around the starting value."----
# Basic normal walk
random_normal_walk(.num_walks = 10, .n = 100) |>
  visualize_walks()

## ----normal_walk_custom, fig.alt="Line plot showing 5 random walks with custom mean and standard deviation parameters. The walks start at 100 and show an upward drift with lower volatility."----
# With custom mean and SD
random_normal_walk(
  .num_walks = 5,
  .n = 200,
  .mu = 0.01,    # Slight upward drift
  .sd = 0.5,     # Lower volatility
  .initial_value = 100
) |> visualize_walks()

## ----normal_walk_2d, fig.alt="Scatter plot showing 3 random walks in 2D space, with x-coordinate on the x-axis and y-coordinate on the y-axis. Each walk is represented by connected points forming a path through the 2D plane."----
# 2D spatial walk
random_normal_walk(
  .num_walks = 3,
  .n = 500,
  .dimensions = 2
) |> visualize_walks()

## ----normal_drift_compare, fig.alt="Two panel plot comparing normal walks with and without drift. The top panel shows walks with subtle drift, while the bottom panel shows walks with more pronounced trending behavior due to explicit drift term."----
# Compare walks with and without drift
p1 <- random_normal_walk(.num_walks = 5, .mu = 0.1) |>
  visualize_walks(.pluck = "y") +
  labs(title = "Normal Walk")

p2 <- random_normal_drift_walk(.num_walks = 5, .mu = 0.1) |>
  visualize_walks(.pluck = "y") +
  labs(title = "Normal Walk with Drift")

p1 / p2

## ----brownian_basic, fig.alt="Line plot showing 10 standard Brownian motion paths. The paths start at zero and fluctuate randomly with continuous movements typical of Wiener processes."----
# Standard Brownian motion
brownian_motion(.num_walks = 10) |>
  visualize_walks()

## ----brownian_drift_vol, fig.alt="Line plot showing 50 Brownian motion paths with custom drift and volatility parameters. The paths start at 100 and show semi-transparent overlapping trajectories."----
# With drift and volatility
brownian_motion(
  .num_walks = 50,
  .n = 252,
  .delta_time = 0.05,
  .initial_value = 100
) |> visualize_walks(.alpha = 0.3)

## ----gbm_stock_sim, fig.alt="Line plot showing 1000 semi-transparent stock price simulation paths using geometric Brownian motion. All paths start at 100 and spread out over time, remaining positive throughout."----
# Model stock prices
stock_sim <- geometric_brownian_motion(
  .num_walks = 1000,
  .n = 252,        # Trading days in a year
  .mu = 0.08,      # 8% expected return
  .sigma = 0.25,   # 25% volatility
  .initial_value = 100
)

# Visualize scenarios
stock_sim |> visualize_walks(.alpha = 0.1)

## ----gbm_analysis, eval=FALSE-------------------------------------------------
# # Analyze outcomes
# stock_sim |>
#   summarize_walks(.value = cum_prod_y, .group_var = walk_number) |>
#   summarize(
#     median_price = median(max_val),
#     prob_profit = mean(max_val > 100),
#     prob_loss_50 = mean(min_val < 50)
#   )

## ----beta_symmetric, fig.alt="Line plot showing 10 random walks following a symmetric beta distribution with shape parameters 2 and 2. The walks fluctuate with steps bounded between 0 and 1."----
# Symmetric beta walk
random_beta_walk(
  .num_walks = 10,
  .shape1 = 2,
  .shape2 = 2
) |> visualize_walks()

## ----beta_skewed, fig.alt="Line plot showing 10 random walks following a right-skewed beta distribution with shape parameters 2 and 5. The walks show a tendency toward lower values."----
# Right-skewed (toward 0)
random_beta_walk(
  .num_walks = 10,
  .shape1 = 2,
  .shape2 = 5
) |> visualize_walks()

## ----beta_compare, fig.alt="Two panel plot comparing beta distributions with different shape parameters. The top panel shows uniform (1,1) beta walks, while the bottom panel shows right-skewed (2,5) beta walks."----
# Compare different shapes
p1 <- random_beta_walk(.shape1 = 1, .shape2 = 1) |>
  visualize_walks(.pluck = "y")
p2 <- random_beta_walk(.shape1 = 2, .shape2 = 5) |>
  visualize_walks(.pluck = "y")
p1 / p2

## ----cauchy_basic, fig.alt="Line plot showing 10 random walks following a Cauchy distribution. The walks exhibit extreme volatility with large jumps due to the heavy-tailed distribution."----
# Standard Cauchy walk
random_cauchy_walk(.num_walks = 10) |>
  visualize_walks()

## ----cauchy_compare, fig.alt="Two panel plot comparing normal walks (top) versus Cauchy walks (bottom). The Cauchy walks show much more extreme movements and larger fluctuations due to heavy tails."----
# Compare with normal walk
p1 <- random_normal_walk(.num_walks = 5, .sd = 1) |>
  visualize_walks(.pluck = "y") +
  labs(title = "Normal Walk")

p2 <- random_cauchy_walk(.num_walks = 5, .scale = 1) |>
  visualize_walks(.pluck = "y") +
  labs(title = "Cauchy Walk (Heavy Tails)")

p1 / p2

## ----chisq_low_df, fig.alt="Line plot showing 10 random walks following a chi-squared distribution with 1 degree of freedom. The walks are strongly right-skewed with always positive steps."----
# Low df (very skewed)
random_chisquared_walk(.num_walks = 10, .df = 1) |>
  visualize_walks()

## ----chisq_high_df, fig.alt="Line plot showing 10 random walks following a chi-squared distribution with 10 degrees of freedom. The walks are more symmetric than the low df case but still show positive steps only."----
# Higher df (more symmetric)
random_chisquared_walk(.num_walks = 10, .df = 10) |>
  visualize_walks()

## ----exp_basic, fig.alt="Line plot showing 10 random walks following a standard exponential distribution. The walks increase monotonically with always positive steps."----
# Standard exponential
random_exponential_walk(.num_walks = 10) |>
  visualize_walks()

## ----exp_fast, fig.alt="Line plot showing 10 random walks with fast exponential rate (5). The walks show smaller steps compared to the standard rate."----
# Fast rate (smaller steps)
random_exponential_walk(.num_walks = 10, .rate = 5) |>
  visualize_walks()

## ----exp_slow, fig.alt="Line plot showing 10 random walks with slow exponential rate (0.5). The walks show larger steps compared to the standard rate."----
# Slow rate (larger steps)
random_exponential_walk(.num_walks = 10, .rate = 0.5) |>
  visualize_walks()

## ----f_dist, fig.alt="Line plot showing 10 random walks following an F-distribution with 5 and 10 degrees of freedom. The walks show right-skewed behavior typical of F-distributions."----
random_f_walk(.num_walks = 10, .df1 = 5, .df2 = 10) |>
  visualize_walks()

## ----gamma_shape1, fig.alt="Line plot showing 10 random walks following a gamma distribution with shape parameter 1. The walks show exponential-like behavior with always positive steps."----
# Different shape parameters
random_gamma_walk(.num_walks = 10, .shape = 1, .rate = 1) |>
  visualize_walks()

## ----gamma_shape5, fig.alt="Line plot showing 10 random walks following a gamma distribution with shape parameter 5. The walks show more symmetric behavior compared to shape 1."----
random_gamma_walk(.num_walks = 10, .shape = 5, .rate = 1) |>
  visualize_walks()

## ----lognormal, fig.alt="Line plot showing 10 random walks following a log-normal distribution. The walks are right-skewed with always positive steps typical of log-normal distributions."----
random_lognormal_walk(.num_walks = 10) |>
  visualize_walks()

## ----logistic, fig.alt="Line plot showing 10 random walks following a logistic distribution. The walks have symmetric fluctuations with slightly heavier tails than normal distribution."----
random_logistic_walk(.num_walks = 10) |>
  visualize_walks()

## ----t_heavy, fig.alt="Line plot showing 10 random walks following a t-distribution with 3 degrees of freedom. The walks show heavy tails with occasional large jumps."----
# Heavy tails (df = 3)
random_t_walk(.num_walks = 10, .df = 3) |>
  visualize_walks()

## ----t_normal, fig.alt="Line plot showing 10 random walks following a t-distribution with 30 degrees of freedom. The walks closely resemble normal walks with lighter tails."----
# Nearly normal (df = 30)
random_t_walk(.num_walks = 10, .df = 30) |>
  visualize_walks()

## ----uniform, fig.alt="Line plot showing 10 random walks with uniformly distributed steps between -1 and 1. The walks show bounded steps with all values equally likely."----
# Symmetric around 0
random_uniform_walk(.num_walks = 10, .min = -1, .max = 1) |>
  visualize_walks()

## ----weibull_low, fig.alt="Line plot showing 10 random walks following a Weibull distribution with shape parameter 0.5. The walks show decreasing failure rate behavior with always positive steps."----
# Different shape parameters
random_weibull_walk(.num_walks = 10, .shape = 0.5) |>
  visualize_walks()

## ----weibull_high, fig.alt="Line plot showing 10 random walks following a Weibull distribution with shape parameter 2. The walks show increasing failure rate behavior."----
random_weibull_walk(.num_walks = 10, .shape = 2) |>
  visualize_walks()

