Musings

A random collection

Archive for the ‘quant-finance’ Category

TECH: Get a real-time quote from Yahoo Finance

require 'open-uri'
require 'nokogiri'

def getQuote(symbol)
  q = ""
  doc = Nokogiri::HTML(open("http://finance.yahoo.com/q?s=#{symbol}&ql=1"))
  doc.xpath('//*[@class="real_time"]').each do |node|
    y = node.xpath("span").first
    node.xpath("span").each { |x| q += x.text }
  end
  return q
end

ARGV.each { |s| print "#{s} = #{getQuote(s)}\n"; }

Another place to get real-time quote: http://batstrading.com/json/bzx/book/WFC

Advertisements

Written by curious

August 31, 2011 at 8:13 am

Posted in quant-finance

QUANT: Important Stochastic Processes

Geometric Brownian Motion

  1. It’s SDE is:
      dY_t = \mu Y_t dt + \sigma Y_t dW_t

    or also as

      d \log (Y_t) = \left(\mu -\sigma^2/2\right) dt + \sigma dW_t
  2. Solution: Y_T = Y_0 e^{(\mu-\sigma^2/2)T+\sigma W_T}

Ornstein-Uhlenbeck Process (aka Vasicek Model)

  1. It is a Mean-Reverting Process, although random, shows a pronounced tendency toward an equilibrium value.
  2. It’s SDE is:
      dY_t = -\alpha(Y_t - \mu) dt + \sigma dW_t

    \mu – long term mean/equilibrium value/mean value
    \sigma – volatility to model random shocks
    \alpha – rate by which the shocks dissipate and variable returns towards equilibrium mean

  3. Solution for this SDE is:
      Y_t = Y_0 e^{-\alpha t} + \sigma e^{-\alpha t} \int_0^t e^{\alpha s} dW_s
  4. Useful for modeling interest rates, currency exchange rates, and commodity prices

Bessel Process

  1. Stochastic Differential Equation:
      dX_t = dW_t + \frac{a}{X_t}dt

Written by curious

February 8, 2011 at 12:59 pm

Posted in quant-finance

Protected: QUANT: Trading Software

This content is password protected. To view it please enter your password below:

Written by curious

November 10, 2010 at 9:50 am

Posted in quant-finance

Protected: QUANT: Some more portfolio theory

This content is password protected. To view it please enter your password below:

Written by curious

October 31, 2010 at 11:25 am

Posted in quant-finance

