Python Monte Carlo error calculation PYTHON

Click For Summary
The discussion focuses on using the Monte Carlo method to calculate the integral of cos^2(x) over the interval from 0 to 2, employing N random numbers where N is defined as 10^i for i ranging from 1 to 6. The user aims to compute the error associated with each integral calculation by taking 100 samples for each value of N and using NumPy to determine the average and standard deviation of the results. The provided code attempts to implement this but contains errors, particularly in how it handles the measurement array and the calculation of average and standard deviation. The measurement list is incorrectly initialized and populated with a string instead of numerical results. Additionally, the results of the average and standard deviation calculations are not stored or printed, leading to incomplete output. Suggestions include correcting the measurement array to store numerical results and ensuring proper usage of NumPy functions for accurate calculations.
Amy Elliott
Messages
1
Reaction score
0
New member warned about posting without the template
I am using the monte carlo method to calculate the integral of cos^2(x). This is done using N random numbers. In my case I am doing it for 10^i random numbers when i ranges from 1-6. I want to calculate the error on the integral for each value of i used.

I know for each value of N I want to take say 100 samples of the integral then using numpy calculate the average and standard deviation for my final result. I will need to use arrays to store the values in. I'm just not sure how to do this. This is my code so far. I know it's wrong in the middle where the correct code needs to go. Thanks in advance!

CODE:

# this is the integral from 0 to 2 of cos(x)^2
# using monte carlo integration
# the errors are also calculated

import numpy.random as rnd;
import numpy as np;

def integrate(n):
return np.sum(pow(np.cos(rnd.uniform(0,1,size=n)),2))/n

for i in range(1,8):
# creating and adding to arrays
measurement=[];# create an empty array or list
measurement.append('intergrate(n)');# add to it
np.average(measurement)
np.std(measurement)
ERROR

print(pow(10,i),":", integrate(pow(10,i)),":", ERROR)
 
Technology news on Phys.org
Amy Elliott said:
I am using the monte carlo method to calculate the integral of cos^2(x). This is done using N random numbers. In my case I am doing it for 10^i random numbers when i ranges from 1-6. I want to calculate the error on the integral for each value of i used.

I know for each value of N I want to take say 100 samples of the integral then using numpy calculate the average and standard deviation for my final result. I will need to use arrays to store the values in. I'm just not sure how to do this. This is my code so far. I know it's wrong in the middle where the correct code needs to go. Thanks in advance!

CODE:
Python:
    # this is the integral from 0 to 2 of cos(x)^2
    # using monte carlo integration
    # the errors are also calculated

    import numpy.random as rnd;
    import numpy as np;

    def integrate(n):
        return np.sum(pow(np.cos(rnd.uniform(0,1,size=n)),2))/n

    for i in range(1,8):
       # creating and adding to arrays
       measurement=[];# create an empty array or list
       measurement.append('intergrate(n)');# add to it
       np.average(measurement)
       np.std(measurement)
       ERROR

      print(pow(10,i),":", integrate(pow(10,i)),":", ERROR)
Please use
Code:
 tags around your code. This makes your code easier to read, and with Python, especially, preserves the indentation that is crucial in Python.
[code=python]
   Your python code

Before getting fancy, with different values for i, try setting i to a specific value and see if you can get some reasonable output. I'm familiar with Python, but not familar with numpy, so can't comment on whether you are calling numpy functions correctly. I suspect that your calls to average and std won't work, as you aren't storing the return values anywhere.

Also, you have measurement as a list. Why are you storing a string 'intergrate(n)' (which BTW is misspelled) in your list?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
4
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
6
Views
3K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
3
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K