Generate random samples following the 2D distribution of input data.
It is often needed to generate random values following a 2D sampling distribution. This is not straightforward when the two values are correlated. For example, I have a sample of 100 sources with redshifts and fluxes (higher redshift, lower flux). For Monte-Carlo simulation, I need to draw 10000 random sources from this population, recovering the redshift-flux joint distribution. "2Drandom.py" realizes this function with the aid of Voronoi tessellation.
Using an extension function in "SweepLine.py", the 2D space can be divided into pieces of triangles, each of which is composed of one Voronoi edge and one Voronoi site. The probability (weight) of a random point falling in each triangle is proportional to the number of data points in it, that is, 1 × triangle area / cell area (It is < 1). To generate a desired random position, "2Drandom.py" first select randomly one from all the triangles considering their weights, and then distribute a point randomly inside the triangle.
2Drandom.py inputfile [number] [output] [--border xlow,xhigh,ylow,yhigh] inputfile: 2D data in the first two columns number: default 100 output: file name can not be a number --border xlow,xhigh,ylow,yhigh: Set image border. Any one of the four can be empty.
File "AGN.dat" contains redshifts and photon counts of 327 AGNs.
unix$ 2Drandom.py AGN.dat 1000 --border 0,7.5,1.5,5 Voronoi Construction: AGN image size: 0.0-7.5 1.5-5.0 >> AGN_Triangles.dat EdgePoints removed in Amap 1000 random points saved in AGN_2Drandom.dat
Distribution of the 327 sources
Divide the space into triangles.
1000 random positions generated.