Skip to content

Commit

Permalink
Fred M2 Mac Updates
Browse files Browse the repository at this point in the history
  • Loading branch information
fjhickernell committed May 17, 2023
1 parent 8bd4b5c commit 0663556
Show file tree
Hide file tree
Showing 3 changed files with 271 additions and 16 deletions.
24 changes: 11 additions & 13 deletions demos/asian-option-mlqmc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -13,7 +13,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -43,7 +43,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -74,7 +74,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -106,7 +106,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -123,17 +123,17 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\tMLMC solution 5.7620 number of levels 10 time 16.439\n",
"\tcontinuation MLMC solution 5.7580 number of levels 7 time 11.721\n",
"\tMLQMC solution 5.7606 number of levels 8 time 6.095\n",
"\tcontinuation MLQMC solution 5.7594 number of levels 7 time 1.804\n"
"\tMLMC solution 5.7620 number of levels 10 time 12.119\n",
"\tcontinuation MLMC solution 5.7580 number of levels 7 time 7.775\n",
"\tMLQMC solution 5.7606 number of levels 8 time 55.780\n",
"\tcontinuation MLQMC solution 5.7594 number of levels 7 time 18.445\n"
]
}
],
Expand All @@ -151,9 +151,7 @@
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"scrolled": false
},
"metadata": {},
"outputs": [
{
"name": "stdout",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 9,
"id": "f6646cd6-4c59-4389-aa93-e06115c9004c",
"metadata": {},
"outputs": [
Expand All @@ -965,7 +965,7 @@
" n_total 2^(18)\n",
" n [[ 1024. 1024. 262144. 2048.]\n",
" [ 1024. 1024. 262144. 2048.]]\n",
" time_integrate 1.131\n",
" time_integrate 1.063\n",
"CubQMCNetG (StoppingCriterion Object)\n",
" abs_tol 0.050\n",
" rel_tol 2^(-1)\n",
Expand Down Expand Up @@ -1067,6 +1067,263 @@
"#root: results.to_latex(root+'lr_table.tex',formatters={'%s'%tt:lambda v:'%.1f'%(100*v) for tt in ['accuracy','precision','recall']},float_format=\"%.2e\")"
]
},
{
"cell_type": "markdown",
"id": "eb89695e-91b0-46ac-805f-56601e5b2278",
"metadata": {},
"source": [
"## Asian Option"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "2bb3b4ed-3321-48f1-8ac6-1d683647a1ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"BrownianMotion (TrueMeasure Object)\n",
" time_vec [0.5 1. ]\n",
" drift 0\n",
" mean [0. 0.]\n",
" covariance [[0.5 0.5]\n",
" [0.5 1. ]]\n",
" decomp_type PCA\n",
"[[ 0.60150096 -0.37174803]\n",
" [ 0.97324899 0.22975292]]\n",
"[[0.5 0.5]\n",
" [0.5 1. ]]\n",
"BrownianMotion (TrueMeasure Object)\n",
" time_vec [0.5 1. ]\n",
" drift 0\n",
" mean [0. 0.]\n",
" covariance [[0.5 0.5]\n",
" [0.5 1. ]]\n",
" decomp_type CHOLESKY\n",
"[[0.70710678 0. ]\n",
" [0.70710678 0.70710678]]\n",
"[[0.5 0.5]\n",
" [0.5 1. ]]\n"
]
}
],
"source": [
"nsteps = 2\n",
"ld = qp.Sobol(nsteps)\n",
"bmPCA = qp.BrownianMotion(ld)\n",
"print(bmPCA)\n",
"print(bmPCA.a)\n",
"covPCA = np.matmul(bmPCA.a,bmPCA.a.T)\n",
"print(covPCA)\n",
"bmCHO = qp.BrownianMotion(ld,decomp_type = 'Cholesky')\n",
"print(bmCHO)\n",
"print(bmCHO.a)\n",
"covCHO = np.matmul(bmCHO.a,bmCHO.a.T)\n",
"print(covCHO)"
]
},
{
"cell_type": "markdown",
"id": "5e1d6b19-3297-4078-845f-9ab479803b29",
"metadata": {},
"source": [
"### Comparing PCA and Cholesky Decompositions\n",
"Note that the Cholesky decomposition might not even meet the error tolerance"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "e1e428a2-11ca-446e-b1d7-0ba5c08363f1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Asian Option true value (1 time steps): 5.22530 (to within 1e-4) in 0.06 seconds using PCA\n",
"Asian Option true value (1 time steps): 5.22528 (to within 1e-4) in 0.20 seconds using Cholesky\n",
"Asian Option true value (2 time steps): 5.63592 (to within 1e-4) in 0.07 seconds using PCA\n",
"Asian Option true value (2 time steps): 5.63593 (to within 1e-4) in 0.13 seconds using Cholesky\n",
"Asian Option true value (4 time steps): 5.73167 (to within 1e-4) in 0.10 seconds using PCA\n",
"Asian Option true value (4 time steps): 5.73173 (to within 1e-4) in 0.47 seconds using Cholesky\n",
"Asian Option true value (8 time steps): 5.75526 (to within 1e-4) in 0.31 seconds using PCA\n",
"Asian Option true value (8 time steps): 5.75531 (to within 1e-4) in 2.86 seconds using Cholesky\n",
"Asian Option true value (16 time steps): 5.76112 (to within 1e-4) in 0.61 seconds using PCA\n",
"Asian Option true value (16 time steps): 5.76124 (to within 1e-4) in 9.04 seconds using Cholesky\n",
"Asian Option true value (32 time steps): 5.76261 (to within 1e-4) in 1.27 seconds using PCA\n",
"Asian Option true value (32 time steps): 5.76259 (to within 1e-4) in 31.14 seconds using Cholesky\n",
"Asian Option true value (64 time steps): 5.76297 (to within 1e-4) in 2.34 seconds using PCA\n",
"Asian Option true value (64 time steps): 5.76272 (to within 1e-4) in 72.32 seconds using Cholesky\n",
"AsianOption (Integrand Object)\n",
" volatility 0.200\n",
" call_put call\n",
" start_price 100\n",
" strike_price 100\n",
" interest_rate 0.050\n",
" mean_type arithmetic\n",
" dim_frac 0\n",
"LDTransformData (AccumulateData Object)\n",
" solution 5.763\n",
" comb_bound_low 5.763\n",
" comb_bound_high 5.763\n",
" comb_flags 1\n",
" n_total 2^(24)\n",
" n 2^(24)\n",
" time_integrate 72.319\n",
"CubQMCSobolG (StoppingCriterion Object)\n",
" abs_tol 1.00e-04\n",
" rel_tol 0\n",
" n_init 2^(10)\n",
" n_max 2^(35)\n",
"AsianOption (Integrand Object)\n",
" volatility 0.200\n",
" call_put call\n",
" start_price 100\n",
" strike_price 100\n",
" interest_rate 0.050\n",
" mean_type arithmetic\n",
" dim_frac 0\n",
"BrownianMotion (TrueMeasure Object)\n",
" time_vec [0.016 0.031 0.047 ... 0.969 0.984 1. ]\n",
" drift 0\n",
" mean [0. 0. 0. ... 0. 0. 0.]\n",
" covariance [[0.016 0.016 0.016 ... 0.016 0.016 0.016]\n",
" [0.016 0.031 0.031 ... 0.031 0.031 0.031]\n",
" [0.016 0.031 0.047 ... 0.047 0.047 0.047]\n",
" ...\n",
" [0.016 0.031 0.047 ... 0.969 0.969 0.969]\n",
" [0.016 0.031 0.047 ... 0.969 0.984 0.984]\n",
" [0.016 0.031 0.047 ... 0.969 0.984 1. ]]\n",
" decomp_type CHOLESKY\n",
"Sobol (DiscreteDistribution Object)\n",
" d 2^(6)\n",
" dvec [ 0 1 2 ... 61 62 63]\n",
" randomize LMS_DS\n",
" graycode 0\n",
" entropy 222126631653436714845721888141095080739\n",
" spawn_key ()\n"
]
}
],
"source": [
"option_parameters = {'volatility' : .2,\n",
" 'start_price' : 100,\n",
" 'strike_price' : 100,\n",
" 'interest_rate' : 0.05}\n",
"for level in range(7):\n",
" nsteps = 2**level\n",
" aco = qp.AsianOption(qp.Sobol(nsteps), **option_parameters, decomp_type = \"PCA\")\n",
" approx_solution, data = qp.CubQMCSobolG(aco, abs_tol=1e-4).integrate()\n",
" print(\"Asian Option true value (%d time steps): %.5f (to within 1e-4) in %.2f seconds using PCA\"%(nsteps, approx_solution, data.time_integrate))\n",
" aco = qp.AsianOption(qp.Sobol(nsteps), **option_parameters, decomp_type = \"Cholesky\")\n",
" approx_solution, data = qp.CubQMCSobolG(aco, abs_tol=1e-4).integrate()\n",
" print(\"Asian Option true value (%d time steps): %.5f (to within 1e-4) in %.2f seconds using Cholesky\"%(nsteps, approx_solution, data.time_integrate))\n",
"print(aco)\n",
"print(data)"
]
},
{
"cell_type": "markdown",
"id": "995cbda4-afe0-441d-9418-38bd44982e1e",
"metadata": {},
"source": [
"This function compares 4 different algorithms: Multilevel Monte Carlo (`CubMCML`), Multilevel Quasi-Monte Carlo (`CubQMCML`), continuation Multilevel Monte Carlo (`CubMCMLCont`) and Multilevel Quasi-Monte Carlo (`CubQMCMLCont`):"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "f424e9f5-d101-4d4b-8af4-8009b803d2e4",
"metadata": {},
"outputs": [],
"source": [
"def eval_option(option_mc, option_qmc, abs_tol):\n",
" stopping_criteria = {\n",
" \"MLMC\" : qp.CubMCML(option_mc, abs_tol=abs_tol, levels_max=15),\n",
" \"continuation MLMC\" : qp.CubMCMLCont(option_mc, abs_tol=abs_tol, levels_max=15),\n",
" \"MLQMC\" : qp.CubQMCML(option_qmc, abs_tol=abs_tol, levels_max=15),\n",
" \"continuation MLQMC\" : qp.CubQMCMLCont(option_qmc, abs_tol=abs_tol, levels_max=15)\n",
" }\n",
" \n",
" levels = []\n",
" times = []\n",
" for name, stopper in stopping_criteria.items():\n",
" sol, data = stopper.integrate()\n",
" levels.append(data.levels)\n",
" times.append(data.time_integrate)\n",
" print(\"\\t%-20s solution %-10.4f number of levels %-6d time %.3f\"%(name, sol, levels[-1], times[-1]))\n",
" \n",
" return levels, times"
]
},
{
"cell_type": "markdown",
"id": "99cd64d9-0800-4ba5-a390-45c59c4c09f7",
"metadata": {},
"source": [
"Define the Multilevel Asian options:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "31865d64-ba37-40de-a819-f8d13544b330",
"metadata": {},
"outputs": [],
"source": [
"option_mc = qp.MLCallOptions(qp.IIDStdUniform(seed = 7), option=\"asian\")\n",
"option_qmc = qp.MLCallOptions(qp.Lattice(seed = 7), option=\"asian\")"
]
},
{
"cell_type": "markdown",
"id": "97d1089c-0a1d-42cb-b5c8-cf4109d1b97d",
"metadata": {},
"source": [
"Run and compare each of the 4 algorithms for the Asian option problem:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "174131d6-bfe9-4c4d-84f1-1e4c187b0f07",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\tMLMC solution 5.7620 number of levels 10 time 13.393\n",
"\tcontinuation MLMC solution 5.7580 number of levels 7 time 9.626\n",
"\tMLQMC solution 5.7606 number of levels 8 time 56.882\n",
"\tcontinuation MLQMC solution 5.7594 number of levels 7 time 24.743\n"
]
}
],
"source": [
"eval_option(option_mc, option_qmc, abs_tol=5e-3);"
]
},
{
"cell_type": "markdown",
"id": "aed5fa32-de38-4a40-92c1-4f7618e0c94d",
"metadata": {},
"source": [
"Repeat this comparison for a sequence of decreasing tolerances, with 5 different random seeds each. This will allow us to visualize the asymptotic cost complexity of each method."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "501e96cf-d98f-4550-82f6-67755b73678e",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"id": "52045e0f-4b62-4e70-bbf2-578080ffea3e",
Expand Down
2 changes: 1 addition & 1 deletion qmcpy/true_measure/gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def _parse_gaussian_params(self, mean, covariance, decomp_type):
order = argsort(-evals)
self.a = dot(evecs[:,order],diag(sqrt(evals[order])))
elif self.decomp_type == 'CHOLESKY':
self.a = cholesky(self.sigma).T
self.a = cholesky(self.sigma) #Fred changed this
else:
raise ParameterError("decomp_type should be 'PCA' or 'Cholesky'")
self.mvn_scipy = multivariate_normal(mean=self.mu,cov=self.sigma)
Expand Down

0 comments on commit 0663556

Please sign in to comment.