Monte Carlo Integration

  1. 找到原函数,再计算
  2. 无法找到原函数,MC积分

Assume that we can generate \(U_1, . . . , U_n \sim Uniform (0, 1)\), and define \(\hat \theta_n = \frac{1}{n} \sum_{i=1}^{n} g(U_i)\)

By law of large numbers, if \(\int_{0}^{1}|g(u)|du < \infty\), we have \(\hat \theta_n \rightarrow \theta := \mathbb{E}g(U) = \int_0^1 g(u)du \quad a.s. \quad n \rightarrow \infty\)

a.s. means almost surely:the set of possible exceptions may be non-empty, but it has probability 0.

bounded interval

Eg. Calculate \(\theta = \int_0^1 x^2 dx\)

n <- 100
set.seed(1)
x <- runif(n)
theta.hat <- mean(x^2)
theta.hat

求:\(\theta = \int_a^{b} g(t) dt\)

利用\(\int_a^bg(t)dt = (b-a)\int_a^b g(t)\frac{1}{b-a}dt = (b-a)\mathbb{E}g(Y)\), where Y~ Uniform(a,b)

所以我们可以:

  1. 先生成\(Y_1,...,Y_n \sim Uniform(a,b)\)
  2. \(\hat \theta_n = (b-a) * \frac{1}{n} \sum_{i=1}^n g(Y_i)\)

Eg. Calculate \(\theta = \int_1^4 x^2dx\)

n <- 100
set.seed(1)
x <- runif(n, min = 1, max = 4)
theta.hat <- (4 - 1) * mean(x^2)
theta.hat

Unbounded Interval

\[\Phi(x) = \int_{-\infty}^x \frac{1}{\sqrt{2\pi}}e^\frac{-t^2}{2}dt
\]

Note that $$\Phi(x) = \mathbb{P}(Z \le x) = \mathbb{E}[I_{Z \le x}], \text{where } Z \sim N(0,1)$$

Therefore, we can estimate

\[\widehat{\Phi(x)}=\frac{1}{n} \sum_{i=1}^n \mathbb{I}\left(Z_i \leq x\right)=\frac{\#\left\{i: Z_i \leq x\right\}}{n}
\]

事件发生的概率等于它的示性函数的期望。

Eg. Calculate \(\Phi(x) = \int_{-\infty}^x \frac{1}{\sqrt{2\pi}}e^\frac{t^2}{2}dt\) (1) when x = 0.3. (2) when \(x = (x_1, ..., x_T)\)

#(1)
x <- 0.3
n <- 100
set.seed(1)
z <- rnorm(n)
mean(z < x) # estimate
pnorm(x) # true value by pnorm() #(2)
Phi.hat <- function(x, n = 1000){
z <- rnorm(n)
return(mean(z < x))
}
x <- seq(0, 2.5, len = 5)
Phi.hat(x) #但是这样有问题,输出只有一个值
##原因是当向量和数进行比较的时候,向量的每一个元素会分别跟数比较;而向量和向量比较时,是对应元素进行比较。Be Careful!
round(pnorm(x), digits = 3) # true value #(2)解决方法
probab <- numeric(5)
for(i in 1:5){
probab[i] <- Phi.hat(x[i])
}
probab

For General Distributions

Calculate \(\theta = \int_A g(x)f(x) dx\)

  1. 先选一个满意的\(f(x)\)
  2. 生成一组\(X_1,...,X_n\) 服从\(f(x)\) (上节课的知识)
  3. 计算\(\hat \theta_n = \frac{1}{n} \sum{i=1}^n g(X_i)\)

Eg. Calculate \(\int_0^{\infty} \frac{e^{-x}}{1+x^2}dx\)

可以把\(e^{-x}\)视作均值为1的指数分布的密度函数,由此可以生成X。

Law of Large Numbers

Let\(\xi_1,\xi_2...\) be a sequence of i.i.d. random variables with finite expected value,by \(\mu\). Let

\[\bar{\xi}_n=\frac{1}{n} \sum_{i=1}^n \xi_i .
\]

Then,

  • Weak law of large numbers
\[\bar{\xi}_n \stackrel{P}{\rightarrow} \mu \text { as } n \rightarrow \infty .
\]
  • Strong law of large numbers)
