Skip to content

Commit

Permalink
updates from develop branch
Browse files Browse the repository at this point in the history
  • Loading branch information
alegresor committed Aug 4, 2020
1 parent 7369e80 commit 142940e
Show file tree
Hide file tree
Showing 241 changed files with 10,569 additions and 25,414 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ docs/Gemfile.lock
# Sphinx
sphinx/_build/
sphinx/_static/
sphinx/demo_rst/
sphinx/readme_rst/

# Jupyter Notebooks
*checkpoint.ipynb
Expand Down
4 changes: 3 additions & 1 deletion MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
include qmcpy/discrete_distribution/qrng/*.h
include qmcpy/discrete_distribution/qrng/*.h
include qmcpy/discrete_distribution/sobol/mps_sobol_Cs.col
include qmcpy/discrete_distribution/lattice/lattice-32001-1024-1048576.3600.npy
166 changes: 117 additions & 49 deletions README.md

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions blogs/why_q_in_mc/gen_figures.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@
IIDStdUniform(2,seed=7).gen_samples(n),
Lattice(2,backend='GAIL',seed=7).gen_samples(n)]
titles = ['$U[0,1]^2$','Shifted Lattice']
symbols = ['T','X']
output_files = ['iid_uniform_pts','lattice_pts']

for pts,title,out_f in zip(pts_sets,titles,output_files):
for pts,title,symbol,out_f in zip(pts_sets,titles,symbols,output_files):
fig,ax = pyplot.subplots(nrows=1, ncols=1, figsize=(5,5))
ax.scatter(pts[:,0],pts[:,1],color='b')
ax.set_xlabel('$X_{i1}$')
ax.set_xlabel('$%s_{i1}$'%symbol)
ax.set_xlim([0,1])
ax.set_xticks([0,1])
ax.set_ylabel('$X_{i2}$')
ax.set_ylabel('$%s_{i2}$'%symbol)
ax.set_ylim([0,1])
ax.set_yticks([0,1])
ax.set_title(title)
Expand Down
Binary file removed blogs/why_q_in_mc/iid_uniform_pts.png
Binary file not shown.
Binary file removed blogs/why_q_in_mc/lattice_pts.png
Binary file not shown.
270 changes: 114 additions & 156 deletions demos/MC_vs_QMC.ipynb

Large diffs are not rendered by default.

409 changes: 202 additions & 207 deletions demos/importance_sampling.ipynb

Large diffs are not rendered by default.

198 changes: 96 additions & 102 deletions demos/integration_examples.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"In this demo, we show how to use `qmcpy` for performing numerical multiple integration of two built-in integrands, namely, the Keister function and the Asian put option payoff. To start, we import the `qmcpy` module and the function `arrange()` from `numpy` for generating evenly spaced discrete vectors in the examples."
"In this demo, we show how to use `qmcpy` for performing numerical multiple integration of two built-in integrands, namely, the Keister function and the Asian call option payoff. To start, we import the `qmcpy` module and the function `arrange()` from `numpy` for generating evenly spaced discrete vectors in the examples."
]
},
{
Expand All @@ -36,7 +36,7 @@
" \n",
"* Gaussian true measure: $\\mathcal{N}(0,\\frac{1}{2})$\n",
" \n",
"* Sobol discrete distribution: $x_j \\overset{lds}{\\sim} \\mathcal{U}(0,1)$"
"* Sobol discrete distribution: $x_j \\overset{lLD}{\\sim} \\mathcal{U}(0,1)$"
]
},
{
Expand All @@ -51,36 +51,35 @@
"Solution: 2.1718 \n",
"Keister (Integrand Object)\n",
"Sobol (DiscreteDistribution Object)\n",
"\tdimension 3\n",
"\tscramble 1\n",
"\tseed 7\n",
"\tbackend qrng\n",
"\tmimics StdUniform\n",
"\tgraycode 0\n",
" dimension 3\n",
" randomize 1\n",
" seed 7\n",
" backend qrng\n",
" mimics StdUniform\n",
" graycode 0\n",
"Gaussian (TrueMeasure Object)\n",
"\tdistrib_name Sobol\n",
"\tmean 0\n",
"\tcovariance 0.5000\n",
"CubQmcSobolG (StoppingCriterion Object)\n",
"\tabs_tol 0.0500\n",
"\trel_tol 0\n",
"\tn_init 1024\n",
"\tn_max 34359738368\n",
" mean 0\n",
" covariance 2^(-1)\n",
" decomp_type pca\n",
"CubQMCSobolG (StoppingCriterion Object)\n",
" abs_tol 0.050\n",
" rel_tol 0\n",
" n_init 2^(10)\n",
" n_max 2^(35)\n",
"LDTransformData (AccumulateData Object)\n",
"\tn_total 1024\n",
"\tsolution 2.1718\n",
"\tr_lag 4\n",
"\ttime_integrate 0.0030\n",
"\n"
" n_total 2^(10)\n",
" solution 2.172\n",
" error_hat 0.011\n",
" time_integrate 0.002\n"
]
}
],
"source": [
"dim = 3\n",
"distribution = Sobol(dimension=dim, scramble=True, seed=7, backend='QRNG')\n",
"measure = Gaussian(distribution, covariance=1/2)\n",
"distribution = Sobol(dimension=dim, randomize=True, seed=7, backend='QRNG')\n",
"measure = Gaussian(distribution, covariance=1./2)\n",
"integrand = Keister(measure)\n",
"solution,data = CubQmcSobolG(integrand,abs_tol=.05).integrate()\n",
"solution,data = CubQMCSobolG(integrand,abs_tol=.05).integrate()\n",
"print(data)"
]
},
Expand All @@ -94,11 +93,11 @@
"\n",
"* Stock price at time $t_j := jT/d$ for $j=1,\\dots,d$ is a function of its initial price $S(0)$, interest rate $r$, and volatility $\\sigma$: $S(t_j) = S(0)e^{\\left(r-\\frac{\\sigma^2}{2}\\right)t_j + \\sigma\\mathcal{B}(t_j)}$\n",
"\n",
"* Discounted put option payoff is defined as the difference of a fixed strike price $K$ and the arithmetic average of the underlying stock prices at $d$ discrete time intervals in $[0,T]$: $max \\left(K-\\frac{1}{d}\\sum_{j=1}^{d} S(t_j), 0 \\right) e^{-rT}$\n",
"* Discounted put option payoff is defined as the difference of a fixed strike price $K$ and the arithmetic average of the underlying stock prices at $d$ discrete time intervals in $[0,T]$: $\\max \\left(K-\\frac{1}{d}\\sum_{j=1}^{d} S(t_j), 0 \\right) e^{-rT}$\n",
"\n",
"* Brownian motion true measure: $\\mathcal{B}(t_j) = B(t_{j-1}) + Z_j\\sqrt{t_j-t_{j-1}} \\;$ for $\\;Z_j \\sim \\mathcal{N}(0,1)$\n",
"* Brownian motion true measure: $\\mathcal{B}(t_j) = \\mathcal{B}(t_{j-1}) + Z_j\\sqrt{t_j-t_{j-1}} \\;$ for $\\;Z_j \\sim \\mathcal{N}(0,1)$\n",
"\n",
"* Lattice discrete distribution: $\\:\\: x_j \\overset{lds}{\\sim} \\mathcal{U}(0,1)$"
"* Lattice discrete distribution: $\\:\\: x_j \\overset{LD}{\\sim} \\mathcal{U}(0,1)$"
]
},
{
Expand All @@ -110,50 +109,52 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Solution: 6.2744 \n",
"AsianCall (Integrand Object)\n",
"\tvolatility 0.5000\n",
"\tstart_price 30\n",
"\tstrike_price 25\n",
"\tinterest_rate 0.0100\n",
"\tmean_type arithmetic\n",
"\tdimensions 64\n",
"\tdim_fracs 0\n",
"Lattice (DiscreteDistribution Object)\n",
"\tdimension 64\n",
"\tscramble 1\n",
"\tseed 7\n",
"\tbackend gail\n",
"\tmimics StdUniform\n",
"Solution: 6.2708 \n",
"AsianOption (Integrand Object)\n",
" volatility 2^(-1)\n",
" start_price 30\n",
" strike_price 25\n",
" interest_rate 0.010\n",
" mean_type arithmetic\n",
" dimensions 2^(4)\n",
" dim_fracs 0\n",
"IIDStdUniform (DiscreteDistribution Object)\n",
" dimension 2^(4)\n",
" seed 7\n",
" mimics StdUniform\n",
"BrownianMotion (TrueMeasure Object)\n",
"\tdistrib_name Lattice\n",
"\ttime_vector [ 0.016 0.031 0.047 ... 0.969 0.984 1.000]\n",
"\tmean_shift_is 0\n",
"CubQmcLatticeG (StoppingCriterion Object)\n",
"\tabs_tol 0.0500\n",
"\trel_tol 0\n",
"\tn_init 1024\n",
"\tn_max 34359738368\n",
"LDTransformData (AccumulateData Object)\n",
"\tn_total 4096\n",
"\tsolution 6.2744\n",
"\tr_lag 4\n",
"\ttime_integrate 0.0499\n",
"\n"
" time_vector [0.062 0.125 0.188 ... 0.875 0.938 1. ]\n",
" drift 0\n",
" assembly_type pca\n",
"CubMCCLT (StoppingCriterion Object)\n",
" inflate 1.200\n",
" alpha 0.010\n",
" abs_tol 0.025\n",
" rel_tol 0\n",
" n_init 2^(10)\n",
" n_max 10000000000\n",
"MeanVarData (AccumulateData Object)\n",
" levels 1\n",
" solution 6.271\n",
" n 884808\n",
" n_total 885832\n",
" error_hat 0.025\n",
" confid_int [6.245 6.296]\n",
" time_integrate 1.923\n"
]
}
],
"source": [
"distribution = Lattice(dimension=64, scramble=True, seed=7, backend='GAIL')\n",
"distribution = IIDStdUniform(dimension=16, seed=7)\n",
"measure = BrownianMotion(distribution)\n",
"integrand = AsianCall(\n",
"integrand = AsianOption(\n",
" measure = measure,\n",
" volatility = 0.5,\n",
" start_price = 30,\n",
" strike_price = 25,\n",
" interest_rate = 0.01,\n",
" mean_type = 'arithmetic')\n",
"solution,data = CubQmcLatticeG(integrand, abs_tol=.05).integrate()\n",
"solution,data = CubMCCLT(integrand, abs_tol=.025).integrate()\n",
"print(data)"
]
},
Expand All @@ -163,7 +164,7 @@
"source": [
"## Arithmetic-Mean Asian Put Option: Multi-Level\n",
"\n",
"This example is similar to the last one except that we use Gile's multi-level method for estimation of the option price. The main idea can be summarized as follows:\n",
"This example is similar to the last one except that we use a multi-level method for estimation of the option price. The main idea can be summarized as follows:\n",
"\n",
"$Y_0 = 0$\n",
"\n",
Expand All @@ -176,13 +177,6 @@
"$Z_1 = \\mathbb{E}[Y_1-Y_0] + \\mathbb{E}[Y_2-Y_1] + \\mathbb{E}[Y_3-Y_2] = \\mathbb{E}[Y_3]$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The total run time for this example is about one-third of that for the last example."
]
},
{
"cell_type": "code",
"execution_count": 4,
Expand All @@ -192,52 +186,52 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Solution: 6.2553 \n",
"AsianCall (Integrand Object)\n",
"\tvolatility 0.5000\n",
"\tstart_price 30\n",
"\tstrike_price 25\n",
"\tinterest_rate 0.0100\n",
"\tmean_type arithmetic\n",
"\tdimensions [ 4 16 64]\n",
"\tdim_fracs [ 0.000 4.000 4.000]\n",
"Solution: 6.2734 \n",
"AsianOption (Integrand Object)\n",
" volatility 2^(-1)\n",
" start_price 30\n",
" strike_price 25\n",
" interest_rate 0.010\n",
" mean_type arithmetic\n",
" dimensions [ 4 8 16]\n",
" dim_fracs [0. 2. 2.]\n",
"IIDStdGaussian (DiscreteDistribution Object)\n",
"\tdimension 64\n",
"\tseed 7\n",
"\tmimics StdGaussian\n",
" dimension 2^(4)\n",
" seed 7\n",
" mimics StdGaussian\n",
"BrownianMotion (TrueMeasure Object)\n",
"\tdistrib_name IIDStdGaussian\n",
"\ttime_vector [ 0.016 0.031 0.047 ... 0.969 0.984 1.000]\n",
"\tmean_shift_is 0\n",
"CubMcClt (StoppingCriterion Object)\n",
"\tinflate 1.2000\n",
"\talpha 0.0100\n",
"\tabs_tol 0.0500\n",
"\trel_tol 0\n",
"\tn_init 1024\n",
"\tn_max 10000000000\n",
" time_vector [0.062 0.125 0.188 ... 0.875 0.938 1. ]\n",
" drift 0\n",
" assembly_type pca\n",
"CubMCCLT (StoppingCriterion Object)\n",
" inflate 1.200\n",
" alpha 0.010\n",
" abs_tol 0.025\n",
" rel_tol 0\n",
" n_init 2^(10)\n",
" n_max 10000000000\n",
"MeanVarData (AccumulateData Object)\n",
"\tlevels 3\n",
"\tsolution 6.2553\n",
"\tn [334481 27777 2835]\n",
"\tn_total 368165\n",
"\tconfid_int [ 6.206 6.304]\n",
"\ttime_integrate 0.1106\n",
"\n"
" levels 3\n",
" solution 6.273\n",
" n [1406561. 54316. 17116.]\n",
" n_total 1481065\n",
" error_hat 0.026\n",
" confid_int [6.247 6.3 ]\n",
" time_integrate 0.467\n"
]
}
],
"source": [
"distribution = IIDStdGaussian(seed=7)\n",
"measure = BrownianMotion(distribution)\n",
"integrand = AsianCall(measure,\n",
"integrand = AsianOption(measure,\n",
" volatility = 0.5,\n",
" start_price = 30,\n",
" strike_price = 25,\n",
" interest_rate = 0.01,\n",
" mean_type = 'arithmetic',\n",
" multi_level_dimensions = [4,16,64])\n",
"solution,data = CubMcClt(integrand, abs_tol=.05).integrate()\n",
" multi_level_dimensions = [4,8,16])\n",
"solution,data = CubMCCLT(integrand, abs_tol=.025).integrate()\n",
"print(data)"
]
},
Expand All @@ -251,9 +245,9 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.6.9 64-bit ('qmcpy': virtualenv)",
"display_name": "Python 3",
"language": "python",
"name": "python36964bitqmcpyvirtualenva83e6d2f1717432999066fecf731c68c"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
Expand All @@ -265,7 +259,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 142940e

Please sign in to comment.