## Explore investment data

```{r fig.width = 7, fig.height = 7, dpi = 96}
visualize(investment, xvar = c("Exxon", "Boeing", "GM"), custom = FALSE)
```

```{r fig.width = 7, fig.height = 7, dpi = 96}
visualize(
  investment,
  xvar = "week",
  yvar = c("Exxon", "Boeing", "GM"),
  comby = TRUE,
  type = "line",
  custom = FALSE
)
```

```{r fig.width = 6.73, fig.height = 6.73, dpi = 96}
library(radiant.basics)
result <- correlation(investment, vars = c("Exxon", "Boeing", "GM"))
summary(result)
plot(result)
```

```{r}
result <- explore(
  investment,
  vars = c("Exxon", "Boeing", "GM"),
  fun = c("mean", "sd"),
  nr = Inf
)
summary(result)
investment_expl <- result$tab
register("investment_expl")
```

```{r}
## rename variable(s)
investment_expl <- rename(investment_expl, stock = variable, Preturn = mean, Pstdev = sd)
```

## Plot risk and return for 3 stocks

```{r fig.width=7, fig.height=4.67, dpi = 96}
visualize(
  investment_expl,
  xvar = "Pstdev",
  yvar = "Preturn",
  type = "scatter",
  labs = list(x = "Risk (Stdev)", y = "Expected return"),
  custom = TRUE
) +
  geom_text(
    label = c("Exxon", "Boeing", "GM"),
    size = 8,
    hjust = 0,
    vjust = 0
  ) +
  coord_cartesian(xlim = c(.11, .18), ylim = c(.085, .135))
```

## Simulate portfolio outcomes for 2 stocks 

```{r}
simdat2 <- simulater(
  grid = "f 0 1 0.01;",
  data = investment,
  form = c(
    "Preturn = f * mean(Boeing) + (1 - f) * mean(GM)",
    "Pstdev = sdw(f, 1 - f, Boeing, GM)",
    "## Minimize risk",
    "minr_Boeing = find_min(Pstdev, f)",
    "minr_GM = 1 - minr_Boeing",
    "## Maximize sharpe",
    "sharpe = (Preturn / Pstdev)",
    "maxs_Boeing = find_max(sharpe, f)",
    "maxs_GM = 1 - maxs_Boeing"
  ),
  seed = 1234
)
register("simdat2")
summary(simdat2, dec = 2)
```

## Scatterplot of return and stdev

```{r fig.width = 7, fig.height = 4.67, dpi = 144}
visualize(
  simdat2,
  xvar = "Pstdev",
  yvar = "Preturn",
  type = "scatter",
  labs = list(x = "Risk (Stdev)", y = "Expected return"),
  custom = TRUE
) +
  geom_text(
    data = investment_expl,
    label = c("", "Boeing", "GM"),
    size = 8,
    hjust = 0,
    vjust = 0
  ) +
  coord_cartesian(xlim = c(.09, .18), ylim = c(.085, .135))
```

## Minimize risk

The "Constants" line in the simulation output shows the portfolio percentages that lead to minimum risk (stdev).

The plots below show the link between the fraction of investment in Boeing, expected return, and risk

```{r fig.width = 7, fig.height = 4.67, dpi = 144}
visualize(
  simdat2,
  xvar = "Pstdev",
  yvar = "Preturn",
  type = "scatter",
  color = "f",
  labs = list(x = "Risk (Stdev)", y = "Expected return"),
  custom = FALSE
) 
```

```{r fig.width = 7, fig.height = 4.67, dpi = 144}
visualize(
  simdat2,
  xvar = "f",
  yvar = "Pstdev",
  type = "scatter",
  labs = list(
    x = "Fraction (f) invested in Boeing",
    y = "Standard deviation"
  ),
  custom = FALSE
)
```

```{r fig.width=7, fig.height=4.67, dpi = 96}
visualize(
  simdat2,
  xvar = "f",
  yvar = "Preturn",
  type = "scatter",
  labs = list(
    x = "Fraction (f) invested in Boeing",
    y = "Expected return"
  ),
  custom = TRUE
)
```