\[\bar{\xi}_n \stackrel{\text { a.s. }}{\longrightarrow} \mu \text { as } n \rightarrow \infty .
\]

Weak law of large numbers

Definition (Convergence in probability)

  • Let \(\left(\bar{\xi}_n\right)_{n \geq 1}\) be a sequence of real-valued random variables defined on a probability space \((\Omega, \mathcal{F}, P)\).
  • For any positive number \(\varepsilon\),
\[\lim _{n \rightarrow \infty} P\left(\left\{\omega \in \Omega:\left|\bar{\xi}_n(\omega)-\mu\right|>\varepsilon\right\}\right)=0 .
\]
  • Then, we say \(\bar{\xi}_n\) converges in probability to \(\mu\), denoted by
\[\bar{\xi}_n \stackrel{P}{\longrightarrow} \mu .
\]

证明见老师ppt。

M <- 100000
n <- 100
eps <- 0.1
xi.bar <- numeric(M) # xi.bar[i] will record the sample mean of trial i
for (i in 1:M){
set.seed(i)
xi.vec <- sample(1:6, size = n, replace = TRUE)
xi.bar[i] <- mean(xi.vec)
}
mu <- 3.5
mean(abs(xi.bar - mu) > eps)

注意这里只跟n有关,m是试验数量。

如何从

Standard Error

估计这种积分形式的标准差。

如果能用数值解,就用数值解法。不知道分布的时候,可以用MC来估计。

\[\theta = \int_{-\infty}^{\infty} g(x)f(x)dx
\]

where \(f(x)\)is a p.d.f.

\[\hat \theta_n = \frac{1}{n}\sum_{i = 1}^ng(X_i)
\]

, where \(X_i \sim f(x)\)

\[\mathbb{E}[\hat\theta_n] = \theta
\]

现求\(Var(\hat\theta_n)\)

standard error:标准误差

  • The variance can be calculated by
\[\operatorname{Var}\left(\hat{\theta}_n\right)=\frac{\sigma^2}{n}=:(\text { standard error of } \hat{\theta})^2=\left(\operatorname{se}\left(\hat{\theta}_n\right)\right)^2,
\]

where

\[\sigma^2=\operatorname{Var}(g(X))=\int_{-\infty}^{\infty}(g(x)-\theta)^2 f(x) d x .
\]
  • However, \(\sigma^2\) is not known in general.
  • We can use the estimated variance:
\[\hat{\sigma}_n^2=\frac{1}{n-1} \sum_{i=1}^n\left(g\left(X_i\right)-\hat{\theta}_n\right)^2 .
\]
  • The estimated variance of \(\hat{\theta}_n\) is
\[\widehat{\operatorname{se}}\left(\hat{\theta}_n\right)^2:=\frac{\hat{\sigma}_n^2}{n}=\frac{1}{n(n-1)} \sum_{i=1}^n\left(g\left(X_i\right)-\hat{\theta}_n\right)^2 .
\]

f(x) 是自己取的,{\(X_i\)}是根据f(x)生成的。n是MC取的样本数。

由前面的内容,

\[\hat{\theta}_n=\frac{1}{n} \sum_{i=1}^n g\left(X_i\right) .
\]

Eg. Calculate \(\int_0^1 xdx\)

Choose f(x)=1, then g(x) = x, 然后可以代入公式计算。

Central limit theorems

重复生成如10000次\(\hat \theta_n\)(每一次都要投100次骰子,总的会投很多很多次骰子), 当n足够大时,$$P(\frac{\theta_n-\theta}{\sigma/\sqrt{n}}) \rightarrow \Phi(x)$$

Eg. Variance of Dice Rolling

