2018-03-29 开胃学习数学系列 - 蒙特卡洛

2018-03-30  本文已影响0人  Kaiweio

Lecture 6: Monte Carlo

蒙特卡洛方法就是通过大量的随机样本,去了解一个系统,得到所要计算的值。
获得的值不一定是真值,样本量越大,模拟出来的值越接近。

Topics:

Introduction

Monte Carlo simulation:

A classic example

Compute the value of π using Monte Carlo:

%pylab inline

import pandas as pd
import numpy  as np
import sys
import fmt
def sim_pi(ns) :
    es = np.random.uniform(size=[ns[-1], 2])
    d = np.array([x*x + y*y < 1. for x, y in es])
    
    return np.array([4.*np.sum(d[:n])/n for n in ns])

ns = 4**(np.arange(8))*100
ps = sim_pi(ns)

Population and samples

Sample mean

Sample standard deviation

Variance of sample variance

Monte Carlo error

Std dev of sample std dev

样本标准差的标准差


Population and sample statistics

下面是求圆周率的实例:


μ4的化简没有完全理解
1的概率和就是1。

下面这部分再次对应公式



var_var 使用上述完整的公式, std用了上面表格里近似的公式。
作图说明了随机变量的个数和均值方差/标准差,样本标准差的方差/标准差的关系。

# μ,σ方,μ4
u = np.pi/4
v = u*(1-u)
u4 = np.pi/4*(1-np.pi/4)**4 + (1.-np.pi/4)*(np.pi/4)**4


ns = 2**(np.arange(1, 14)) # ns 是 number of random samples
var_u = v/ns 
var_var = (u4 - v*v*(ns-3)/(ns-1))/ns
# 公式如上

beta = u4/v**2

fig = figure(figsize=[12, 4])
subplot(1, 2, 1)
loglog(ns, np.transpose([var_u, var_var]), '.-')
legend(['var of $\hat \mu$', 'var of $\hat s^2$']);
xlabel('Number of random samples')
title('Sample Variance');

ax = fig.add_subplot(122)
# 了解一下add_subplot 的参数和 subplot一样
loglog(ns, np.transpose([np.sqrt(var_u), np.sqrt((beta-1.)/4./ns)*np.sqrt(v)]), '.-');
title('Error Estimate')
legend(['std of mean', 'std of $\hat s$'], loc='best');
xlabel('Number of random samples');























Estimate mean and volatility

Batching

Why Monte Carlo?



在物理学发展的现阶段,计算高维积分唯一的普遍方法,是Monte Carlo.

当积分区域的维数小于4时,用均匀网格计算积分的方法收敛速度更快,当维数大于4时,Monte Carlo方法的收敛速度更快,而当维数等于4时,两种方法不分高下,. 而计算高维积分时,Monte Carlo 方法是较优的选择。
Monte Carlo 方法 (一)
















Curse of dimensionality

在金融中,我们仅此进入高维领域,每一个随机性来源都是一个单独的维度,every source of randomness is a separate dimension:

产品 维度
利率 LMM模型3M转发高达30Y
篮子选项basket options 往往有几十个名字
合成CDO synthetic CDOs 投资组合中的数百个发行人
抵押证券mortgage securities 每月付款,最多30年

大多数数值方法numerical methods 在高维high dimensionality 上失败

x = np.arange(0, 10)*.1
y = np.arange(0, 10)*.1
xm, ym = np.meshgrid(x, y)

figure(figsize=[12, 4])
subplot(1, 2, 1)
plot(xm, ym, '.');
xlabel('x')
ylabel('y')
title('100 Grid Samples');

subplot(1, 2, 2)
xs = np.random.uniform(size=[2, 100])
plot(xs[0,:], xs[1,:], '.');
title('100 Random Samples');
xlabel('x')
ylabel('y');
image.png

Monte Carlo is effective in combating high dimensions:

ℚ measure applications

XVA adjustments: CVA/FVA/DVA

CVA:当交易对手违约了,我的损失是多少。【条件期望】
DVA:当我违约了,我的对手损失是多少。【条件期望】
衍生品价值=无信用风险衍生品价值-CVA+DVA .

ℙ measure applications

Market Risk
Value at Risk: ℙ[v(T)<VaRα]=𝔼t[1l(v(T)<VaRα)]=α
完全不懂这句话的意思
Counterparty credit risk:
EE (expected exposure): et=𝔼[max(vt,0)]=𝔼[vt1(vt>0)]
完全不懂这句话的意思
PFE (potential future exposure): ℙ[vt1(vt>0)<PFEα]=α
完全不懂这句话的意思
Capital allocation
a new area where the MC method is successfully applied

这个1是什么意思

Advantages of Monte Carlo

Monte Carlo被广泛用于定量金融有很好的理由:





















































相关链接:

中心矩维基
中心矩百度
维基 - 辛普森积分法
油管 - 辛普森积分法

Monte Carlo 方法 (一)
蒙特卡洛算法
这个帖子里面用蒙特卡洛求 e 1 - 2 积分的例子有学习价值

上一篇下一篇

猜你喜欢

热点阅读