## Maximize Sharpe

The "Constants" line in the simulation output shows the portfolio percentages that lead to the maximum value for the Sharpe ratio.

```{r fig.width=7, fig.height=4.67, dpi = 96}
visualize(
  simdat2,
  xvar = "Pstdev",
  yvar = "Preturn",
  type = "scatter",
  color = "sharpe",
  labs = list(
    x = "Risk (Stdev)",
    y = "Expected return"
  ),
  custom = FALSE
)
```

## Simulate portfolio outcomes for 3 stocks 

```{r}
simdat3 <- simulater(
  grid = c("f 0 1 0.1", "g 0 1 0.1"),
  data = investment,
  form = c(
    "Preturn = f * mean(Boeing) + g * mean(GM)+ (1 - f - g) * mean(Exxon)",
    "Pstdev = sdw(f, g, 1 - f - g, Boeing, GM, Exxon)",
    "## remove runs that try to use > 100% of budget",
    "constraint = ifelse(f + g > 1, NA, 1)",
    "Pstdev = Pstdev * constraint",
    "## Minimize risk", "minr_Boeing = find_min(Pstdev, f)",
    "minr_GM = find_min(Pstdev, g)",
    "minr_Exxon = find_min(Pstdev, 1 - f - g)",
    "## Maximize sharpe",
    "sharpe = (Preturn / Pstdev) * constraint",
    "maxs_Boeing = find_max(sharpe, f)",
    "maxs_GM = find_max(sharpe, g)",
    "maxs_Exxon = find_max(sharpe, 1 - f - g)"
  ),
  seed = 1234
)
summary(simdat3, dec = 2)
register("simdat3")
```

## Scatterplot of return and stdev

```{r fig.width = 7, fig.height = 4.67, dpi = 144}
visualize(
  simdat3,
  xvar = "Pstdev",
  yvar = "Preturn",
  type = "scatter",
  labs = list(x = "Risk (Stdev)", y = "Expected return"),
  custom = TRUE
) +
  geom_text(
    data = investment_expl,
    label = c("Exxon", "Boeing", "GM"),
    size = 8,
    hjust = 0,
    vjust = 0
  ) +
  coord_cartesian(xlim = c(.09, .18), ylim = c(.085, .135))
```

## Minimize risk

The "Constants" line in the simulation output shows the portfolio percentages that lead to minimum risk (stdev).

The plots below show the link between the fraction of investment in Boeing, expected return, and risk

```{r fig.width = 7, fig.height = 4.67, dpi = 144}
visualize(
  simdat3,
  xvar = "Pstdev",
  yvar = "Preturn",
  type = "scatter",
  color = "f",
  labs = list(x = "Risk (Stdev)", y = "Expected return"),
  custom = FALSE
)
```

```{r fig.width = 7, fig.height = 4.67, dpi = 144}
visualize(
  simdat3,
  xvar = "f",
  yvar = "Pstdev",
  type = "scatter",
  labs = list(
    x = "Fraction (f) invested in Boeing",
    y = "Standard deviation"
  ),
  custom = FALSE
)
```

```{r fig.width = 7, fig.height = 4.67, dpi = 144}
visualize(
  simdat3,
  xvar = "f",
  yvar = "Preturn",
  type = "scatter",
  lab = list(
    x = "Fraction (f) invested in Boeing",
    y = "Expected return"
  ),
  custom = FALSE
)
```

## Maximize Sharpe

The "Constants" line in the simulation output shows the portfolio percentages that lead to the maximum value for the Sharpe ratio.

```{r fig.width = 7, fig.height = 4.67, dpi = 144}
visualize(
  simdat3,
  xvar = "Pstdev",
  yvar = "Preturn",
  type = "scatter",
  color = "sharpe",
  labs = list(
    x = "Risk (Stdev)",
    y = "Expected return"
  ),
  custom = FALSE
)
```