Monte Carlo method

The Monte Carlo method is a method of converting the integral of a function into the sum of sampled points so as to reproduce a given function.

[Wikipedia](http://en.wikipedia.org/wiki/%E3%83%A2%E3%83%B3%E3%83%86%E3%82%AB%E3%83%AB%E3%83 This depends on the quality of random number generation, as described in% AD% E6% B3% 95).

The principle of Monte Carlo method

The principle of numerical calculation by the Monte Carlo method is that in the questionnaire, a large number of samples are extracted from the department to which many people belong, and a small number of samples are extracted from the department to which a small number of people belong. It's similar to the idea that it can be reflected in a questionnaire. For example, to calculate pi, generate a set of random numbers in the range of 0 to 1, see it as a point on the xy coordinates, and determine whether it is in the first quadrant of the unit circle or not from that ratio. ..

For random variables that take consecutive values, the probability distribution is represented by the probability density function f (x). At this time, the mean value u and the variance σ ^ 2 are as follows.

\mu = \int{xf(x)dx} \\
\sigma^2 = \int{(x - \mu)}^2f(x)dx

Actually try with code

There were two sports teams A and B. The two sports teams scored exactly the same, but with different variances. This is expressed by Monte Carlo simulation.

from pylab import *
from scipy.stats import *
import matplotlib.pyplot as plt

runs = 10000

#Results of each team
teamperfA = [0.9,0.8,0,9] #Team A score
teamperfB = [0.9,0.8,0.9] #Team B score

#Dispersion of results by team
teamvarianceA = [0.03, 0.04, 0.02]
teamvarianceB = [0.05, 0.08, 0.09] #Team B has higher variance

weights = [1.0, 0.95, 0.8]

def result(perf,variance,weights):
    res = 0.0
    for i in range(len(perf)-1):
        res += perf[i] * weights[i] * norm(1,variance[i]).rvs()

    return res

resultsA = zeros(shape=(runs,), dtype=float)
resultsB = zeros(shape=(runs,), dtype=float)

for i in range(runs):
    resultsA[i] = result (teamperfA,teamvarianceA,weights)
    resultsB[i] = result (teamperfB,teamvarianceB,weights)

subplot(211)
width = 2
height=runs

title('Team A')
hist(resultsA, bins=50)
axis([1.4,1.9,0,height/10])

subplot(212)
title('Team B')
hist(resultsB, bins=50)

axis([1.4,1.9,0,height/10])

show()
plt.savefig("image.png ")

image.png

reference

Python matplotlib, Monte Carlo simulation, and basic statistics http://softwaredevelopmentperestroika.wordpress.com/2013/12/06/python-matplotlib-monte-carlo-simulation-and-basic-statistics/

Recommended Posts

Monte Carlo method
Introduction to Monte Carlo Method
Simulate Monte Carlo method in Python
Estimating π by Monte Carlo method
Dominion compression play analyzed by Monte Carlo method
#Monte Carlo method to find pi using Python
Try implementing the Monte Carlo method in Python
Speed comparison of each language by Monte Carlo method
Increase the speed of the Monte Carlo method of Cython cut-out implementation.
Sprinkle rice grains to find the circumference (Monte Carlo method)
Finding pi with a three-line function [Python / Monte Carlo method]
Find the ratio of the area of Lake Biwa by the Monte Carlo method
Manim's method 7
Manim's method 13
Manim's method 2
Manim's method 18
Manim's method 17
Manim's method 5
Manim's method 3
Manim's method 15
Manim's method 11
Manim's method 16
Manim's method 20
Binary method
Manim's method 10
Manim's method 9
Manim's method 6
Manim's method 21
Manim's method 4
Manim's method 14
Manim's method 22
Manim's method 19
Manim's method 12
Gomoku AI by Monte Carlo tree search
Special method
Special method
[Statistics] Let's explain sampling by Markov chain Monte Carlo method (MCMC) with animation.
PRML Chapter 11 Markov Chain Monte Carlo Python Implementation
Markov chain and Monte Carlo methods are summarized