montecarlo

Different integration results using Monte Carlo vs scipy.integrate.nquad

限于喜欢 提交于 2019-12-07 12:46:17
问题 The MWE below shows two ways of integrating the same 2D kernel density estimate, obtained for this data using the stats.gaussian_kde() function. The integration is performed for all (x, y) below the threshold point (x1, y1) , which defines the upper integration limits (lower integration limits are -infinity ; see MWE). The int1 function uses simple a Monte Carlo approach. The int2 function uses the scipy.integrate.nquad function. The issue is that int1 (ie: the Monte Carlo method) gives

Evaluating Tensorflow operation is very slow in a loop

南楼画角 提交于 2019-12-06 10:14:45
问题 I'm trying to learn tensorflow by coding up some simple problems: I was trying to find the value of pi using a direct sampling Monte Carlo method. The run time is much longer than I thought it would be when using a for loop to do this. I've seen other posts about similar things and I've tried to follow the solutions, but I think I still must be doing something wrong. Attached below is my code: import tensorflow as tf import numpy as np import time n_trials = 50000 tf.reset_default_graph() x =

Directly “plot” line segments to numpy array

做~自己de王妃 提交于 2019-12-06 09:24:57
问题 One of my first projects realized in python does Monte Carlo simulation of stick percolation. The code grew continually. The first part was the visualization of the stick percolation. In an area of width*length a defined density (sticks/area) of straight sticks with a certain length are plotted with random start coordinates and direction. As I often use gnuplot I wrote the generated (x, y) start and end coordinates to a text file to gnuplot them afterwards. I then found here a nice way to

Thread error: can't start new thread

。_饼干妹妹 提交于 2019-12-06 06:34:51
问题 Here's a MWE of a much larger code I'm using. It performs a Monte Carlo integration over a KDE (kernel density estimate) for all values located below a certain threshold (the integration method was suggested over at this question: Integrate 2D kernel density estimate) iteratively for a number of points in a list and returns a list made of these results. import numpy as np from scipy import stats from multiprocessing import Pool import threading # Define KDE integration function. def kde

Binary tree that stores partial sums: Name and existing implementations

落花浮王杯 提交于 2019-12-06 00:42:11
问题 Consider a sequence of n positive real numbers, ( a i ), and its partial sum sequence, ( s i ). Given a number x ∊ (0, s n ], we have to find i such that s i −1 < x ≤ s i . Also we want to be able to change one of the a i ’s without having to update all partial sums. Both can be done in O(log n ) time by using a binary tree with the a i ’s as leaf node values, and the values of the non-leaf nodes being the sum of the values of the respective children. If n is known and fixed, the tree doesn’t

Different integration results using Monte Carlo vs scipy.integrate.nquad

梦想的初衷 提交于 2019-12-05 19:43:54
The MWE below shows two ways of integrating the same 2D kernel density estimate, obtained for this data using the stats.gaussian_kde() function. The integration is performed for all (x, y) below the threshold point (x1, y1) , which defines the upper integration limits (lower integration limits are -infinity ; see MWE). The int1 function uses simple a Monte Carlo approach. The int2 function uses the scipy.integrate.nquad function. The issue is that int1 (ie: the Monte Carlo method) gives systematically larger values for the integral than int2 . I don't know why this happens. Here's an example

Probability density function from a paper, implemented using C++, not working as intended

杀马特。学长 韩版系。学妹 提交于 2019-12-05 07:19:04
So i'm implementing a heuristic algorithm, and i've come across this function. I have an array of 1 to n (0 to n-1 on C, w/e). I want to choose a number of elements i'll copy to another array. Given a parameter y, (0 < y <= 1), i want to have a distribution of numbers whose average is (y * n). That means that whenever i call this function, it gives me a number, between 0 and n, and the average of these numbers is y*n. According to the author, "l" is a random number: 0 < l < n . On my test code its currently generating 0 <= l <= n. And i had the right code, but i'm messing with this for hours

Python Numerical Integration for Volume of Region

好久不见. 提交于 2019-12-05 05:10:48
For a program, I need an algorithm to very quickly compute the volume of a solid. This shape is specified by a function that, given a point P(x,y,z), returns 1 if P is a point of the solid and 0 if P is not a point of the solid. I have tried using numpy using the following test: import numpy from scipy.integrate import * def integrand(x,y,z): if x**2. + y**2. + z**2. <=1.: return 1. else: return 0. g=lambda x: -2. f=lambda x: 2. q=lambda x,y: -2. r=lambda x,y: 2. I=tplquad(integrand,-2.,2.,g,f,q,r) print I but it fails giving me the following errors: Warning (from warnings module): File "C:

Calculating π using a Monte Carlo Simulation limitations

早过忘川 提交于 2019-12-05 04:50:11
问题 I have asked a question very similar to this so I will mention the previous solutions at the end, I have a website that calculates π with the client's CPU while storing it on a server, so far I've got: '701.766.448.388' points inside the circle, and '893.547.800.000' in total, these numbers are calculated using this code. (working example at: https://jsfiddle.net/d47zwvh5/2/) let inside = 0; let size = 500; for (let i = 0; i < iterations; i++) { var Xpos = Math.random() * size; var Ypos =

Finding PI digits using Monte Carlo

廉价感情. 提交于 2019-12-05 03:17:57
问题 I have tried many algorithms for finding π using Monte Carlo. One of the solutions (in Python) is this: def calc_PI(): n_points = 1000000 hits = 0 for i in range(1, n_points): x, y = uniform(0.0, 1.0), uniform(0.0, 1.0) if (x**2 + y**2) <= 1.0: hits += 1 print "Calc2: PI result", 4.0 * float(hits) / n_points The sad part is that even with 1000000000 the precision is VERY bad ( 3.141... ). Is this the maximum precision this method can offer? The reason I choose Monte Carlo was that it's very