IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory...

12
Introduction SAS IML References Using IML to Calculate Value at Risk (VaR) SAS’s ‘functional’ sibling Sébastien Casault ENMAX Energy Corporation 5 October, 2016 Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Transcript of IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory...

Page 1: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

Using IML to Calculate Value at Risk (VaR)SAS’s ‘functional’ sibling

Sébastien Casault

ENMAX Energy Corporation

5 October, 2016

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 2: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

VaR DefinitionVaR TheoryMath theory

Introduction

DefinitionValue at Risk (VaR) is a measure of the risk of investments. Itestimates how much a set of investments might lose, given normalmarket conditions, in a set time period such as a day. – Wikipedia

Value at Risk is often part of an organization’s controls andperformance metrics. It’s a simple number that can be used toquickly gauge both the dynamics of and exposure to a market.

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 3: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

VaR DefinitionVaR TheoryMath theory

Graphical representation

$0

$20

$40

$60

$80

15‐Jun‐16 15‐Jul‐16 15‐Aug‐16 15‐Sep‐16

Actuals

gaussian

0%

2%

4%

6%

8%

10%

12%

14%

16%

‐0.04

‐0.035

‐0.03

‐0.025

‐0.02

‐0.015

‐0.01

‐0.005 0

0.005

0.01

0.015

0.02

0.025

0.03

0.035

0.04

0.045

0.05

0.055

0.06

Actuals

Gaussian

VaRi = wiPiσiZα

It’s a measure of maximum* potential loses assuming that theunderlying commodity behaves normally.Things get a little trickier when we have a portfolio ofinstruments – diversification reduces risk.

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 4: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

VaR DefinitionVaR TheoryMath theory

Var-covar two commodity portfolio calculationSimplified var‐covar (parametric) Value at Risk (VaR) calculation

Portfolio inputs Power GasPrice 35.00$                   2.55$              Confidence level 0.975Position 500                        5,000              critical z 1.960Volatility 8.98% 4.60%CorrelationExposure 17,500.00$           12,730.00$   VaR 3,080.08$             1,147.72$     

Portfolio exposure 30,230.00$          

Var‐covar matrix, ∑ Matrix MathPosition (x') ∑ Position (x)

Power Gas 17,500.00$          12,730.00$  0.00806404 0.00082616 17,500.00$ Power 0.0081 0.0008 0.00082616 0.002116 12,730.00$ Gas 0.0008 0.0021

Position (x') ∑ xPortfolio 17,500.00$          12,730.00$  151.64$       Variance 3,180,611.77$   41.39$         Volatility 1,783.43$         VaR 3,495.45$          x' ∑ x

3,180,612$         

20%

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 5: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

VaR DefinitionVaR TheoryMath theory

Matrix multiplication

(AB)ij =m∑

k=1AikBkj

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 6: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

VaR DefinitionVaR TheoryMath theory

Base SAS matrix multiplication

1 Reshape matrix to ‘long’ format

Data step with array to cycle through columns

2 Perform product of two matrices

PROC SQL to perform pairwise sum(product)

3 Use PROC Transpose to reshape the results

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 7: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

IntroductionExampleInterfacing with imlVaR calculation

The Interactive Matrix Language

x11 x12 x13 . . . x1nx21 x22 x23 . . . x2n...

...... . . . ...

xm1 xm2 xm3 . . . xmn

IML is a matrix programming language similar to R and MATLABthat enables you to use natural mathematical syntax to writecustom algorithms and to compute statistics that are not built intoany SAS procedure:

1 iterative-type statistical procedures;2 doing operations on row and column of data sets;3 e.g., matrix multiplication.

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 8: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

IntroductionExampleInterfacing with imlVaR calculation

Matrix multiplication example

proc iml;print "---------- SAS/IML Results --------------";x = 1:3;m = {1 4 7, 2 5 8, 3 6 9};q = m * t(x);

print("------------- R Results -----------------")x <- rep(1:3) # vector of sequence 1,2,3m <- matrix(1:9, nrow=3) # 3 x 3 matrixq <- m %*% x # matrix multiplicationprint(q)

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 9: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

IntroductionExampleInterfacing with imlVaR calculation

Pushing and pulling data

Pull datasets

proc iml;use WORK.correlationMatric;

read all into correls[];close WORK.correlationMatric;

Push datasets

create portVaR var {variables};append;

close portVaR;

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 10: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

IntroductionExampleInterfacing with imlVaR calculation

VaR calculation

zScore = probit(.975);

DO i = 1 to items;indivVaR[i] = volatility[i] * portfolio[i] * zScore;

END;/* indivVaR = volatility # portfolio * zScore; */

portVaR = sqrt(t(indivVaR) * correls * indivVaR);

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 11: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

IntroductionExampleInterfacing with imlVaR calculation

VaR backtesting

Inputs: historical daily Profits & Losses and calculatedVaR.

This example is lifted from the SAS website and shows—over oneyear—that the PnL was greater than VaR about once a month, asexpected for a calculation using a 95th percentile

Sébastien Casault Using IML to Calculate Value at Risk (VaR)

Page 12: IML Value-At-Risk Backtesting (Casault) · Introduction SASIML References VaRDefinition VaRTheory Maththeory Var-covartwocommodityportfoliocalculation Simplified var r covar (parametric)

IntroductionSAS IML

References

Bibliography

1 SAS/IML(R) 14.1 User’s Guide2 https://en.wikipedia.org/wiki/Value_at_risk

Sébastien Casault Using IML to Calculate Value at Risk (VaR)