{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Sampling" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We show how to construct correlated sample with Gaussian copula." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import chaospy as cp\n", "import numpy as np\n", "\n", "from econsa.correlation import gc_correlation" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "First we specify the marginal distributions and correlation matrix." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "corr = [[1.0, 0.6, 0.2], [0.6, 1.0, 0.0], [0.2, 0.0, 1.0]]\n", "marginals = (\n", " cp.Normal(mu=1230),\n", " cp.Normal(mu=0.0135),\n", " cp.Uniform(lower=1.15, upper=3.15),\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We then transform the correlation matrix using equation (4.5) in Kucherenko et al. (2012)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "corr_transformed = gc_correlation(marginals, corr)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are ready to use transformed correlation matrix to sample from a Gaussian copula." ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[ 1. , 0.6018, 0.2013],\n", " [ 0.6018, 1. , -0.0018],\n", " [ 0.2013, -0.0018, 1. ]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "copula = cp.Nataf(cp.J(*marginals), corr_transformed)\n", "corr_copula = np.corrcoef(copula.sample(100000))\n", "\n", "np.round(corr_copula, decimals=4)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.7" } }, "nbformat": 4, "nbformat_minor": 4 }