vartests is a Python library to perform some statistic tests to evaluate Value at Risk (VaR) Models

Overview

python   MIT license  

vartests is a Python library to perform some statistic tests to evaluate Value at Risk (VaR) Models, such as:

  • T-test: verify if mean of distribution is zero;
  • Kupiec Test (1995): verify if the number of violations is consistent with the violations predicted by the model;
  • Berkowitz Test (2001): verify if conditional distributions of returns "GARCH(1,1)" used in the VaR Model is adherent to the data. In this specific test, we do not observe the whole data, only the tail;
  • Christoffersen and Pelletier Test (2004): also known as Duration Test. Duration is time between violations of VaR. It tests if VaR Model has quickly response to market movements by consequence the violations do not form volatility clusters. This test verifies if violations has no memory i.e. should be independent.

Installation

Using pip

You can install using the pip package manager by running:

pip install vartests

Alternatively, you could install the latest version directly from Github:

pip install https://github.com/rafa-rod/vartests/archive/refs/heads/main.zip

Why vartests is important?

After VaR calculation, it is necessary to perform statistic tests to evaluate the VaR Models. To select the best model, they should be validated by backtests.

Example

First of all, lets read a file with a PnL (distribution of profit and loss) of a portfolio in which also contains the VaR and its violations.

import pandas as pd

data = pd.read_excel("Example.xlsx", index_col=0)
violations = data["Violations"]
pnl = data["PnL"] 
data.sample(5)

The dataframe looks like:

' |     PnL       |      VaR        |   Violations |
  | -889.003707   | -2554.503872    |            0 |
  | -2554.503872  | -2202.221691    |            1 | 
  | -887.527423   | -2193.692570    |            0 |  
  | -274.344126   | -2160.290746    |            0 | 
  | 1376.018638   | -5719.833100    |            0 |'

Not all tests should be applied to the VaR Model. Some of them its applied whether the VaR Model has assumption of zero mean or follow a specific distribution. So you should test the data:

import vartests

vartests.zero_mean_test(pnl.values, conf_level=0.95)

This assumption is commom used in parametric VaR like EWMA and GARCH Models. Besides that, is necessary check assumption of distribution. So you should test with Berkowitz (2001):

import vartests

vartests.berkowtiz_tail_test(pnl, volatility_window=252, var_conf_level=0.99, conf_level=0.95)

The following tests should be used to any kind of VaR Models.

import vartests

vartests.kupiec_test(violations, var_conf_level=0.99, conf_level=0.95)

vartests.duration_test(violations, conf_level=0.95)

If you want to see the failure ratio of the VaR Model, just type:

import vartests

vartests.failure_rate(violations)
You might also like...
Describing statistical models in Python using symbolic formulas

Patsy is a Python library for describing statistical models (especially linear models, or models that have a linear component) and building design mat

A Python package for the mathematical modeling of infectious diseases via compartmental models
A Python package for the mathematical modeling of infectious diseases via compartmental models

A Python package for the mathematical modeling of infectious diseases via compartmental models. Originally designed for epidemiologists, epispot can be adapted for almost any type of modeling scenario.

Fit models to your data in Python with Sherpa.

Table of Contents Sherpa License How To Install Sherpa Using Anaconda Using pip Building from source History Release History Sherpa Sherpa is a modeli

A probabilistic programming language in TensorFlow. Deep generative models, variational inference.

Edward is a Python library for probabilistic modeling, inference, and criticism. It is a testbed for fast experimentation and research with probabilis

A Pythonic introduction to methods for scaling your data science and machine learning work to larger datasets and larger models, using the tools and APIs you know and love from the PyData stack (such as numpy, pandas, and scikit-learn).

This tutorial's purpose is to introduce Pythonistas to methods for scaling their data science and machine learning work to larger datasets and larger models, using the tools and APIs they know and love from the PyData stack (such as numpy, pandas, and scikit-learn).

Generate lookml for views from dbt models
Generate lookml for views from dbt models

dbt2looker Use dbt2looker to generate Looker view files automatically from dbt models. Features Column descriptions synced to looker Dimension for eac

Collections of pydantic models

pydantic-collections The pydantic-collections package provides BaseCollectionModel class that allows you to manipulate collections of pydantic models