sigma <- sqrt(35/12)
xi.bar.standardized <- (xi.bar - mu)/(sigma/sqrt(n))
hist(xi.bar.standardized, probability = TRUE)

By CLT(Central Limit Theorem) $$ P(-1.96 \le \frac{\hat \theta_n - \theta}{\sigma/\sqrt{n}}) \le 1.96 \sim 0.95 $$, So,$$ \hat{\theta}_n \in\left[\theta-1.96 \times \frac{\sigma^{\prime}}{\sqrt{n}}, \theta+1.96 \times \frac{\sigma}{\sqrt{n}}\right] . $$

但是\(\sigma\)不一定知道,可以用估计值代替,于是就有了t-分布。

  • As \(\sigma^2\) is unknown in general, people usually use \(\hat{\sigma}^2\) to replace it in practice. In this case,
\[\frac{\hat{\theta}_n-\theta}{\hat{\sigma}_n / \sqrt{n}}=\frac{\hat{\theta}_n-\theta}{\hat{\operatorname{se}}\left(\hat{\theta}_n\right)} \Longrightarrow t_{n-1} .
\]

So, by limit distribution,

\[P\left(\theta-t_{0.025}(n-1) \frac{\hat{\sigma}_n}{\sqrt{n}} \leq \hat{\theta}_n \leq t_{0.025}(n-1) \frac{\hat{\sigma}_n}{\sqrt{n}}\right) \approx 0.05
\]

Eg. Dice Rolling

# student's t-distribution
M <- 100
xi.student <- numeric(M)
for (i in 1:M){
set.seed(i)
xi.vec <- sample(1:6, size = n, replace = TRUE)
xi.student[i] <- (mean(xi.vec) - 3.5) / (sd(xi.vec) / sqrt(n))
}

Note在n越来越大的时候,t分布会和normal越来越像。

M<-100
n<-200
u<-runif(n)
set.seed(1)
g <- numeric(M)
for (i in 1:M){
g[i]<-sin(u[i])^4*exp(-u[i])
theta<-g[i]
}
theta<-theta/M
theta

Variance and Efficiency

Definition (Efficiency)

Let \(\hat{\theta}_1\) and \(\hat{\theta}_2\) be two estimators of \(\theta\). We say \(\hat{\theta}_1\) is more efficient (in a statistical sense) than \(\hat{\theta}_2\) if

\[\frac{\operatorname{Var}\left(\hat{\theta}_1\right)}{\operatorname{Var}\left(\hat{\theta}_2\right)}<1
\]

Remark.

If the variances of estimators \(\hat{\theta}_1\) and \(\hat{\theta}_2\) are unknown, we can estimate efficiency by substituting a sample estimate of the variance for each estimator.

Antithetic variables (对照变量)

如果自己生成两个负相关的变量,然后就可以生成一个variance更小的变量。

  • We can also use
\[\hat{\theta}_{1,2}=\frac{1}{2}\left(\hat{\theta}_1+\hat{\theta}_2\right)
\]

to estimate \(\theta\).

  • Note that
\[\operatorname{Var}\left(\hat{\theta}_{1,2}\right)=\operatorname{Var}\left(\frac{\hat{\theta}_1+\hat{\theta}_2}{2}\right)=\frac{1}{4}\left(\operatorname{Var}\left(\hat{\theta}_1\right)+\operatorname{Var}\left(\hat{\theta}_2\right)+2 \operatorname{Cov}\left(\hat{\theta}_1, \hat{\theta}_2\right)\right) \text {. }
\]
  • If \(\hat{\theta}_1\) and \(\hat{\theta}_2\) are negatively correlated, then
\[\operatorname{Cov}\left(\hat{\theta}_1, \hat{\theta}_2\right)<0 \text {, }
\]

and hence,

\[\operatorname{Var}\left(\hat{\theta}_{1,2}\right) \leq \frac{1}{4}\left(\operatorname{Var}\left(\hat{\theta}_1\right)+\operatorname{Var}\left(\hat{\theta}_2\right)\right)
\]

想要构造\(\hat\theta_1, \hat\theta_2\)

