---
title: "End-to-End Workflow"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{End-to-End Workflow}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

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

```{r}
library(finlabR)
```

## 1. Load prices and compute returns

```{r}
prices <- get_example_prices()
rets <- calc_returns(prices[, -1], method = "log")
```

## 2. Correlation analysis across assets

```{r}
cor_mat <- asset_correlation(rets)
cor_mat[1:3, 1:3]
```

## 3. Mean-variance optimization

```{r}
ef <- mvo_efficient_frontier(rets, n = 25, rf = 0.02)
max_sharpe <- mvo_max_sharpe(rets, rf = 0.02)
min_var <- mvo_min_variance(rets)
```

## 4. CVaR and risk parity portfolios

```{r}
cvar <- cvar_minimize(rets, alpha = 0.95)
rp <- risk_parity_weights(stats::cov(rets))
```

## 5. Regime clustering and asset clustering

```{r}
regimes <- market_regime_kmeans(rets, k = 3, window = 60)
clusters <- asset_clustering(rets, method = "kmeans", reduce = "pca", k = 3)
```

## 6. Risk analytics

```{r}
var_cvar(rets, alpha = 0.95)
```

## 7. Monte Carlo and option pricing

```{r}
paths <- simulate_gbm_paths(100, 0.08, 0.2, time_horizon = 1, n_steps = 252, n_sims = 500)
price_option_mc(100, 100, 0.02, 0.2, time_to_maturity = 1, n_sims = 10000)
price_option_binomial(100, 100, 0.02, 0.2, time_to_maturity = 1, n_steps = 200, american = TRUE)
```
