GithubHelp home page GithubHelp logo

jrigh / mc-integration-and-simulations-in-r-and-python Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 1.1 MB

Monte Carlo (MC) Integration and simulations in R and Python

R 6.79% Python 3.19% Jupyter Notebook 90.01%
monte-carlo-integration monte-carlo-simulation python r statistician statistics computational-statistics simulations

mc-integration-and-simulations-in-r-and-python's Introduction

Monte Carlo (MC) Integrations and Simulations

Example of MC integration in R programing.

g = function(x) {sin(x) + cos(x)}
set.seed(2023)
m = 10000
fx = runif(m, 0, pi)

I_hat = ((pi - 0) / m) * sum(g(fx))  
# [1] 2.047382

se_I_hat = (pi / m) * sqrt(sum((g(fx) - I_hat)^2)) 
# [1] 0.05000365

c(I_hat - qnorm(1 - 0.05/2) * se_I_hat, I_hat + qnorm(1 - 0.05/2) * se_I_hat) 
# [1] 1.949377 2.145387

plot1R

Same example this time in Python for similar results.

import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm

def g(x):
  return np.sin(x) + np.cos(x)

np.random.seed(2023)
m = 10000
fx = np.random.uniform(0, np.pi, m)

I_hat = ((np.pi - 0) / m) * np.sum(g(fx))
# 2.0111916600488873

se_I_hat = (np.pi / m) * np.sqrt(np.sum((g(fx) - I_hat)**2))
# 0.049309740888497745

confidence_interval = norm.interval(0.95, loc=I_hat, scale=se_I_hat/np.sqrt(m))
Confidence Interval: [1.9145463438204295, 2.107836976277345]

plot1Py

mc-integration-and-simulations-in-r-and-python's People

Contributors

jrigh avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

chunchi031

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.