使得他们负相关。下面构造的\(X_i\)和\(\tilde X_i\)就是负相关的。

  • If $$ \hat{\theta}1=\frac{1}{n} \sum^n g\left(X_i\right) $$

    where
\[X_i \sim F_X(x)
\]
  • Assume that \(X_i\) is generated from the inverse transform method, then
\[X_i=F^{-1}\left(U_i\right) \stackrel{d}{=} \tilde{X}_i=F^{-1}\left(1-U_i\right) .
\]
  • If the function \(g\) is a monotone function, then
\[\operatorname{Cov}\left(g\left(X_i\right), g\left(\tilde{X}_i\right)\right) \leq 0
\]
  • Therefore,
\[\hat{\theta}_2=\frac{1}{n} \sum_{i=1}^n g\left(\tilde{X}_i\right)
\]

is also an estimator of \(\theta\)

Eg.\(\int_0^1x^2dx\)

u<-runif(100)
theta1 <-mean(u^2)
theta2<-mean((1-u)^2)
theta<-mean(theta1,theta2)
theta

Eg. Normal distribution function

x <- 1
pnorm(1) n <- 100
V <- runif(100, 0, 1)
V.tilde <- 1 - V theta1 <- mean(exp(-V^2/2)*(1/sqrt(2*pi)))
theta2 <- mean(exp(-V.tilde^2/2)*(1/sqrt(2*pi))) theta <- mean(theta1, theta2)
theta

控制方差的程度和g(x)有关,也就是和f(x)的选择有关,好的时候能控制到90%以下

Control variates

  • Choose $$ c^*=-\frac{\operatorname{Cov}(g(X), f(X))}{\operatorname{Var}(f(X))} $$
  • Therefore, the improved estimator is defined as
\[\hat{\theta}^*=\frac{1}{n} \sum_{i=1}^n g\left(X_i\right)+\hat{c}^* \cdot \frac{1}{n} \sum_{i=1}^n\left(f\left(X_i\right)-\mu\right),
\]

where

\[\hat{c}^*=-\frac{\text { sample covariance }}{\text { sample variance }} \text {. }
\]

Eg. \(\theta = \int_0^1 e^u du\)

u <- runif(100)
gu <- exp(u)
theta.hat <- mean(gu)
fu <- u
mu <- 1/2
sample.cov <- cov(gu,fu)
sample.var <- var(fu) c.star <- -sample.cov / sample.var
theta.star <- mean(gu) + c.star*mean(fu-1/2) print(theta.hat)
print(theta.star)

这里取f(u)=u