In this tutorial, raster models of soil depth and soil water holding capacity for the United States will be sampled at random geographic coordinates within the state of Colorado.
In this tutorial, raster models of soil depth and soil water holding capacity for the United States will be sampled at random geographic coordinates within the state of Colorado.

Raster_Sampling_Demo (Resulting graph of this demo) Background Sampling values of a raster at specific geographic coordinates can be done with a numbe

Comments
  • Error in duration test

    Error in duration test

    Hi there,

    Thanks a lot for this open source var tests algos! I found an error occurred when running duration test(Christofferson duration test). When I input a violations series starting with 1, the error reported: UnboundLocalError: local variable 'D' referenced before assignment. From the code, we could also find that when violation series start with 1, D is unassigned. image I might have a bug free algo in creating durations and censor series, welcome to discuss with me if you want :). Also, I have another question about the optimization part, where you assign: N = len(D)-1, I guess you do minus 1 to make the index correct. But based on the Christofferson paper, it should be N = len(D) from my point of view.

    Best regards, Ruicheng

    opened by RuichengGeng 9
  • Not able to import vartests even after installation

    Not able to import vartests even after installation

    I have installed vartests using various methods. One among them is from Jupyter, the other is from pip install vartests. I am able to see vartests directory in site-packages, but when I try importing the package I am unable to do so. I have tried all the methods that are found in the web to install and debug but to no avail. I just wanted to understand if the package is good to be used.

    opened by deeppradhan 5
Owner
RAFAEL RODRIGUES
Quantitative Finance, data science, optimisation, Python, julia, R.
RAFAEL RODRIGUES
BigDL - Evaluate the performance of BigDL (Distributed Deep Learning on Apache Spark) in big data analysis problems

Evaluate the performance of BigDL (Distributed Deep Learning on Apache Spark) in big data analysis problems.

Vo Cong Thanh 1 Jan 6, 2022
Multiple Pairwise Comparisons (Post Hoc) Tests in Python

scikit-posthocs is a Python package that provides post hoc tests for pairwise multiple comparisons that are usually performed in statistical data anal

Maksim Terpilowski 264 Dec 30, 2022
A python package which can be pip installed to perform statistics and visualize binomial and gaussian distributions of the dataset

GBiStat package A python package to assist programmers with data analysis. This package could be used to plot : Binomial Distribution of the dataset p

Rishikesh S 4 Oct 17, 2022
Mortgage-loan-prediction - Show how to perform advanced Analytics and Machine Learning in Python using a full complement of PyData utilities

Mortgage-loan-prediction - Show how to perform advanced Analytics and Machine Learning in Python using a full complement of PyData utilities. This is aimed at those looking to get into the field of Data Science or those who are already in the field and looking to solve a real-world project with python.

Joachim 1 Dec 26, 2021
Using Python to scrape some basic player information from www.premierleague.com and then use Pandas to analyse said data.

PremiershipPlayerAnalysis Using Python to scrape some basic player information from www.premierleague.com and then use Pandas to analyse said data. No

null 5 Sep 6, 2021
Created covid data pipeline using PySpark and MySQL that collected data stream from API and do some processing and store it into MYSQL database.

Created covid data pipeline using PySpark and MySQL that collected data stream from API and do some processing and store it into MYSQL database.

null 2 Nov 20, 2021
A probabilistic programming library for Bayesian deep learning, generative models, based on Tensorflow

ZhuSuan is a Python probabilistic programming library for Bayesian deep learning, which conjoins the complimentary advantages of Bayesian methods and

Tsinghua Machine Learning Group 2.2k Dec 28, 2022
ForecastGA is a Python tool to forecast Google Analytics data using several popular time series models.

ForecastGA is a tool that combines a couple of popular libraries, Atspy and googleanalytics, with a few enhancements.

JR Oakes 36 Jan 3, 2023
Hidden Markov Models in Python, with scikit-learn like API

hmmlearn hmmlearn is a set of algorithms for unsupervised learning and inference of Hidden Markov Models. For supervised learning learning of HMMs and

null 2.7k Jan 3, 2023
A Python package for Bayesian forecasting with object-oriented design and probabilistic models under the hood.

Disclaimer This project is stable and being incubated for long-term support. It may contain new experimental code, for which APIs are subject to chang

Uber Open Source 1.6k Dec 29, 2022