
This repository contains the code for the numerical experiments in the CoVaR paper. There are three main experiment folders:

- ex1: the experiment in Section 5.1;
- ex2: the experiment in Section 5.2;
- ex3: the experiment in Section EC.4.

The structure of ex1 is described first. The folders ex2 and ex3 follow related structures, with the differences explained below.


1. Structure of ex1

- Function_Set.py

This file contains auxiliary functions developed for the implementation of the experiment, including functions for simulation, risk estimation and other supporting computations.


- NN.py

This file defines the neural network architecture and related functions used by the neural-network-based smoothing method.


- data_generation.py

This file performs the two-level simulation to generate the simulated datasets. Specifically, it generates the risk factors and the Monte Carlo option prices.
To ensure a fair comparison among different smoothing methods, all smoothing methods are evaluated using the same two-level simulated data. Since the two-level simulation is computationally expensive, the generated datasets are saved in the `data` folder and can be directly loaded when running the smoothing methods.


- data/

This folder stores the datasets generated by data_generation.py.
The datasets are named in the format

    Gamma_m_data

where Gamma is the total inner-level sample size and m is the outer-level sample size in the two-level simulation. The value of m is also the actual size of the generated dataset.


- covar_true.py

This file specifies the experimental parameter design and computes the true CoVaR value under this design.
The generated parameter settings and true CoVaR values are saved in the `param` folder.


- param/

This folder stores the experimental parameter settings and the corresponding true CoVaR values generated by covar_true.py.


- Smoothing Methods in ex1

Each smoothing method in ex1 involves two Python files:

    func_xx.py
    main_xx.py

where xx denotes the corresponding smoothing technique.

     func_xx.py

	This file implements the smoothing procedure and the CoVaR estimation.

	For different simulation budgets, sample sizes, and sample allocations, the following quantities should be modified in this file:

    	Gamma
   	m
    	l
   	batch_size
    	batch_num
    	batch_size_c
    	batch_num_c

	Here:
 	batch_size and batch_num specify the batching allocation for the decoupled estimator;
 	batch_size_c and batch_num_c specify the batching allocation for the coupled estimator.

	The choices of sample sizes and sample allocations can be found in the paper or in hyperparameters and results.xlsx.

     main_xx.py

	This file runs independent replications for the corresponding smoothing method.

	It outputs the estimation results over replications, including: relative bias; relative standard deviation; relative root mean squared error.

	The hyperparameters of the smoothing method should be modified in this file. 
	The hyperparameter choices used in the numerical experiments are listed in hyperparameters and results.xlsx.

- hyperparameters and results.xlsx: summarizes sample allocation, smoothing hyperparameters and numerical results.





2. Structure of ex2

The files Function_Set.py and NN.py in ex2 are the same as those in ex1.

Different from ex1, each smoothing method in ex2 is implemented in a single file:

    ex2_xx.py

where xx denotes the corresponding smoothing technique.

Each ex2_xx.py file contains the complete experimental procedure for the corresponding smoothing method, including: simulation, hyperparameter tuning, model fitting, CoVaR estimation and computation time recording.





3. Structure of ex3

The code structure of ex3 is similar to that of ex1. It contains files for data generation, true CoVaR computation, smoothing-based estimation and hyperparameters and results following a similar organization.

The main additional component in ex3 is a separate folder containing the code for generating Figure EC.1. This folder includes the plotting scripts used to produce Figure EC.1 in the electronic companion.





4. Suggested Workflow

A typical workflow for running ex1 or ex3 is as follows.

1) Check whether the required experimental parameter settings and true CoVaR values already exist in the `param` folder. 
   If they are not available, run `covar_true.py` to generate them. The output will be saved in the `param` folder.

2) Check whether the required two-level simulated dataset already exists in the data folder. 
   If the required dataset is not available, run `data_generation.py` to generate it. The output will be saved in the `data` folder.

3) Choose a smoothing method and modify the corresponding func_xx.py file according to the desired simulation budget, sample size, and sample allocation.

4) Modify the hyperparameters in the corresponding main_xx.py file according to hyperparameters and results.xlsx.

5) Run main_xx.py to obtain the replication results, including relative bias, standard deviation, and RMSE.

For ex2, the complete procedure for each smoothing method is contained in the corresponding ex2_xx.py file. Therefore, users can directly modify and run ex2_xx.py.

