---
title: "Getting Started with RandomWalker"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Getting Started with RandomWalker}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

```{r setup}
library(RandomWalker)
```

## Random Walks

What is a random walk? A random walk is a mathematical concept describing a path consisting of a succession of random steps. Each step is independent of the previous ones, and the direction and distance of each step are determined by chance. The simplest form of a random walk is a one-dimensional walk, where an object moves either forward or backward with equal probability at each step. 

In a random walk, the average position of the object after \(N\) steps is zero, but the average of the square of the distance from the starting point is \(N\), meaning the object is typically about \(\sqrt{N}\) steps away from the start after \(N\) steps. This concept is widely used in various fields, including physics, economics, and computer science, to model phenomena such as particle diffusion, stock price movements, and network topology.

![Random Walk Example](https://upload.wikimedia.org/wikipedia/commons/thumb/2/28/Eight-step_random_walks.png/500px-Eight-step_random_walks.png)

In finance, the random walk theory suggests that stock prices move unpredictably, making it impossible to predict future prices based on past movements. This theory underpins the Efficient Market Hypothesis, which posits that all available information is already reflected in stock prices, thus making it impossible to consistently outperform the market.

![Random Walk in Higher Dimensions](https://upload.wikimedia.org/wikipedia/commons/thumb/b/bd/Walk3d_0.png/500px-Walk3d_0.png)

In higher dimensions, random walks exhibit complex geometric properties and can be used to model more intricate systems, such as the behavior of particles in a fluid or the structure of networks.

By understanding random walks, researchers can gain insights into various stochastic processes and their applications across different scientific and practical domains.

Now that we have a basic understanding of these concepts, let's explore how we can create and visualize random walks using the `RandomWalker` package in R.

## Installation

You can install the released version of `RandomWalker` from CRAN with:

```r
install.packages("RandomWalker")
```

You can install the development version of `RandomWalker` from [GitHub](https://github.com/spsanderson/RandomWalker) with:

``` r
# install.packages("devtools")
devtools::install_github("spsanderson/RandomWalker")
```

## Example Usage

Let's start by generating a simple random walk using the `rw30()` function from the `RandomWalker` package. This function generates a random walk with 30 steps.

```{r rw30_example}
rw30() |>
  head(10)
```

The output shows the first 10 steps of the random walk. Each step represents the position of the object after moving forward or backward by one unit. The `rw30()` function takes
no parameters and generates 30 random walks with 100 steps. It uses the `rnorm()`
function to generate random numbers from a normal distribution with mean 0 and standard deviation 1.

## Attributes

Each function in the `RandomWalker` package has attributes that can be accessed using the `attributes()` function. For example, let's look at the attributes of the `rw30()` function:

```{r rw30_attributes}
atb <- attributes(rw30())
atb[!names(atb) %in% c("row.names")]
```

## Visualizing Random Walks

To visualize the random walk generated by the `rw30()` function, we can use the `visualize_walks()` function. This function creates a line plot showing the path of the random walk over time.

```{r random_walk_visual_example, fig.width=7, fig.height=4}
#| fig.alt: >
#|   Visualize a Random Walk of 30 simulations
visualize_walks(rw30())
```

The plot shows the path of the random walk over time, with the x-axis representing the number of steps and the y-axis representing the position of the object. The random walk exhibits a random pattern of movement, with the object moving forward and backward in a seemingly unpredictable manner.

## Future Direction

As of now `RandomWalker` only supports 1-dimensional random walks. In the future, we plan to extend the package to support higher-dimensional random walks and provide additional functionalities for analyzing and visualizing random walks in various contexts.

We hope this vignette has provided you with a basic understanding of random walks and how to use the `RandomWalker` package in R. If you have any questions or feedback, please feel free to reach out to us.

## References

- [Issues](https://github.com/spsanderson/RandomWalker/issues)
- [Discussions](https://github.com/spsanderson/RandomWalker/discussions)
- [Random Walks on Wikipedia](https://en.wikipedia.org/wiki/Random_walk)
