- Introduction to Python
- Getting started with Python and the IPython notebook
- Functions are first class objects
- Data science is OSEMN
- Working with text
- Preprocessing text data
- Working with structured data
- Using SQLite3
- Using HDF5
- Using numpy
- Using Pandas
- Computational problems in statistics
- Computer numbers and mathematics
- Algorithmic complexity
- Linear Algebra and Linear Systems
- Linear Algebra and Matrix Decompositions
- Change of Basis
- Optimization and Non-linear Methods
- Practical Optimizatio Routines
- Finding roots
- Optimization Primer
- Using scipy.optimize
- Gradient deescent
- Newton’s method and variants
- Constrained optimization
- Curve fitting
- Finding paraemeters for ODE models
- Optimization of graph node placement
- Optimization of standard statistical models
- Fitting ODEs with the Levenberg–Marquardt algorithm
- 1D example
- 2D example
- Algorithms for Optimization and Root Finding for Multivariate Problems
- Expectation Maximizatio (EM) Algorithm
- Monte Carlo Methods
- Resampling methods
- Resampling
- Simulations
- Setting the random seed
- Sampling with and without replacement
- Calculation of Cook’s distance
- Permutation resampling
- Design of simulation experiments
- Example: Simulations to estimate power
- Check with R
- Estimating the CDF
- Estimating the PDF
- Kernel density estimation
- Multivariate kerndel density estimation
- Markov Chain Monte Carlo (MCMC)
- Using PyMC2
- Using PyMC3
- Using PyStan
- C Crash Course
- Code Optimization
- Using C code in Python
- Using functions from various compiled languages in Python
- Julia and Python
- Converting Python Code to C for speed
- Optimization bake-off
- Writing Parallel Code
- Massively parallel programming with GPUs
- Writing CUDA in C
- Distributed computing for Big Data
- Hadoop MapReduce on AWS EMR with mrjob
- Spark on a local mahcine using 4 nodes
- Modules and Packaging
- Tour of the Jupyter (IPython3) notebook
- Polyglot programming
- What you should know and learn more about
- Wrapping R libraries with Rpy
Monte Carlo swindles (Variance reduction techniques)
There are several general techiques for variance reduction, someitmes known as Monte Carlo swindles since these metthods improve the accuracy and convergene rate of Monte Carlo integration without increasing the number of Monte Carlo samples. Some Monte Carlo swindles are:
- importance sampling
- stratified sampling
- control variates
- antithetic variates
- conditioning swindles including Rao-Blackwellization and independent variance decomposition
Most of these techniques are not particularly computational in nature, so we will not cover them in the course. I expect you will learn them elsewhere. Indepedence sampling will be shown as an example of a Monte Carlo swindle.
Variance reduction by change of variables
The Cauchy distribution is given by
\[\begin{split}f(x) = \frac{1}{\pi (1 + x^2)}, \ \ -\infty < x < \infty\end{split}\]
Suppose we want to integrate the tail probability \(P(X > 3)\) using Monte Carlo
h_true = 1 - stats.cauchy().cdf(3) h_true
Direct Monte Carlo integration is inefficient since only 10% of the samples give inforrmation about the tail
n = 100 x = stats.cauchy().rvs(n) h_mc = 1.0/n * np.sum(x > 3) h_mc, np.abs(h_mc - h_true)/h_true
We are trying to estimate the quantity
\[\int_3^\infty \frac{1}{\pi (1 + x^2)} dx\]
Using the substitution \(y = 3/x\) (and a little algebra), we get
\[\int_0^1 \frac{3}{\pi(9 + y^2)} dy\]
Hence, a much more efficient MC estimator is
\[\frac{1}{n} \sum_{i=1}^n \frac{3}{\pi(9 + y_i^2)}\]
where \(y_i \sim \mathcal{U}(0, 1)\).
y = stats.uniform().rvs(n) h_cv = 1.0/n * np.sum(3.0/(np.pi * (9 + y**2))) h_cv, np.abs(h_cv - h_true)/h_true
Importance sampling
Basic Monte Carlo sampling evaluates
\[E[h(X)] = \int_X h(x) f(x) dx\]
Using another distribution \(g(x)\) - the so-called “importance function”, we can rewrite the above expression
\[E_f[h(x)] \ = \ \int_X h(x) \frac{f(x)}{g(x)} g(x) dx \ = \ E_g\left[ \frac{h(X) f(X)}{g(X)} \right]\]
giving us the new estimator
\[\bar{h_n} = \frac{1}{n} \sum_{i=1}^n \frac{f(x_i)}{g(x_i)} h(x_i)\]
where \(x_i \sim g\) is a draw from the density \(g\).
Conceptually, what the likelihood ratio \(f(x_i)/g(x_i)\) provides an indicator of how “important” the sample \(h(x_i)\) is for estmating \(\bar{h_n}\). This is very dependent on a good choice for the importance function \(g\). Two simple choices for \(g\) are scaling
\[g(x) = \frac{1}{a} f(x/a)\]
and translation
\[g(x) = f(x - a)\]
Alternatlvely, a different distribtuion can be chosen as shown in the example below.
Example
Suppose we want to estimate the tail probability of \(\mathcal{N}(0, 1)\) for \(P(X > 5)\). Regular MC integration using samples from \(\mathcal{N}(0, 1)\) is hopeless since nearly all samples will be rejected. However, we can use the exponential density truncated at 5 as the importance function and use importance sampling.
x = np.linspace(4, 10, 100) plt.plot(x, stats.expon(5).pdf(x)) plt.plot(x, stats.norm().pdf(x));
Expected answer
We expect about 3 draws out of 10,000,000 from \(\mathcal{N}(0, 1)\) to have a value greater than 5. Hence simply sampling from \(\mathcal{N}(0, 1)\) is hopelessly inefficient for Monte Carlo integration.
%precision 10
h_true =1 - stats.norm().cdf(5) h_true
Using direct Monte Carlo integration
n = 10000 y = stats.norm().rvs(n) h_mc = 1.0/n * np.sum(y > 5) # estimate and relative error h_mc, np.abs(h_mc - h_true)/h_true
Using importance sampling
n = 10000 y = stats.expon(loc=5).rvs(n) h_is = 1.0/n * np.sum(stats.norm().pdf(y)/stats.expon(loc=5).pdf(y)) # estimate and relative error h_is, np.abs(h_is- h_true)/h_true
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论