In my stochastic processes class, Prof Mike Steele assigned a homework problem to calculate the ruin probabilities for playing a game where you with 1 dollar with probability p and lose 1 dollar with probability 1-p. The probability of winning is not specified, so it can be a biased game. Ruin probabilities are defined to be the probability that in a game you win 10 before losing 10, win 25 before losing 25, and win 50 before losing 50, etc. In total, I found three distinct methods to calculate.

This is a particularly great example to illustrate how to solve a problem using three fundamentally different methods: the first is theoretical calculation, second is simulation to obtain asymptotic values, and third is numerical linear algebra (matrix algorithm) which also gives exact values.


Method 1: First Step Analysis and Direct Computation of Ruin Probabilities

Let h(x) be the probability of winning $n before losing stake of x dollars.

First step analysis gives us a system of three equations: h(0) = 0; h(n) = 1; h(x) = p*h(x+1) + (1-p)*h(x-1).

How to solve this system of equations? We need the "one" trick and the telescoping sequence.

The trick is: (p + (1-p)) * h(x) = h(x) = p*h(x+1) + (1-p)*h(x-1) => p*(h(x+1) - h(x)) = (1-p)*(h(x) - h(x-1)) => h(x+1) - h(x) = (1-p)/p * (h(x)-h(x-1))

Denote h(1) - h(0) = c, which is unknown yet, we have a telescoping sequence: h(1) - h(0) = c; h(2) - h(1) = (1-p)/p * c; h(3) - h(2) = ((1-p)/p)^2 * c ... h(n) - h(n-1) = ((1-p)/p)^(n-1) * c.

Now, add up the telescoping sequence and use the initial conditions, we get: 1 = h(n) = c*(1+ ((1-p)/p) + ((1-p)/p)^2 + ... + ((1-p)/p)^(n-1)) => c = (1 - (1-p)/p) / (1 - ((1-p)/p)^N-1). So h(x) = c * (((1-p)/p) ^ x - 1) / ((1-p)/p)-1) = (((1-p)/p) ^ x - 1) / (((1-p)/p)^N - 1)


Method 2: Monte Carlo Simulation of Ruin Probabilities

The idea is to simulate sample paths from initial stake of x dollars and stop when it either hits 0 or targeted wealth of n.

We can specify the number of trials and get the percentage of trials which eventually hit 0 and which eventuallyhit n. This is important - in fact, I think the essence of Monte Carlo method is to have a huge number of trials to maintain accuracy, and to get a percentage of the number of successful trials in the total number of trials.

In each step of a trial, we need a Bernoulli random variable (as in a coin flip) to increment x by 1 with probability p and -1 with probability 1-p.

In Python this becomes:

from numpy import random
import numpy as np def MC(x,a,p):
  end_wealth = a
  init_wealth = x
  list = []
  for k in range(0, 1000000):
    while x!= end_wealth and x!= 0:
      if np.random.binomial(1,p,1) == 1:
        x += 1
      else:
        x -= 1
    if x == a:
      list.append(1)
    else:
      list.append(0)
  x = init_wealth
  print float(sum(list))/len(list) MC(10,20,0.4932)
MC(25,50,0.4932)
MC(50,100,0.4932)

You can see the result of this simulation by plugging in p = 0.4932 = (18/37)*.5 + .5*.5 = 0.4932, which is the probability of winning the European Roulette with prisoner's rule. As the number of trials get bigger and bigger, the result gets closer and closer to the theoretical value calculated under Method 1.


Method 3: Tridiagonal System

According to wiki, a tridiagonal system has the form of a_i * x_i-1 + b_i * x_i + c_i * x_i+1 = d_i where i's are indices.

It is clear that the ruin problem exactly satisfies this form, i.e.  h(x) := probability of winning n starting from i, h(x) = (1-p)*h(x-1) + p*h(x+1) => -(1-p)*h(x-1) + h(x) -p*h(x+1) = 0, h(0) = 0, h(n) = 1.

And therefore, for the tridiagonal matrix, the main diagonal consists of 1's, and the upper diagonal consists of -(1-p)'s, and the lower diagonal consists of -p's.

In Python this becomes:

import numpy as np
from scipy import sparse
from scipy.sparse.linalg import spsolve n = 100
p = 0.4932
q = 1-p d_main = np.ones(n+1)
d_super = -p * d_main
d_super[1] = 0
d_sub = -q * d_main
d_sub[n-1] = 0 data = [d_sub, d_main, d_super]
print data
A = sparse.spdiags(data, [-1,0,1], n+1, n+1, format='csc') b = np.zeros(n+1)
b[n] = 1
x = spsolve(A, b)
print x