QUANT: More on Portfolio Theory

  1. Utility Theory
    1. Utility
      1. more is preferred to less (non-satiation)
      2. diminishing marginal utility
    2. Utility Function, U(w). Increasing (U'(w) > 0) => non-satiation, Concave down (U”(w) < 0) = diminishing marginal utility
    3. Utility Maximization
    4. Constrained Optimization
    5. Certainty Equivalent – amount in risk-free investment that will give the same utility value as expected utility value of investment in risky investments. \text{certainty-equiv} = U^{-1}(\mathbb{E}[U(r_{\text{risky}})])
    6. Risk Premium = \mathbb{E}[r_{\text{risky}}] - r_{\text{riskfree}} = \mathbb{E}[r_{\text{risky}}] - \text{certainty-equiv}

      Risk Premium rate is \frac{\text{riskpremium}}{\text{certainty-equiv}}-1 or \log(\frac{\text{riskpremium}}{\text{certainty-equiv}})-1

      A Risk Averse investor would expect positive risk-premium

      A Risk Seeking investor would expect negative risk-premium

    7. Risk Aversion
    8. Absolute Risk Aversion, A(w) = - \frac{U''(w)}{U'(w)}
    9. Relative Risk Aversion, R(w) = w A(w)
  2. Maximum Variance Portfolio Analysis – Goal
    1. Maximize return for a given amount of risk
    2. Given an expected return minimize the amount of risk
  3. Efficient Frontier
    1. Combining the assets:

      Suppose we have a portfolio of 2 assets (X, Y) with weights (a, 1-a), then expected return and variance of the portfolio will be:

      1. Expected Return: \mathbb{E}[\mathbf{r}_{p}] = a \mu_X + (1-a) \mu_Y
      2. Variance: \text{Var}[\mathbf{r}_{p}] = a^2 \text{Var}[X] + (1-a)^2 \text{Var}[Y] + 2a(1-a)\text{Cov}(X,Y) = a^2 \sigma_X^2 + (1-a)^2 \sigma_Y^2 + 2a(1-a)\rho_{X,Y}\sigma_X \sigma_Y

      If we plot Volatility vs Expected Return (obtained from the above formulas) for different possible values of ‘a’, we get the hyperbolic curve that gives us the Efficient Frontier.

      R = [.04 .07];
      Vol = [.2 .3];
      rho=0.5;
      efficientFrontier(R, Vol, rho)
      
      function efficientFrontier(assetReturn, assetVolatility, rho)
        asset1Weight = -10:0.01:10;
        asset2Weight = 1-asset1Weight;
        Y=assetReturn(1).*asset1Weight+assetReturn(2).*asset2Weight;
        X=sqrt((assetVolatility(1)*asset1Weight).^2+(assetVolatility(2)*asset2Weight).^2+2*rho*assetVolatility(1)*assetVolatility(2)*asset1Weight*diag(asset2Weight));
        plot(X,Y);
        xlabel('Volatility');
        ylabel('Returns');
      end
      

      n-Assets Portfolio with

      1. Weights \sum_{i=1}^n w_i = 1,
      2. Expected Return: \mathbb{E}[\mathbf{p}] = \sum_{i=1}^n w_i \mu_i
      3. Variance: \text{Var}[\mathbf{p}] = \sum_{i=1}^n w_i^2 \sigma_i^2 + \sum_{i=1}^n \sum_{j=1, j \neq i}^n (\text{Cov}(i,j) w_i w_j) = \mathbf{w}_p' \mathbf{S} \mathbf{w}_p = \sigma_p^2
    2. How do we find Minimum Variance Portfolio? For what weight ‘w’, will the portfolio have minimum variance? Hint: At the minima, d(Var[r])/dw = 0.

      w = \frac{\sigma_Y^2 - \rho_{X,Y} \sigma_X \sigma_Y}{\sigma_X^2+\sigma_Y^2-2\rho_{X,Y}\sigma_X\sigma_Y}, 1-w = \frac{\sigma_X^2 - \rho_{X,Y} \sigma_X \sigma_Y}{\sigma_X^2+\sigma_Y^2-2\rho_{X,Y}\sigma_X\sigma_Y}

    3. Global Minimum Variance Portfolio (the general case – portfolio of n-assets).

      Given: Covariance Matrix, S, the portfolio with minimum variance is given by (weight vector of the portfolio):

        \mathbf{w}_{\text{G}} = \frac{\mathbf{S}^{-1} \mathbf{1}}{\mathbf{1}' \mathbf{S}^{-1} \mathbf{1}}, where \mathbf{1} = \begin{bmatrix} 1 \\ \vdots \\ 1 \end{bmatrix}
    4. Mean Variance Efficient Portfolio/Tangency Portfolio/Optimal Portfolio
      1. Given Excess Return vector (in excess of risk free return rate) \mathbf{z} = \begin{bmatrix} r_{1} - r_{\text{free}} \\ \vdots \\ r_{n} - r_{\text{free}} \end{bmatrix}
      2. Given Covariance Matrix, S
      3. Efficient Portfolio weight, \mathbf{w}_E = h \mathbf{S}^{-1} \mathbf{z}, where h = \frac{1}{\mathbf{1}' S^{-1} \mathbf{z}}
    5. Global Minimum Variance Portfolio will be identical to the Tangency Portfolio when all the Assets Returns (hence Excess Returns too) are exactly the same
  4. Sharpe Ratio: “excess return” per unit volatility of an asset or a portfolio, p
      Sharpe Ratio = \frac{r_p - r_{\text{free}}}{\sigma_p}

    Note: On a “Volatility vs Returns” plot/graph, all portfolios on a straight line have the same Sharpe Ratio. Like all portfolios on the tangent line have the same Sharpe Ratio.

    Note: Tangency Portfolio on the Efficient Frontier has the highest possible Sharpe Ratio

    Note: Capital Market Line – The Tangency Portfolio line is also called Capital Market Line

  5. Decomposition of a Security’s Return:
      r_s = \eta_s + \beta_s r_M + \epsilon_s

    \eta_s – a constant term for the security, s
    \beta_s – beta term – also a constant for the security, s.
    r_M – market’s return. Note: \beta_s r_M is the Market Specific component of the Security’s return.
    \epsilon_sresidual return/specific return/idiosyncratic return/unique return of the security, s. It is not a constant, it is a Random Variable and has mean 0.

    Assumptions:

    • Mean of Residual Return is 0, \mathbb{E}[\epsilon_s] = 0
    • Residual Return for any security is independent of the market return
      \text{Cov}[\epsilon_s, r_M] = 0
    • Residual Return for any security is independent of Residual Return of any other security
      \text{Cov}[\epsilon_s, \epsilon_t] = 0

    Note:

    1. Expected Return of Security s, \mathbb{E}[r_s] = \eta_s + \beta_s \mathbb{E}[r_M]
    2. Variance (of Return) of Security s, \text{Var}[r_s] = \beta_s^2 \text{Var}[r_M] + \text{Var}[\epsilon_s] (= market part + security specific part)
    3. Covariance of Securities s, t, \text{Cov}[r_s, r_t] = \beta_s \beta_t \text{Var}[r_M] = \mathbb{E}[r_s r_t] - \mathbb{E}[r_s]\mathbb{E}[r_t]

      Note: Covariance of 2 securities only depends on the Market Risk and their Betas. How 2 securities move together can be described by their common response to market moves.

    4. Expected Beta of any security is 1, \mathbb{E}[\beta_s] = 1, \mathbb{E}[\eta_s] = 0, \forall s
    5. Portfolio Return: r_p = \sum_s w_s r_s
      Portfolio Expected Return: \mathbb{E}[r_p] = \eta_p + \beta_p \mathbb{E}[r_M], where \beta_p = \sum w_s \beta_s, \eta_p = \sum w_s \eta_s
    6. Variance of Return of a Portfolio, \text{Var}[r_p] = \beta_p^2 \text{Var}[r_M] + \sum_{s \in p} w_s^2 \text{Var}[\epsilon_s]

      Note: In a large diversified portfolio, the impact of residual risk on the portfolio vanishes. \text{Var}[r_p] \to \beta_p^2 \text{Var}[r_M].

    7. Note: \text{Var}[\epsilon_s] is diversifiable or unsystematic risk, \beta_s is undiversifiable/systematic risk
    8. Note: Recall \text{Var}[r_p] \to \overline{\text{Cov}}. Which means risk that can not be diversified away is due to the covariances of the securities in the portfolio. Combining that with the result above we note that it is proportional to the market’s risk \text{Var}[r_M].
    9. Market Portfolio, p = M, then \eta_M = 0 = \mathbb{E}[\eta_s], \beta_M = 1 = \mathbb{E}[\beta_s] = 1
  6. Security Market Line – line on \beta_s \text{ vs } \mathbb{E}[r_s] plot. Y-axis intercept is risk-free return and has \beta = 0. The “market portfolio” has \beta_M = 1

    Equation of a “Security Market Line” is

    • \mathbb{E}[r_s] = r_{\text{free}} + (\mathbb{E}[r_M] - r_{\text{free}})\beta_s

      equivalently: (\mathbb{E}[r_s] - r_{\text{free}}) = (\mathbb{E}[r_M] - r_{\text{free}})\beta_s

      or (Excess Return of Security) = (Security’s Beta) x (Excess Return of Market)

    • \mathbb{E}[r_s] = (1-\beta_s) r_{\text{free}} + \mathbb{E}[r_M] \beta_s

      or (Expected Return of Security) = Weighted Average of (Risk-Free Rate, Market Expected Return)

    Note: SML shows: “Expected Return” of any Security/Portfolio = linear and increasing function of Systematic Risk (beta)
    Note: SML shows: Only Risk that effects the “Expected Return” of Security/Portfolio is Market Risk

  7. Jensen’s Alpha: any extra expected return above that predicted by SML
      (\mathbb{E}[r_p] - r_{\text{free}}) = \alpha_p + (\mathbb{E}[r_M] - r_{\text{free}})\beta_p
  8. Treynor Ratio: excess return per unit beta of an asset, s
      Treynor Ratio = \frac{r_s - r_{\text{free}}}{\beta_s}
  9. Beta – signifies systemic risk component of the asset
    1. \beta = \rho_{\text{asset,market}} \frac{\sigma_{\text{asset}}}{\sigma_{\text{market}}} = \text{Cov}(\text{asset},\text{market})/\text{Var}(\text{market})
  10. Capital Asset Pricing Model
  11. Regression is equivalent to projection on a line
  12. Beta Hedging a Portfolio
  13. Tracking Basket, Tracking Error
  14. Single Index Model
    r_s = \eta_s + \beta_s r_M + \epsilon_s

    where
    \mathbb{E}[\epsilon_s] = 0, \text{Cov}(\epsilon_s, \epsilon_t) = 0, \text{Cov}(\epsilon_s, r_M) = 0

    Covariance of 2 assets/stocks
    \text{Cov}(r_s,r_t) = \beta_s \beta_t \text{Var}[r_M]

  15. Multi Factor Model
    r_s = \eta_s + \sum_{k=1}^K \beta_{s,k} f_k + \epsilon_s

    where
    \epsilon_{s} = \text{ residual portion of returns}

    \beta_{s,k} = \text{ factor loading for asset s, factor}, f_k

    f_{k} = \text{ factor,} f_k

    In other words
    \begin{bmatrix} r_1 \\ r_2 \\ \vdots \\ r_N \end{bmatrix} = \begin{bmatrix} \eta_1 \\ \eta_2 \\ \vdots \\ \eta_N \end{bmatrix} + \begin{bmatrix} \beta_{1,1} & \beta_{1,2} & \cdots & \beta_{1,K} \\ \beta_{2,1} & \beta_{2,2} & \cdots & \beta_{2,K} \\ \vdots      & \vdots      & \ddots & \vdots      \\ \beta_{N,1} & \beta_{N,2} & \cdots & \beta_{N,K} \\ \end{bmatrix} \begin{bmatrix} f_1 \\ f_2 \\ \vdots \\ f_K \end{bmatrix} + \begin{bmatrix} \epsilon_1 \\ \epsilon_2 \\ \vdots \\ \epsilon_N \end{bmatrix}

    \mathbf{r} = \boldsymbol{\eta} + \boldsymbol{\beta} \mathbf{f} + \boldsymbol{\epsilon}

    Covariance Matrix in terms of multi-factor model:
    \mathbf{S}_{N \times N} = \boldsymbol{\beta}_{N \times K} \mathbf{F}_{K \times K} \boldsymbol{\beta}_{K \times N}^T + \boldsymbol{\Omega}_{N \times N}

    where
    \mathbf{F}_{K \times K} = \text{ covariance matrix of factors,} f_{k}
    \boldsymbol{\Omega}_{N \times N} = \text{ covariance matrix of residuals, (diagonal matrix)}
    \mathbf{S}_{N \times N} = \text{ covariance matrix of returns of assets}

    \mathbf{S} = \begin{bmatrix} \beta_{1,1} & \beta_{1,2} & \cdots & \beta_{1,K} \\ \beta_{2,1} & \beta_{2,2} & \cdots & \beta_{2,K} \\ \vdots      & \vdots      & \ddots & \vdots      \\ \beta_{N,1} & \beta_{N,2} & \cdots & \beta_{N,K} \\ \end{bmatrix} \begin{bmatrix} \text{Var}[f_1] & \text{Cov}(f_1,f_2) & \cdots & \text{Cov}(f_1,f_K) \\ \text{Cov}(f_2,f_1) & \text{Var}[f_2] & \cdots & \text{Cov}(f_2,f_K) \\ \vdots      & \vdots      & \ddots & \vdots      \\ \text{Cov}(f_K,f_1) & \text{Cov}(f_K,f_2) & \cdots & \text{Var}[f_K] \\ \end{bmatrix} \begin{bmatrix} \beta_{1,1} & \beta_{2,1} & \cdots & \beta_{N,1} \\ \beta_{1,2} & \beta_{2,2} & \cdots & \beta_{N,2} \\ \vdots      & \vdots      & \ddots & \vdots      \\ \beta_{1,K} & \beta_{2,K} & \cdots & \beta_{N,K} \\ \end{bmatrix} + \begin{bmatrix} \text{Var}[\epsilon_1] & 0 & \cdots & 0 \\ 0 & \text{Var}[\epsilon_2] & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & \text{Var}[\epsilon_n] \end{bmatrix}

  16. Principal Component Analysis
  17. Regression of X on Y
  18. Marginal Contribution of an asset on portfolio risk
  19. Optimal Portfolio when you change currency

Written by curious

October 24, 2010 at 10:21 am

Posted in quant-finance

QUANT: Portfolio Theory Topics

Topics in Portfolio Theory

  1. Risk is relative. Finance can not be risk free.
  2. Markets – bring buyer and seller together. They facilitate trade: provide a mechanism (called “price discovery”) for mutually beneficial exchanges.
  3. Price Discovery – markets discover price as buyers and sellers BID and OFFER to buy and sell
  4. BID – tell a price at which you are willing to BUY
  5. OFFER/ASK – tell a price at which are willing to SELL
  6. Correct/Market Price – what clears the market, i.e., leaves no excess quantity demanded or supplied
  7. Spread – difference of BID price and OFFER price in a market. Indication of liquidity. Narrow spread implies easy liquidity
  8. Liquidity – ability to do a transaction at a price similar to the last traded price. There should be willing Buyers and Sellers at all times.
  9. Liquidity is correlated with Volume. Higher Volume implies Higher Liquidity and vice versa.
  10. Market Depth – the volume/quantity that can be Bought or Sold at a given price
  11. Risk – we know probability distribution of the outcomes, but we do not know the outcome
  12. Uncertainty – we do not know the outcome and we also do not know the probability distribution
  13. Decision Criteria to decide under Uncertainty – Way to pick a strategy when there is no knowledge of probabilities of outcomes. Example: Maximax, Maximin, Minimax Regret Criterion
  14. Measure risk – Variance, Mean/Variance
  15. Price – exchange rate. Ratio of quantities.
  16. Return – measure of change in a price
  17. Given time series of prices: P_{s,0}, P_{s,1}, \ldots, P_{s,T}
    1. Simple Return/Percent Return, R_{s,t} = \frac{P_{s,t}-P_{s,t-1}}{P_{s,t-1}}, t \in [1, ..., T]
    2. Gross Return, G_{s,t} = 1+\frac{P_{s,t}-P_{s,t-1}}{P_{s,t-1}} = 1 + R_{s,t}
    3. Log Return, r_{s,t} = \log(P_{s,t}) - \log(P_{s,t-1}) = \log\left(\frac{P_{s,t}}{P_{s,t-1}}\right) = \log(1+R_{s,t})
    4. Simple Return is to Simple Compounding: P_{s,t} = P_{s,t-1}(1+R_{s,t})
    5. Log Return is to Continuous Compounding: P_{s,t} = P_{s,t-1}e^{r_{s,t}}
    6. Effect of dividend on returns calculation. R_{s,t} = \frac{(P_{s,t}+D_{s,t})-P_{s,t-1}}{P_{s,t-1}}, r_{s,t} = \log(P_{s,t}+D_{s,t}) - \log(P_{s,t-1})
    7. Effect of m:n stock split on returns calculation. R_{s,t} = \frac{(m P_{s,t})-n P_{s,t-1}}{n P_{s,t-1}}, r_{s,t} = \log(m P_{s,t}) - \log(n P_{s,t-1})
    8. Stochastic Processes:
      1. R_{t} = \Delta P_{t}/P_{t-1} = m\Delta t + \sigma \epsilon \sqrt{\Delta t}, P_{t} = P_{t-1}(1+R_{t})
      2. dP_t/P_t = \mu dt + \sigma dW_t, dr_t = (\mu-\sigma^2/2) dt + \sigma dW_t, \log P_T = \log P_0 + (\mu-\sigma^2/2) T + \sigma W_T
    9. Average Returns: Given a time series of returns R_1, R_2, \ldots, R_T
      1. Arithmetic Average: \overline{R}_{arith} = \frac{1}{T} \sum_{t=1}^T R_t
      2. Geometric Average: 1+\overline{R}_{geo} = \left(\prod_{t=1}^T \left(1+R_t\right)\right)^{1/T}
  18. Statistics:
    1. Expectation \mathbb{E}[r] = \mu(r)
    2. Variance \text{Var}[r] = \sigma^2(r) = \mathbb{E}[r^2-\mathbb{E}[r]^2] = \mathbb{E}[r^2]-\mathbb{E}[r]^2
    3. Standard Deviation \text{STD}[r] = \sigma(r)
    4. CoVariance \text{Cov}[x,y] = \mathbb{E}[(x-\mathbb{E}[x])(y-\mathbb{E}[y])] = \mathbb{E}[xy] - \mathbb{E}[x]\mathbb{E}[y]
    5. Correlation \mathbf{Corr}[x,y] = \frac{\text{Cov}[x,y]}{\text{STD}[x]\text{STD}[y]}
  19. Mean (Log) Return: \overline{r} = \frac{1}{T} \sum_{t=1}^T r_t, r_t = \log(P_t/P_{t-1})
  20. Total Holding Period Return: r_{HPR} = \sum_{t=1}^T r_t = \log(P_T/P_0)
  21. Annualized Mean Return: \overline{r}_{\text{annual}} = \text{(num of observations per year)} \frac{1}{T} \sum_{t=1}^T r_t
  22. num of observations per year
    1. Annual returns = 1
    2. Monthly returns = 12
    3. Weekly returns = 52.177
    4. Daily returns = 252 (# of trading days in 1 year for volatility calculations), for interest rate related calculations, use calendar days 365
  23. Volatility (always annualized):
    1. Historic Annualized Volatility/Standard Deviation \hat{\sigma} = \sqrt{\text{(num of observations per year)} \frac{1}{T-1} \sum_{t=1}^{T} (r_t - \hat{r}_{\text{sample}})^2}
    2. Zero Mean Assumption Volatility: \hat{\sigma}_{\text{zero}} = \sqrt{\text{(num of observations per year)} \frac{1}{T} \sum_{t=1}^{T} r_t^2}
    3. Expected Return Assumption Volatility: \hat{\sigma}_{\text{exp}} = \sqrt{\text{(num of observations per year)} \frac{1}{T} \sum_{t=1}^{T} (r_t - r_t^{\text{expected}})^2}.
      r_t^{\text{expected}} is the expected/desired return for the period t.
  24. Rolling window – showing historical volatility over time. Calculate volatility for time t by using returns over the window period before and up to time t, i.e., t \in [T-\delta T, T]
  25. Weighted windows: weighting function: equal weight, linear weight, exponential weight, truncated exponential weight
  26. Correlation – related to regression
  27. Covariance Matrix, S: let Y be T x N matrix of observations of returns (T returns observations for each of N assets), Covariance matrix is a N x N matrix given by:

    \mathbf{S} = \Sigma_{N \times N}^2 = (\text{observations per year}) \frac{1}{T} \mathbf{Y}' \mathbf{Y} = \begin{bmatrix} \text{Cov}(r_1,r_1) & \text{Cov}(r_1,r_2) & \cdots & \text{Cov}(r_1,r_N) \\ \text{Cov}(r_1,r_2) & \text{Cov}(r_2,r_2) & \cdots & \text{Cov}(r_2,r_N) \\ \vdots & \vdots & \ddots & \vdots \\ \text{Cov}(r_1,r_N) & \text{Cov}(r_2,r_N) & \cdots & \text{Cov}(r_N,r_N) \end{bmatrix}

  28. Covariance matrix is symmetric. Diagonal elements are variances.

    \mathbf{S} = \begin{bmatrix} \text{Var}[r_1] & \text{Cov}(r_1,r_2) & \cdots & \text{Cov}(r_1,r_N) \\ \text{Cov}(r_1,r_2) & \text{Var}[r_2] & \cdots & \text{Cov}(r_2,r_N) \\ \vdots & \vdots & \ddots & \vdots \\ \text{Cov}(r_1,r_N) & \text{Cov}(r_2,r_N) & \cdots & \text{Var}[r_N] \end{bmatrix}
    \text{Var}[\mathbf{Y}]_{N \times 1} = \text{diag}(\mathbf{S})

  29. When the assets are uncorrelated, then the Covariance matrix becomes a diagonal matrix (since all the covariance terms are 0).
    \mathbf{S} = \begin{bmatrix} \text{Var}[r_1] & 0 & \cdots & 0 \\ 0 & \text{Var}[r_2] & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & \text{Var}[r_N] \end{bmatrix} = \text{diag}(\text{Var}[\mathbf{Y}])

    Inverse of a diagonal matrix is easy
    \mathbf{S}^{-1} = \begin{bmatrix} \frac{1}{\text{Var}[r_1]} & 0 & \cdots & 0 \\ 0 & \frac{1}{\text{Var}[r_2]} & \cdots & 0 \\ \vdots & \vdots & \ddots & \vdots \\ 0 & 0 & \cdots & \frac{1}{\text{Var}[r_N]} \end{bmatrix} = \text{diag}(\frac{1}{\text{Var}[\mathbf{Y}]})

  30. Correlation matrix:

    \mathbf{Corr} = \begin{bmatrix} 1 & \rho(r_1,r_2) & \cdots & \rho(r_1,r_N) \\ \rho(r_1,r_2) & 1 & \cdots & \rho(r_2,r_N) \\ \vdots & \vdots & \ddots & \vdots \\ \rho(r_1,r_N) & \rho(r_2,r_N) & \cdots & 1 \end{bmatrix}

    Correlation matrix is also symmetric. Diagonal elements are 1.

  31. Correlation matrix to Covariance matrix conversion

    \mathbf{S} = \text{diag}(\sqrt{\text{Var}[\mathbf{Y}]}) \times \mathbf{Corr} \times  \text{diag}(\sqrt{\text{Var}[\mathbf{Y}]})

    here, \text{Var}[\mathbf{Y}] = \begin{bmatrix} \sigma_1^2 & \sigma_2^2 & \cdots & \sigma_n^2 \end{bmatrix}^T

  32. Covariance matrix to Correlation matrix conversion

    \mathbf{Corr} = \text{diag}(1/\sqrt{\text{Var}[\mathbf{Y}]}) \times \mathbf{S} \times \text{diag}(1/\sqrt{\text{Var}[\mathbf{Y}]}) \\ = \text{diag}(1/\sqrt{\text{diag}(\mathbf{S})}) \times \mathbf{S} \times \text{diag}(1/\sqrt{\text{diag}(\mathbf{S})})

  33. Vector of returns:

    \mathbf{z} = \begin{bmatrix} r_1 & r_2 & \cdots & r_N \end{bmatrix}^T

  34. Portfolio – a vector of weights of assets,

    \mathbf{w}_p = \begin{bmatrix} w_1 & w_2 & \cdots & w_n \end{bmatrix}^T

  35. Portfolio Returns – total return of all the assets in a portfolio,

    r_p = \mathbf{w}_p^T \mathbf{z} = \sum_{s=1}^n w_s r_s

  36. Portfolio Volatility – total volatility of all the assets in a portfolio,

    \sigma_p = \sqrt{\mathbf{w}_p^T \mathbf{S} \mathbf{w}_p}

  37. Diversification – invest in all assets. Example, put (1/n) in each asset. Portfolio Volatility of a well diversified portfolio is driven by Mean of Covariances only, individual asset volatilities do not matter. Why? To calculate portfolio volatility, we add all the Variance and Covariance terms, there are n Variance terms (along the diagonal) and [(n-1) x n] Covariance terms. So Covariances dominate the contribution.

    \lim_{n \to \infty} \sigma_p^2 \to \overline{\text{Cov}} = \frac{1}{n} \sum_{i \neq j} \text{Cov}(r_i,r_j)

  38. Covariance between two Portfolios (of the same assets): It is a number (not a matrix), \text{Cov}(\mathbf{r}_a, \mathbf{r}_b) = \mathbf{w}_a \mathbf{S} \mathbf{w}_b
  39. A Covariance Matrix must be Positive Semi-Definite (i.e. \mathbf{x} \mathbf{S} \mathbf{x} \geq 0, \forall \mathbf{x} \neq \mathbf{0}). Which means all eigenvalues of S are positive. A condition for that is \det(\mathbf{S}-\lambda \mathbf{I}) = 0.
  40. A valid Correlation Matrix must also be positive semi-definite. A 2 x 2 matrix (for 2 assets) is valid if \rho_{1,2}^2 \leq 1.

    For a 3 x 3 correlation matrix a necessary condition is:
    \rho_{1,2}^2 + \rho_{2,3}^2 + \rho_{1,3}^2 - 2\rho_{1,2}\rho_{2,3}\rho_{1,3} \leq 1

  41. Simulate Sample price movements: S + \Delta S = S e^{(\mu - \sigma^2/2)\Delta t + \sigma Z \Delta t}, Z \sim \Phi(0,1)

    Stochastic Process: dS = \mu dt + \sigma S dW_t, Discrete Version: \Delta S = \mu S \Delta t + \sigma S Z \sqrt{\Delta t}, Z \sim \Phi(0,1)
    Log S Process: d (\log S) = (\mu-\sigma^2/2)dt + \sigma dW_t, Discrete Version: \log (S+\Delta S)-\log(S) = (\mu-\sigma^2/2)\Delta t + \sigma Z \sqrt{\Delta t}

  42. Generating Independent Random Numbers in MATLAB:
    % m x n matrix of independent random numbers
    randn(m,n) 
    

    Generating Correlated Random Normal Variables. Want to generate N x n matrix of random variables Y.
    These random variables should be correlated normal, and must have covariance matrix equal to S (given)

    Method 1: Cholesky Decomposition

    1. Generate Independent Random Variables, U
    2. Take Cholesky Decomposition of the Covariance Matrix (S), call it C. Then CC = S.
    3. The Correlated Random Variables will then be, Y = U C
    U = randn(T,N);
    C = chol(S);
    Y = UC;
    

    Ponder on what is the correlation matrix of Y? It is \frac{1}{T} \mathbf{Y}'\mathbf{Y} = \frac{1}{T} \mathbf{UC}'\mathbf{UC} = \frac{1}{T} \mathbf{C}' \mathbf{U}'\mathbf{UC} = \mathbf{S}. Remember U is independent standard normal, so it’s mean is 0 and variance is 1, i.e., \frac{1}{T} \mathbf{U}' \mathbf{U} = \mathbf{I}.

    In fact, any matrix B such that \mathbf{B}' \mathbf{B} = \mathbf{S} can be used to generate the correlated random variables.

    Method 2: Spectral Decomposition

    1. Given an S, take it’s eigenvalues λ and eigenvectors V. Then \mathbf{B}' = \text{diag}(\sqrt{\mathbf{\lambda}}) \mathbf{V},  \mathbf{B}' \mathbf{B} = \mathbf{S}
    2. Define Y = UB

Written by curious

October 23, 2010 at 7:13 pm

Posted in quant-finance

Protected: QUANT: Comparison

This content is password protected. To view it please enter your password below:

Written by curious

October 15, 2010 at 8:37 am

Posted in quant-finance