# -*- coding: utf-8 -*- """ Created on Fri Oct 2 12:48:44 2020 @author: jbobowsk """ # This tutorial will use Monte Carlo methods to calculate the volume of an # n-dimensional sphere. import numpy as np import matplotlib.pyplot as plt # In all Monte Carlo simulations it is necessary to generate random or # pseudo-random numbers. The Python command 'np.random.uniform()' # from the NumPy module generates random numbers uniformly distributed # between zero and one. print(np.random.uniform()) # This tutorial will attempt to numerically determine the volume of a sphere # of arbitrary dimension using the Hit & Miss method. This is equivalent to # evaluating a d-dimensional integral. In d-dimensions, a hypersphere is # defined by x1^2+x^2+x3^3+...+xd^2 < R^2. # To find the volume of a sphere we'll generate random numbers over some # "square" volume and count how many land inside the sphere. The # probability of the randomly generated point landing within the sphere is # simply p = Vsphere / Vsquare. If we use spheres of radius 1, then the # volume of the sqaure that contains the sphere is 2^d where d is the # number of dimensions that we're working with. The Monte Carlo simulation # is used to determine p = Zn/n where n is the number of trials and Zn is # the number of "Hits". Collecting all of these results we have # p = Zn/n = Vsphere/2^d or Vsphere = 2^d*(Zn/n). # We'll start with the trivial example of a 1-D sphere. In 1-D, x1^2