Simulation-计算统计——Monte Carlo的更多相关文章

  1. 利用蒙特卡洛(Monte Carlo)方法计算π值[ 转载]

    部分转载自:https://blog.csdn.net/daniel960601/article/details/79121055 圆周率π是一个无理数,没有任何一个精确公式能够计算π值,π的计算只能 ...

  2. Monte Carlo计算Pi,python实现

    Monte Carlo import random import matplotlib.pyplot as plt import numpy as np 6 # 函数模拟点的随机掉落,并分为两组 de ...

  3. Markov Chain Monte Carlo Simulation using C# and MathNet

    Math.Net Numerics has capability to conduct Markov Chair Monte Carlo simulations, yet the document i ...

  4. 蒙特卡罗(Monte Carlo)方法简介

    蒙特卡罗(Monte Carlo)方法,也称为计算机随机模拟方法,是一种基于"随机数"的计算方法. 二 解决问题的基本思路 Monte Carlo方法的基本思想很早以前就被人们所发 ...

  5. Monte carlo

    转载 http://blog.sciencenet.cn/blog-324394-292355.html 蒙特卡罗(Monte Carlo)方法,也称为计算机随机模拟方法,是一种基于"随机数 ...

  6. 蒙特卡罗方法、蒙特卡洛树搜索(Monte Carlo Tree Search,MCTS)初探

    1. 蒙特卡罗方法(Monte Carlo method) 0x1:从布丰投针实验说起 - 只要实验次数够多,我就能直到上帝的意图 18世纪,布丰提出以下问题:设我们有一个以平行且等距木纹铺成的地板( ...

  7. Monte Carlo方法简介(转载)

    Monte Carlo方法简介(转载)       今天向大家介绍一下我现在主要做的这个东东. Monte Carlo方法又称为随机抽样技巧或统计实验方法,属于计算数学的一个分支,它是在上世纪四十年代 ...

  8. 增强学习(四) ----- 蒙特卡罗方法(Monte Carlo Methods)

    1. 蒙特卡罗方法的基本思想 蒙特卡罗方法又叫统计模拟方法,它使用随机数(或伪随机数)来解决计算的问题,是一类重要的数值计算方法.该方法的名字来源于世界著名的赌城蒙特卡罗,而蒙特卡罗方法正是以概率为基 ...

  9. Monte Carlo Approximations

    准备总结几篇关于 Markov Chain Monte Carlo 的笔记. 本系列笔记主要译自A Gentle Introduction to Markov Chain Monte Carlo (M ...

  10. (转)Monte Carlo method 蒙特卡洛方法

    转载自:维基百科  蒙特卡洛方法 https://zh.wikipedia.org/wiki/%E8%92%99%E5%9C%B0%E5%8D%A1%E7%BE%85%E6%96%B9%E6%B3%9 ...

随机推荐

  1. windows系统错误代码

    0  操作成功完成.  1  功能错误.  2  系统找不到指定的文件.  3  系统找不到指定的路径.  4  系统无法打开文件.  5  拒绝访问.  6  句柄无效.  7  存储控制块被损坏. ...

  2. 常用的typedef 定义

    今天开始学习VC++基础,系统编程栏目下都是WinAPI和MFC的内容,此为浏览博客园时学习的一篇文章,觉得很实用,拿来做笔记. 出处见最底部. 三行代码:     typedef char CHAR ...

  3. win10edge浏览器个人账户退出登录后再次登录自动登录问题

    edge浏览器退出登录后,再次点击登录以同步数据会自动登录,可查看书签等个人数据 解决方法: 先在浏览器里面退出账户. 1.设置--电子邮件和账户--管理 2.登录后--安全--安全仪表板--高级安全 ...

  4. C# 将实体转xml/xml转实体

    xml转实体 /// <summary> /// 把xml转换成实体 /// </summary> /// <typeparam name="T"&g ...

  5. CSS pointer-events 属性

    pointer-events 属性用于设置元素是否对鼠标事件做出反应. CSS 语法 pointer-events: auto|none; 属性值 属性值 描述 auto 默认值,设置该属性链接可以正 ...

  6. rosetta Resfile语法和约束

    介绍 参考:https://www.rosettacommons.org/docs/latest/rosetta_basics/file_types/resfiles resfile包含输入到Pack ...

  7. Linux的文件权限管理

    Linux文件权限管理介绍 一:Ubuntu 简介 1 .什么是Ubuntu Ubuntu是基于Debian开发的一个开源的Linux操作系统,Ubuntu这个名字名称来⾃⾮洲南部某种语言的一个词语, ...

  8. MySQL学习(十一)B树与B+树了解

    参考博客:https://www.cnblogs.com/kismetv/p/11582214.html

  9. 同步协程的必备工具: WaitGroup

    1. 简介 本文将介绍 Go 语言中的 WaitGroup 并发原语,包括 WaitGroup 的基本使用方法.实现原理.使用注意事项以及常见的使用方式.能够更好地理解和应用 WaitGroup 来协 ...

  10. Little Snitch 5 - Mac 老牌防火墙安全工具软件小飞贼,监控和组织特定软件的网络连接

    一旦连接到Internet,应用程序就可以随时随地发送它们想要的任何东西.通常情况下,他们是为你做的.但有时,例如在跟踪软件.木马或其他恶意软件的情况下,它们不会.但你不会注意到任何东西,因为所有这些 ...