human
What is a the Python program you requested given the following:
I am working on the file "FEEG6016 Simulation and Modelling/01-Monte-Carlo-Lab-1.ipynb".
The first task is:
This is a plot of my calculated values of the integral in blue and the actual values in red. This plot however uses the domain -2 to 2 in each dimension but calculated for the same unit hypersphere, the values diverge at higher dimensionality, why?
As dimensionality increases so does the effective hypervolume of the domain by a factor $2^{dimensionality}$, this means that the probability of a random point being inside the hypersphere is proportional to $2^{-dimensionality}$ so as dimensionality is increased the points are much less likely to fall inside the hypersphere and so you need a larger N, number of random points, for the answer to converge.
Now let us repeat this across multiple dimensions.
The errors clearly vary over a range, but the convergence remains roughly as $N^{-1/2}$ independent of the dimension.
using other techniques such as Gauss quadrature would see the points required scaling geometrically with the dimension.
Importance sampling
Consider the integral (which arises, for example, in the theory of Fermi gases)
$$
\begin{equation}
  I = \int_0^1 \frac{x^{-1/2}}{e^x + 1} \, dx.
\end{equation}
$$
This has a finite value, but the integrand diverges as $x \to 0$. This may cause a problem for Monte Carlo integration when a single value may give a spuriously large contribution to the sum.
We can get around this by changing the points at which the integrand is sampled. Choose a weighting function $w(x)$. Then a weighted average of any function $g(x)$ can be
$$
\begin{equation}
  <g>_w = \frac{\int_a^b w(x) g(x) \, dx}{\int_a^b w(x) \, dx}.
\end{equation}
$$
As our integral is
$$
\begin{equation}
  I = \int_a^b f(x) \, dx
\end{equation}
$$
we can, by setting $g(x) = f(x) / w(x)$ get
$$
\begin{equation}
  I = \int_a^b f(x) \, dx = \left< \frac{f(x)}{w(x)} \right>_w \int_a^b w(x) \, dx.
\end{equation}
$$
This gives
$$
\begin{equation}
  I \simeq \frac{1}{N} \sum_{i=1}^N \frac{f(x_i)}{w(x_i)} \int_a^b w(x) \, dx,
\end{equation}
$$
where the points $x_i$ are now chosen from a non-uniform probability...
Can you write Python program for it?
assistant
#Here is the Python program you requested.

def ic_integrate(f, domain_x, N = 10000):
    """
    Mean value Monte Carlo integration: to be completed
    Using w(x)=x**(-0.5)
    """
    import numpy.random
    def w(x):
        return x**(-0.5)
    
    a = domain_x[0]
    b = domain_x[1]
    lower_limit = (a**0.5)
    upper_limit = (b**0.5)
    domain_yrange = upper_limit-lower_limit
    yArray = numpy.random.random(N) * (domain_yrange + lower_limit) #y = unformly distributed random no.s
    xArray = yArray**(2)  #x = random numbers distributed with the probability distribution p(x)=x**(-0.5)/2
    #print(xArray)
    funcOverWeightingArray = f(xArray)/w(xArray)
    sum_ = sum(funcOverWeightingArray)
    #print(sum_, N, domain_x[0], domain_x[1])
    I = (sum_/N)*2*(b**0.5-a**0.5)
    return I

def f_fermi(x):
    return x**(-0.5)/(numpy.exp(x)+1)

ic_integrate(f_fermi, [0,1], N=1000000)