Appendix A — R mini-labs

These examples are ordinary, visible R code blocks so the HTML book builds with Quarto without requiring R. Copy them into RStudio to run them, or change a block to an executable Quarto R chunk once you want code to execute automatically during rendering.

A.1 Diversification with simple assumed inputs

mu <- c(A = 0.08, B = 0.05)
sigma <- matrix(c(0.04, 0.006, 0.006, 0.01), nrow = 2)
w <- c(A = 0.6, B = 0.4)
expected_return <- sum(w * mu)
portfolio_volatility <- sqrt(drop(t(w) %*% sigma %*% w))
c(expected_return = expected_return, volatility = portfolio_volatility)

The numbers are hypothetical annualized inputs, not forecasts.

A.2 Plot an expiration payoff

ST <- seq(70, 140, by = 1)
long_call  <- pmax(ST - 100, 0) - 6
short_call <- 2 - pmax(ST - 110, 0)
spread_profit <- long_call + short_call
plot(ST, spread_profit, type = 'l', lwd = 2,
     xlab = 'Underlying price at expiration', ylab = 'Profit per share')
abline(h = 0, lty = 2)

It excludes financing, bid-ask spreads, commissions, assignment, and early exit.