Value at Risk using Python

statistics
code
analysis
Author

Francois de Ryckel

Published

February 14, 2024

Modified

February 14, 2024

Computing Value at Risk (VaR) using Python.

VaR is the maximum amount loss that can happen within a confidence interval over a pre-dertermined interval. If the 1-day 95% confidence Interval Var is $100; this means that 95% of the time, under ‘normal’ market conditions, we do not expect to loose more than $100 per day for our portfolio.

import numpy as np
import pandas as pd

df = pd.read_csv('../../../raw_data/FDX.csv', index_col = 0, parse_dates = True)
returns = df['adjClose'].pct_change().dropna()
print(df)

df

returns
                  open        high         low       close    adjClose  \
date                                                                     
2023-03-09  211.080000  213.310000  207.300000  207.830000  207.830002   
2023-03-08  205.900000  209.970000  205.390000  209.540000  209.539993   
2023-03-07  209.720000  209.720000  206.160000  206.320000  206.320007   
2023-03-06  209.600000  211.890000  209.350000  210.130000  210.130005   
2023-03-03  208.460000  210.100000  207.130000  209.080000  209.080002   
...                ...         ...         ...         ...         ...   
2001-01-08   36.671723   37.013253   36.432653   36.825411   36.835415   
2001-01-05   38.165914   38.165914   36.722953   37.406011   37.416172   
2001-01-04   36.714414   38.422061   36.458267   37.790232   37.800480   
2001-01-03   32.624599   35.228761   32.445296   35.057997   35.067524   
2001-01-02   33.683341   34.127329   32.616061   32.616061   32.624928   

             volume  unadjustedVolume    change  changePercent    vwap  \
date                                                                     
2023-03-09  1953078           1918911 -3.250000       -1.54000  210.08   
2023-03-08  1073890           1073900  3.640000        1.77000  208.29   
2023-03-07  1527138           1526900 -3.400000       -1.62000  207.00   
2023-03-06  2148072           2148000  0.530000        0.25286  210.52   
2023-03-03  1612507           1612300  0.620000        0.29742  208.87   
...             ...               ...       ...            ...     ...   
2001-01-08  1120700           1120700  0.153688        0.41909   36.76   
2001-01-05  1485800           1485800 -0.759903       -1.99000   37.43   
2001-01-04  3410600           3410600  1.075820        2.93000   37.56   
2001-01-03  2203000           2203000  2.433400        7.46000   34.24   
2001-01-02   936600            936600 -1.067280       -3.17000   33.12   

                     label  changeOverTime  
date                                        
2023-03-09    March 09, 23       -0.015400  
2023-03-08    March 08, 23        0.017700  
2023-03-07    March 07, 23       -0.016200  
2023-03-06    March 06, 23        0.002529  
2023-03-03    March 03, 23        0.002974  
...                    ...             ...  
2001-01-08  January 08, 01        0.004191  
2001-01-05  January 05, 01       -0.019900  
2001-01-04  January 04, 01        0.029300  
2001-01-03  January 03, 01        0.074600  
2001-01-02  January 02, 01       -0.031700  

[5581 rows x 12 columns]
date
2023-03-08    0.008228
2023-03-07   -0.015367
2023-03-06    0.018466
2023-03-03   -0.004997
2023-03-02   -0.010283
                ...   
2001-01-08    0.020345
2001-01-05    0.015766
2001-01-04    0.010271
2001-01-03   -0.072300
2001-01-02   -0.069654
Name: adjClose, Length: 5580, dtype: float64