discSpec.py
def GridDensity(x, px, N):

	"""#
	#  PROGRAM: GridDensity(input)
	#
	#	Takes in a PDF or density function, and spits out a bunch of points in
	#       accordance with the PDF
	#
	#  Input:
	#       x  = vector of points. It need *not* be equispaced,
	#       px = vector of same size as x: probability distribution or
	#            density function. It need not be normalized but has to be positive.
	#  	    N  = Number of points >= 3. The end points of "x" are included
	#  	     necessarily,
	# 
	#  Output:
	#       z  = Points distributed according to the density
	#       hz = width of the "intervals" - useful to apportion domain to points
	#            if you are doing quadrature with the results, for example.
	#
	#  (c) Sachin Shanbhag, November 11, 2015
	#"""

	npts = 100;                              # can potentially change
	xi   = np.linspace(min(x),max(x),npts)   # reinterpolate on equi-spaced axis
	fint = interp1d(x,px,'cubic')	         # smoothen using cubic splines
	pint = fint(xi)        					 # interpolation
	ci   = cumtrapz(pint, xi, initial=0)                
	pint = pint/ci[npts-1]
	ci   = ci/ci[npts-1]                     # normalize ci

	alfa = 1./(N-1)                          # alfa/2 + (N-1)*alfa + alfa/2
	zij  = np.zeros(N+1)                     # quadrature interval end marker
	z    = np.zeros(N)                       # quadrature point

	z[0]    = min(x);  
	z[N-1]  = max(x); 

	# ci(Z_j,j+1) = (j - 0.5) * alfa
	beta       = np.arange(0.5, N-0.5) * alfa
	zij[0]     = z[0]
	zij[N]     = z[N-1]
	fint       = interp1d(ci, xi, 'cubic')
	zij[1:N]   = fint(beta)
	h          = np.diff(zij)

	# Quadrature points are not the centroids, but rather the center of masses
	# of the quadrature intervals
	beta     = np.arange(1, N-1) * alfa
	z[1:N-1] = fint(beta)

	return z, h

	#  PROGRAM: GridDensity(input)
	#
	#	Takes in a PDF or density function, and spits out a bunch of points in
	#       accordance with the PDF
	#
	#  Input:
	#       x  = vector of points. It need *not* be equispaced,
	#       px = vector of same size as x: probability distribution or
	#            density function. It need not be normalized but has to be positive.
	#  	    N  = Number of points >= 3. The end points of "x" are included
	#  	     necessarily,
	# 
	#  Output:
	#       z  = Points distributed according to the density
	#       hz = width of the "intervals" - useful to apportion domain to points
	#            if you are doing quadrature with the results, for example.
	#
	#  (c) Sachin Shanbhag, November 11, 2015
	
GridDensity(input)
Takes in a PDF or density function, and spits out a bunch of points in
accordance with the PDF

x  = vector of points. It need *not* be equispaced,
px = vector of same size as x: probability distribution or
density function. It need not be normalized but has to be positive.
N  = Number of points >= 3. The end points of "x" are included
necessarily.

z  = Points distributed according to the density
hz = width of the "intervals" - useful to apportion domain to points
if you are doing quadrature with the results, for example.

(c) Sachin Shanbhag, November 11, 2015
GridDensity ( input ) Takes in a PDF or density function and spits out a bunch of points in accordance with the PDF x = vector of points . It need * not * be equispaced px = vector of same size as x : probability distribution or density function . It need not be normalized but has to be positive . N = Number of points > = 3 . The end points of " x " are included necessarily . z = Points distributed according to the density hz = width of the " intervals " - useful to apportion domain to points if you are doing quadrature with the results for example . ( c ) Sachin Shanbhag November 11 2015
def GridDensity(x, px, N):
	npts = 100;                              
	xi   = np.linspace(min(x),max(x),npts)   
	fint = interp1d(x,px,'cubic')	         
	pint = fint(xi)        					 
	ci   = cumtrapz(pint, xi, initial=0)                
	pint = pint/ci[npts-1]
	ci   = ci/ci[npts-1]                     
	alfa = 1./(N-1)                          
	zij  = np.zeros(N+1)                     
	z    = np.zeros(N)                       
	z[0]    = min(x);  
	z[N-1]  = max(x); 
	beta       = np.arange(0.5, N-0.5) * alfa
	zij[0]     = z[0]
	zij[N]     = z[N-1]
	fint       = interp1d(ci, xi, 'cubic')
	zij[1:N]   = fint(beta)
	h          = np.diff(zij)
	beta     = np.arange(1, N-1) * alfa
	z[1:N-1] = fint(beta)
	return z, h
def GridDensity ( x , px , N ) : npts = 100 ; xi = np . linspace ( min ( x ) , max ( x ) , npts ) fint = interp1d ( x , px , 'cubic' ) pint = fint ( xi ) ci = cumtrapz ( pint , xi , initial = 0 ) pint = pint / ci [ npts - 1 ] ci = ci / ci [ npts - 1 ] alfa = 1. / ( N - 1 ) zij = np . zeros ( N + 1 ) z = np . zeros ( N ) z [ 0 ] = min ( x ) ; z [ N - 1 ] = max ( x ) ; beta = np . arange ( 0.5 , N - 0.5 ) * alfa zij [ 0 ] = z [ 0 ] zij [ N ] = z [ N - 1 ] fint = interp1d ( ci , xi , 'cubic' ) zij [ 1 : N ] = fint ( beta ) h = np . diff ( zij ) beta = np . arange ( 1 , N - 1 ) * alfa z [ 1 : N - 1 ] = fint ( beta ) return z , h
PROGRAM: GridDensity(input)
       Takes in a PDF or density function, and spits out a bunch of points in
       accordance with the PDF
PROGRAM : GridDensity ( input ) Takes in a PDF or density function and spits out a bunch of points in accordance with the PDF
"""#
	#  PROGRAM: GridDensity(input)
	#
	#	Takes in a PDF or density function, and spits out a bunch of points in
	#       accordance with the PDF
	#
	#  Input:
	#       x  = vector of points. It need *not* be equispaced,
	#       px = vector of same size as x: probability distribution or
	#            density function. It need not be normalized but has to be positive.
	#  	    N  = Number of points >= 3. The end points of "x" are included
	#  	     necessarily,
	# 
	#  Output:
	#       z  = Points distributed according to the density
	#       hz = width of the "intervals" - useful to apportion domain to points
	#            if you are doing quadrature with the results, for example.
	#
	#  (c) Sachin Shanbhag, November 11, 2015
	#""" # can potentially change # reinterpolate on equi-spaced axis # smoothen using cubic splines # interpolation # normalize ci # alfa/2 + (N-1)*alfa + alfa/2 # quadrature interval end marker # quadrature point # ci(Z_j,j+1) = (j - 0.5) * alfa # Quadrature points are not the centroids, but rather the center of masses # of the quadrature intervals
x
px
N