Gambler's Ruin Problem and 3 Solutions的更多相关文章

  1. [Introduction to programming in Java 笔记] 1.3.8 Gambler's ruin simulation 赌徒破产模拟

    赌徒赢得机会有多大? public class Gambler { public static void main(String[] args) { // Run T experiments that ...

  2. 比特币_Bitcoin 简介

    2008-11   Satoshi Nakamoto  Bitcoin: A Peer-to-Peer Electronic Cash System http://p2pbucks.com/?p=99 ...

  3. Bitcoin: A Peer-to-Peer Electronic Cash System

    Bitcoin: A Peer-to-Peer Electronic Cash System Satoshi Nakamoto October 31, 2008 Abstract A purely p ...

  4. Mathematics for Computer Science (Eric Lehman / F Thomson Leighton / Albert R Meyer 著)

    I Proofs1 What is a Proof?2 The Well Ordering Principle3 Logical Formulas4 Mathematical Data Types5 ...

  5. [0x01 用Python讲解数据结构与算法] 关于数据结构和算法还有编程

    忍耐和坚持虽是痛苦的事情,但却能渐渐地为你带来好处. ——奥维德 一.学习目标 · 回顾在计算机科学.编程和问题解决过程中的基本知识: · 理解“抽象”在问题解决过程中的重要作用: · 理解并实现抽象 ...

  6. URAL 1430 Crime and Punishment

    Crime and Punishment Time Limit:500MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

  7. Attention and Augmented Recurrent Neural Networks

    Attention and Augmented Recurrent Neural Networks CHRIS OLAHGoogle Brain SHAN CARTERGoogle Brain Sep ...

  8. Win7 服务优化个人单机版

    我的PC设备比较旧了,为了系统能流畅点,不必要的服务就不开启了.然而,服务那么多,每次重装,都要从头了解一下一边,浪费时间. 个人在网络上收集信息并结合自己的摸索,整理如下,以备查找. 服务名称  显 ...

  9. [转]WIN7服务一些优化方法

    本文转自:http://bbs.cfanclub.net/thread-391985-1-1.html Win7的服务,手动的一般不用管他,有些自动启动的,但对于有些用户来说是完全没用的,可以考虑禁用 ...

随机推荐

  1. Android菜鸟成长记16 -- JSON的解析

    JSON的定义  一种轻量级的数据交换格式,具有良好的可读和便于快速编写的特性.业内主流技术为其提供了完整的解决方案(有点类似于正则表达式 ,获得了当今大部分语言的支持),从而可以在不同平台间进行数据 ...

  2. QuickHit快速击键小程序 --S2.4.5

    我们现在要做一个项目 一个小小的程序 叫做快速击键 很明了的目的 就是在规定时间内,每次出现一组字母的组合,这个字母只能在DFJK中生成 然后输入相应的文字,按回车 自动判断输入的是否正确 在规定时间 ...

  3. less预处理的好处,补充关于之前发表的rem单位的运用于计算

    我认识的less 优点:优雅,好用,简单,可复用性强, 缺点:less并其实不能为我们减少沉余css代码,还是要靠自己的CSS基础去判断哪些是沉余代码或者是可以合并的代码 之前发表的一篇文章一看就懂得 ...

  4. POJ 3624 Charm Bracelet(01背包)

    Charm Bracelet Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 34532   Accepted: 15301 ...

  5. SQL Server 2008 通用分页存储过程

    1.alert USE [数据库名称] GO /****** Object: StoredProcedure [dbo].[dbTab_PagerHelper] Script Date: 08/22/ ...

  6. php开发环境搭建——laravel框架,apache服务器,git版本控制

    本文主要阐述做项目前的开发环境安装——后端为php,前端采用grunt进行自动化构建.具体介绍了windows平台下采用apache运行php的环境搭建,以及git工具安装.写得有点粗糙,但过程完整, ...

  7. python学习笔记-多进程

    multiprocessing from multiprocessing import Process import time def f(name): time.sleep(2) print('he ...

  8. Leetcode Elemination Game

    题目网址:https://leetcode.com/contest/2/problems/elimination-game/ 题意: 给定一个从1到n的数列,第一次从最左边开始,每隔一个淘汰一个数字. ...

  9. IT人 转型

    IT人 转型 转自: http://blog.sina.com.cn/s/blog_88534dff0101232b.html      “35岁,技术生涯即告终结.”这种说法在it界得到众多人认可, ...

  10. Ant: Class not found: javac1.8

    今天用ant,在选择build.xml,run as ant build后出错Ant: Class not found: javac1.8 分析问题:是否是eclipse中的ant版本和java的版本 ...