I need to generate a vector sampled uniformly with 10 directions (a collection of 10 random numbers) which lies over a unit sphere. So, the sum of the squares of the 10 values should be 1.
This is the exact question for which I need to generate those points:
Implement the Perceptron algorithm and run it on the following
synthetic data sets in ℝ10: pick 𝑤∗ = [1,0,0,…,0]; generate 1000
points 𝑥 by sampling uniformly at random over the unit sphere and
then removing those that have margin 𝛾 smaller than 0.1; generate
label 𝑦 = sign((𝑤∗)T𝑥).
There is a math theorem saying that if X = (X1,...,XN) is a vector with Xi the standard normal distribution, then X/NORM(X) is uniform in the unit sphere, where NORM is the euclidean norm. So you have to sample 10 points from a standard normal distribution (using numpy?) and then normalize the result.
As #Andrex suggested, here is the right solution:
import numpy as np
import math
s = np.random.normal(0, 1, 10)
norm=math.sqrt(sum(s*s))
result=s/norm
where result is the answer. You can evaluate the result:
sum([x*x for x in result])
1.0
(Working in 2d for simplicity) I know that the force exerted on two spherical bodies by each other due to gravity is
G(m1*m2/r**2)
However, for a non-spherical object, I cannot find an algorithm or formula that is able to calculate the same force. My initial thought was to pack circles into the object so that the force by gravity would be equal to the sum of the forces by each of the circles. E.g (pseudocode),
def gravity(pos1,shape):
circles = packCircles(shape.points)
force = 0
for each circle in circles:
distance = distanceTo(pos1,circle.pos)
force += newtonForce(distance,shape.mass,1) #1 mass of observer
return force
Would this be a viable solution? If so, how would I pack circles efficiently and quickly? If not, is there a better solution?
Edit: Notice how I want to find the force of the object at a specific point, so angles between the circle and observer will need to be calculated (and vectors summed). It is different from finding the total force exerted.
Background
Some of this explanation will be somewhat off-topic but I think it is necessary to help clarify some of the things brought up in the comments and because much of this is somewhat counterintuitive.
This explanation of gravitational interactions depends on the concept of point masses. Suppose you have two point masses which are in an isolated system separated from each other by some distance, r1, with masses of m1 and m2 respectively,
The gravitational field created by m1 is given by
where G is the universal gravitational constant, r is the distance from m1 and r̂ is the unit direction along the line between m1 and m2.
The gravitational force exerted on m2 by this field is given by
Note - Importantly, this is true for any two point masses at any distance.1
The field nature of gravitational interactions allows us to employ superposition in calculating the net gravitational force due to multiple interactions. Consider if we add another mass, m3 to the previous scenario,
Then the gravitational force on mass m2 is simply a sum of the gravitational force from the fields created by each other mass,
with ri,j = rj,i. This holds for any number of masses at any separations. It also implies that the field created by a collection of masses can be aggregated by a vector sum, if you prefer that formalism.
Now consider if we had a very large number of point masses, M, aggregated together in a continuous, rigid body of uniform density. Then we wanted to calculate the gravitational force on a single spatially distinct point mass, m, due to the aggregate mass, M:
Then instead of considering point masses we can consider areas (or volumes) of mass of differential size and either integrate or sum the effect of these areas (or volumes) on the point mass. In the two dimensional case, the magnitude of the gravitational force is then
where σ is the density of the aggregate mass.2 This is equivalent to summing the gravitational vector field due to each differential mass, σdxdy. Such equivalence is critically important because it implies that for any point mass far enough outside of a mass distribution, the gravitational force due to the mass distribution is almost exactly the same as it would be for a point mass of mass M located at the center of mass of the mass distribution.3 4
This means that, to very good approximation, when it comes to calculating the gravitational field due to any mass distribution, the mass distribution can be replaced with an equivalent-mass point mass at the center of mass of the distribution. This holds for any number of spatially distinct mass distributions, whether those distributions constitute a rigid body or not. Furthermore, it means that you can even aggregate groups of distributions into a single point mass at the center of mass of the system.5 As long as the reference point is far enough away.
However, in order to find the gravitational force on a point mass due to a mass distribution at any point, for any mass distribution in a shape and separation agnostic manner we have to calculate the gravitational field at that point by summing the contributions from each portion of the mass distribution.6
Back to the question
Of course for an arbitrary polygon or polyhedron the analytical solution can be prohibitively difficult, so it is much simpler to use a summation, and algorithmic approaches will similarly use a summation.
Algorithmically speaking, the simplest approach here is not actually geometric packing (with either circles/spheres or squares/cubes). It's not impossible to use packing, but mathematically there are significant challenges to that approach - it is better to employ a method which relies on simpler math. One such approach is to define a grid encompassing the spatial extent of the mass distribution, and then create simple (square/cubic or rectangular/cuboidic) polygons or polyhedrons with the grid points as vertices. This creates three kinds of polygons or polyhedrons:
Those which do not encompass any of the mass distribution
Those which are completely filled by the mass distribution
Those which are partially filled by the mass distribution
Center of Mass - Approach 1
This will work well when the distance from the reference point to the mass distribution is large relative to the angular extent of the distribution, and when there is no geometric enclosure of the reference by the mass distribution (or by any several distributions).
You can then find the center of mass, R of the distribution by summing the contributions from each polygon,
where M is the total mass of the distribution, ri is the spatial vector to the geometric center of the ith polygon, and mi is the density times the portion of the polygon which contains mass (i.e. 1.00 for completely filled polygons and 0.00 for completely empty polygons). As you increase the sampling size (the number of grid points) the approximation for the center of mass will approach the analytical solution. Once you have the center of mass it is trivial to calculate the gravitational field created: you simply place a point mass of mass M at the point R and use the equation from above.
For demonstration, here is an implementation of the described approach in two dimensions in Python using the shapely library for the polygon operations:
import numpy as np
import matplotlib.pyplot as plt
import shapely.geometry as geom
def centerOfMass(r, density = 1.0, n = 100):
theta = np.linspace(0, np.pi*2, len(r))
xy = np.stack([np.cos(theta)*r, np.sin(theta)*r], 1)
mass_dist = geom.Polygon(xy)
x, y = mass_dist.exterior.xy
# Create the grid and populate with polygons
gx, gy = np.meshgrid(np.linspace(min(x), max(x), n), np.linspace(min(y),
max(y), n))
polygons = [geom.Polygon([[gx[i,j], gy[i,j]],
[gx[i,j+1], gy[i,j+1]],
[gx[i+1,j+1],gy[i+1,j+1]],
[gx[i+1,j], gy[i+1,j]],
[gx[i,j], gy[i,j]]])
for i in range(gx.shape[0]-1) for j in range(gx.shape[1]-1)]
# Calculate center of mass
R = np.zeros(2)
M = 0
for p in polygons:
m = (p.intersection(mass_dist).area / p.area) * density
M += m
R += m * np.array([p.centroid.x, p.centroid.y])
return geom.Point(R / M), M
density = 1.0 # kg/m^2
G = 6.67408e-11 # m^3/kgs^2
theta = np.linspace(0, np.pi*2, 100)
r = np.cos(theta*2+np.pi)+5+np.sin(theta)+np.cos(theta*3+np.pi/6)
R, M = centerOfMass(r, density)
m = geom.Point(20, 0)
r_1 = m.distance(R)
m_1 = 5.0 # kg
F = G * (m_1 * M) / r_1**2
rhat = np.array([R.x - m.x, R.y - m.y])
rhat /= (rhat[0]**2 + rhat[1]**2)**0.5
# Draw the mass distribution and force vector, etc
plt.figure(figsize=(12, 6))
plt.axis('off')
plt.plot(np.cos(theta)*r, np.sin(theta)*r, color='k', lw=0.5, linestyle='-')
plt.scatter(m.x, m.y, s=20, color='k')
plt.text(m.x, m.y-1, r'$m$', ha='center')
plt.text(1, -1, r'$M$', ha='center')
plt.quiver([m.x], [m.y], [rhat[0]], [rhat[1]], width=0.004,
scale=0.25, scale_units='xy')
plt.text(m.x - 5, m.y + 1, r'$F = {:.5e}$'.format(F))
plt.scatter(R.x, R.y, color='k')
plt.text(R.x, R.y+0.5, 'Center of Mass', va='bottom', ha='center')
plt.gca().set_aspect('equal')
plt.show()
This approach is a bit overkill: in most cases it would suffice to find the centroid and the area of the polygon multiplied by the density for the center of mass and total mass. However, it would work for even non-uniform mass distributions - that's why I have used it for demonstration.
Field Summation - Approach 2
In many cases this approach is also overkill, especially in comparison to the first approach, but it will provide the best approximation under any distributions (within the classical regime).
The idea here is to sum the effect of each chunk of the mass distribution on a point mass to determine the net gravitational force (based on the premise that the gravitational fields can be independently added)
class pointMass:
def __init__(self, mass, x, y):
self.mass = mass
self.x = x
self.y = y
density = 1.0 # kg/m^2
G = 6.67408e-11 # m^3/kgs^2
def netForce(r, m1, density = 1.0, n = 100):
theta = np.linspace(0, np.pi*2, len(r))
xy = np.stack([np.cos(theta)*r, np.sin(theta)*r], 1)
# Create a shapely polygon for the mass distribution
mass_dist = geom.Polygon(xy)
x, y = mass_dist.exterior.xy
# Create the grid and populate with polygons
gx, gy = np.meshgrid(np.linspace(min(x), max(x), n), np.linspace(min(y),
max(y), n))
polygons = [geom.Polygon([[gx[i,j], gy[i,j]],
[gx[i,j+1], gy[i,j+1]],
[gx[i+1,j+1],gy[i+1,j+1]],
[gx[i+1,j], gy[i+1,j]],
[gx[i,j], gy[i,j]]])
for i in range(gx.shape[0]-1) for j in range(gx.shape[1]-1)]
g = np.zeros(2)
for p in polygons:
m2 = (p.intersection(mass_dist).area / p.area) * density
rhat = np.array([p.centroid.x - m1.x, p.centroid.y - m1.y])
rhat /= (rhat[0]**2 + rhat[1]**2)**0.5
g += m1.mass * m2 / p.centroid.distance(geom.Point(m1.x, m1.y))**2 * rhat
g *= G
return g
theta = np.linspace(0, np.pi*2, 100)
r = np.cos(theta*2+np.pi)+5+np.sin(theta)+np.cos(theta*3+np.pi/6)
m = pointMass(5.0, 20.0, 0.0)
g = netForce(r, m)
plt.figure(figsize=(12, 6))
plt.axis('off')
plt.plot(np.cos(theta)*r, np.sin(theta)*r, color='k', lw=0.5, linestyle='-')
plt.scatter(m.x, m.y, s=20, color='k')
plt.text(m.x, m.y-1, r'$m$', ha='center')
plt.text(1, -1, r'$M$', ha='center')
ghat = g / (g[0]**2 + g[1]**2)**0.5
plt.quiver([m.x], [m.y], [ghat[0]], [ghat[1]], width=0.004,
scale=0.25, scale_units='xy')
plt.text(m.x - 5, m.y + 1, r'$F = ({:0.3e}, {:0.3e})$'.format(g[0], g[1]))
plt.gca().set_aspect('equal')
plt.show()
Which, for the relatively simple test case being used, gives a result which is very close to the first approach:
But while there are cases where the first approach will not work correctly, there are no such cases where the second approach will fail (in the classical regime) so it is advisable to favor this approach.
1This does break down under extremes, e.g. past the event horizon of black holes, or when r approaches the Planck length, but those cases are not the subject of this question.
2This becomes significantly more complex in cases where the density is non-uniform, and there is no trivial analytical solution in cases where the mass distribution can not be described symbolically.
3It should probably be noted that this is effectively what the integral is doing; finding the center of mass.
4For a point mass within a mass distribution Newton's Shell Theorem, or a field summation must be used.
5In astronomy this is called a barycenter, and bodies always orbit the barycenter of the system - not the center of mass of any given body.
6In some cases it is sufficient to use Newton's Shell Theorem, however those cases are not distribution geometry agnostic.
I'm setting up a data set, and the goal is to make a sphere with a normal distribution along the radial direction, and uniform theta and phi distributions. Despite using uniform distributions for theta and phi, I keep getting regions of very dense points at the poles (symmetrically near +/- radius on the z axis) that have a small but noticeable size. I am also writing to a file and using ROOT to plot my results.
I've tried reducing the range of theta and phi to graph half a sphere, but the problem still persists in both cases. I have also set the radius to a constant to make sure that wasn't interfering with the formation of the poles. The poles still formed.
#Size is the number of points to generate, and n is the dimension of the problem (n=2 for circle, n=3 for sphere, etc.)
hs_points = np.zeros((size, n))
for i in range(size):
hs_point = hs_points[i]
for j in range(n):
if j == 0:
# normal distribution on radius
coord = np.random.normal(mu, sigma)
elif j < n-1:
coord=round(random.uniform(0,np.pi),15)
else:
coord = np.random.rand()*(2*np.pi)
hs_point[j] = coord
hs_points[i] = hs_point
return hs_points
c_points = np.zeros((size, n))
# translate each hyperspherical point into a cartesian point
for i in range(size):
hs_point = hs_points[i]
xCoord=hs_point[0]*np.sin(hs_point[1])*np.cos(hs_point[2])
yCoord=hs_point[0]*np.sin(hs_point[1])*np.sin(hs_point[2])
zCoord=hs_point[0]*np.cos(hs_point[1])
c_points[i,0]=xCoord
c_points[i,1]=yCoord
c_points[i,2]=zCoord
I expect the program to output a "fuzzy" sphere with a normal distribution along the radial direction, but with uniform behavior everywhere else (i.e. no particularly dense regions). Actual behavior is the generation of dense poles with uniform behavior everywhere else.
I've just started using Python, so I'm sorry if I ask for trivial things.
I generated two random Gaussian distributions, and I used them to generate a 2D Gaussian distribution. What I'd like to do now is to plot a graph that represents the number of elements within a circumference of the 2D Gaussian distribution, varying the radius of the circumference (reducing it at each step).
You would be so kind to help me solve the problem. Thank you for taking my post into consideration.
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
mu1, sigma1 = 0, 0.1 # mean and standard deviation
s1 = np.random.normal(mu1, sigma1, 10000) # generate N randoomly Gaussian points
mu2, sigma2 = 0.8, 0.3
s2 = np.random.normal(mu2, sigma2, 10000)
#ISTOGRAMMA DI DUE DISTRIBUZIONI GAUSSIANE CON DIFFERENTI SET
plt.figure(1)
plt.title('Histogram of a 2D-Gaussian Distribution')
bins1 = plt.hist(s1, 100)
bins2 = plt.hist(s2, 100)
plt.show()
#DISTRIBUZIONE GAUSSIANA 2D
plt.figure(2)
plt.title('2D-Gaussian Distribution')
bins = plt.hist2d(s1, s2, 100)
cb = plt.colorbar()
cb.set_label('counts in bin')
plt.show()
This should do the trick, assuming that s1 and s2 are the coordinates of some 2D points. If not, the code can be easily changed to match your problem.
First you center the two distributions by subtracting their mean, then you check which of their elements (absolute value) is inside the radius of your circle. You then take a logical and to make sure only to take the points that have both coordinates inside the circle.
radius = 0.1
valid_indexes = np.logical_and(abs(s1 -mu1)<= radius, abs(s2 - mu2) <= radius)
s1_valid = s1[valid_indexes]
s2_valid = s2[valid_indexes]
You have now obtained the points in the distributions that are inside a circle with given radius centered in (mu1, mu2).
[Edit]
As you want to count the number of elements, and not extract them, you can easily do
radius = 0.1
sum(np.logical_and(abs(s1 -mu1)< radius, abs(s2 - mu2) < radius))
[Edit 2]
This plots the number of points for every radius of the circle, starting from limit and reducing it by step until 0
step = 0.025
limit = 1
s1_ca = abs(s1-mu1)
s2_ca = abs(s2-mu2)
points_in_radius = []
radius_values = np.round(np.arange(0, limit, step), 3)[::-1]
for radius in radius_values:
points_in_radius.append(sum(np.logical_and(s1_ca < radius, s2_ca < radius)))
plt.plot(points_in_radius)
plt.xticks(range(len(points_in_radius)), radius_values, rotation=90)
plt.show()
first i center the distributions and take their abs value. then I create the range of radiuses to use and finally I cycle through them and add the result using my above formula.
This is not the most efficient way to do it, but it works.
I need code to do 2D Kernel Density Estimation (KDE), and I've found the SciPy implementation is too slow. So, I've written an FFT based implementation, but several things confuse me. (The FFT implementation also enforces periodic boundary conditions, which is what I want.)
The implementation is based on creating a simple histogram from the samples and then convolving this with a gaussian. Here's code to do this and compare it with the SciPy result.
from numpy import *
from scipy.stats import *
from numpy.fft import *
from matplotlib.pyplot import *
from time import clock
ion()
#PARAMETERS
N = 512 #number of histogram bins; want 2^n for maximum FFT speed?
nSamp = 1000 #number of samples if using the ranom variable
h = 0.1 #width of gaussian
wh = 1.0 #width and height of square domain
#VARIABLES FROM PARAMETERS
rv = uniform(loc=-wh,scale=2*wh) #random variable that can generate samples
xyBnds = linspace(-1.0, 1.0, N+1) #boundaries of histogram bins
xy = (xyBnds[1:] + xyBnds[:-1])/2 #centers of histogram bins
xx, yy = meshgrid(xy,xy)
#DEFINE SAMPLES, TWO OPTIONS
#samples = rv.rvs(size=(nSamp,2))
samples = array([[0.5,0.5],[0.2,0.5],[0.2,0.2]])
#DEFINITIONS FOR FFT IMPLEMENTATION
ker = exp(-(xx**2 + yy**2)/2/h**2)/h/sqrt(2*pi) #Gaussian kernel
fKer = fft2(ker) #DFT of kernel
#FFT IMPLEMENTATION
stime = clock()
#generate normalized histogram. Note sure why .T is needed:
hst = histogram2d(samples[:,0], samples[:,1], bins=xyBnds)[0].T / (xy[-1] - xy[0])**2
#convolve histogram with kernel. Not sure why fftshift is neeed:
KDE1 = fftshift(ifft2(fft2(hst)*fKer))/N
etime = clock()
print "FFT method time:", etime - stime
#DEFINITIONS FOR NON-FFT IMPLEMTATION FROM SCIPY
#points to sample the KDE at, in a form gaussian_kde likes:
grid_coords = append(xx.reshape(-1,1),yy.reshape(-1,1),axis=1)
#NON-FFT IMPLEMTATION FROM SCIPY
stime = clock()
KDEfn = gaussian_kde(samples.T, bw_method=h)
KDE2 = KDEfn(grid_coords.T).reshape((N,N))
etime = clock()
print "SciPy time:", etime - stime
#PLOT FFT IMPLEMENTATION RESULTS
fig = figure()
ax = fig.add_subplot(111, aspect='equal')
c = contour(xy, xy, KDE1.real)
clabel(c)
title("FFT Implementation Results")
#PRINT SCIPY IMPLEMENTATION RESULTS
fig = figure()
ax = fig.add_subplot(111, aspect='equal')
c = contour(xy, xy, KDE2)
clabel(c)
title("SciPy Implementation Results")
There are two sets of samples above. The 1000 random points is for benchmarking and is commented out; the three points are for debugging.
The resulting plots for the latter case are at the end of this post.
Here are my questions:
Can I avoid the .T for the histogram and the fftshift for KDE1? I'm not sure why they're needed, but the gaussians show up in the wrong places without them.
How is the scalar bandwidth defined for SciPy? The gaussians have much different widths in the two implementations.
Along the same lines, why are the gaussians in the SciPy implementation not radially symmetric even though I gave gaussian_kde a scalar bandwidth?
How could I implement the other bandwidth methods available in SciPy for the FFT code?
(Let me note that the FFT code is ~390x fast than the SciPy code in the 1000 random points case.)
The differences you're seeing are due to the bandwidth and scaling factors, as you've already noticed.
By default, gaussian_kde chooses the bandwidth using Scott's rule. Dig into the code, if you're curious about the details. The code snippets below are from something I wrote quite awhile ago to do something similar to what you're doing. (If I remember right, there's an obvious error in that particular version and it really shouldn't use scipy.signal for the convolution, but the bandwidth estimation and normalization are correct.)
# Calculate the covariance matrix (in pixel coords)
cov = np.cov(xyi)
# Scaling factor for bandwidth
scotts_factor = np.power(n, -1.0 / 6) # For 2D
#---- Make the gaussian kernel -------------------------------------------
# First, determine how big the gridded kernel needs to be (2 stdev radius)
# (do we need to convolve with a 5x5 array or a 100x100 array?)
std_devs = np.diag(np.sqrt(cov))
kern_nx, kern_ny = np.round(scotts_factor * 2 * np.pi * std_devs)
# Determine the bandwidth to use for the gaussian kernel
inv_cov = np.linalg.inv(cov * scotts_factor**2)
After the convolution, the grid is then normalized:
# Normalization factor to divide result by so that units are in the same
# units as scipy.stats.kde.gaussian_kde's output. (Sums to 1 over infinity)
norm_factor = 2 * np.pi * cov * scotts_factor**2
norm_factor = np.linalg.det(norm_factor)
norm_factor = n * dx * dy * np.sqrt(norm_factor)
# Normalize the result
grid /= norm_factor
Hopefully that helps clarify things a touch.
As for your other questions:
Can I avoid the .T for the histogram and the fftshift for KDE1? I'm
not sure why they're needed, but the gaussians show up in the wrong
places without them.
I could be misreading your code, but I think you just have the transpose because you're going from point coordinates to index coordinates (i.e. from <x, y> to <y, x>).
Along the same lines, why are the gaussians in the SciPy
implementation not radially symmetric even though I gave gaussian_kde
a scalar bandwidth?
This is because scipy uses the full covariance matrix of the input x, y points to determine the gaussian kernel. Your formula assumes that x and y aren't correlated. gaussian_kde tests for and uses the correlation between x and y in the result.
How could I implement the other bandwidth methods available in SciPy
for the FFT code?
I'll leave that one for you to figure out. :) It's not too hard, though. Basically, instead of scotts_factor, you'd change the formula and have some other scalar factor. Everything else is the same.