Monte Carlo error calculation PYTHON

In summary: You probably want to store the return value from your integrate function in the list.In summary, the conversation is about using the Monte Carlo method to calculate the integral of cos^2(x) using N random numbers and for different values of i. The speaker wants to calculate the error on the integral for each value of i and is working on creating arrays and using numpy functions to store and calculate the values. They are also seeking help in fixing their code.
  • #1
Amy Elliott
1
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
  • #2
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
[/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?
 

FAQ: Monte Carlo error calculation PYTHON

What is Monte Carlo error calculation in PYTHON?

Monte Carlo error calculation is a method used to estimate the error or uncertainty in a numerical result by running multiple random simulations. This technique is commonly used in scientific and mathematical computations, and the results can be improved by increasing the number of simulations.

How does Monte Carlo error calculation work in PYTHON?

In PYTHON, Monte Carlo error calculation involves generating random input data within a given range and running simulations using this data. The results of these simulations are then compared to the expected outcome to calculate the error or uncertainty in the result. This process can be repeated multiple times to improve the accuracy of the estimation.

What are the advantages of using Monte Carlo error calculation in PYTHON?

One major advantage of using Monte Carlo error calculation in PYTHON is that it can handle complex and multidimensional problems that may be difficult to solve analytically. It also provides a more accurate estimation of error compared to traditional methods, especially when dealing with non-linear systems.

Are there any limitations to using Monte Carlo error calculation in PYTHON?

Yes, there are some limitations to using Monte Carlo error calculation in PYTHON. One limitation is that it can be computationally expensive, especially when running a large number of simulations. Additionally, the accuracy of the estimation can be affected by the quality of the random number generator used.

How can I implement Monte Carlo error calculation in PYTHON?

To implement Monte Carlo error calculation in PYTHON, you can use the "random" module to generate random numbers and the "numpy" library to perform the simulations and calculations. There are also various tutorials and code examples available online to help you get started.

Back
Top