[SciPy-User] Trying to understand Distributions

Keith Sloan keith at sloan-home.co.uk
Sat Jul 3 13:17:31 EDT 2021


I have a histogram that I would like to fit a gamma and rayleigh curve 
ideally with a measure of fit.
I am struggling to have the histogram and distribution with the same scales.
I tried to 
followhttps://stackoverflow.com/questions/40979643/python-how-to-fit-a-gamma-distribution-from-data 
<https://github.com/scipy/scipy/issues/url>
and don't really know what I am doing

I would like to plot the histogram and distribution in one figure i.e. 
one for gamma + histogram and another for rayleigh + histogram
and as I said some quantitative measure of the fit.

Thanks

from  astropy.table  import  Table,  join
import  numpy  as  np
import  matplotlib.pyplot  as  plt
from  matplotlib.pyplot  import  plot

RawMassEClassEmeasure  =  Table.read('../../GAMA_Data/REMassEClassEmeasure.fits')
#print(RawMassEClassEmeasure.colnames)
# CLEAN DATA
#REMassEClassEmeasure = 
RawMassEClassEmeasure[RawMassEClassEmeasure['CountInCyl']> -500]
RErange  =  RawMassEClassEmeasure[RawMassEClassEmeasure['CountInCyl']>  -500]
RErange1  =  RErange[RErange['SurfaceDensity']<  50]

binCount  =  30
alphaVal  =  .3

##### uminusr
fig  =  plt.figure(figsize=(12,  6),  dpi=200)
fig.suptitle('Plot - Histogram Red Galaxies for Elliptical Galaxies')
#fig.legend(loc="upper right")
#import scipy.stats as stats
from  scipy  import  stats
xfield  =  'uminusr'
counts,  bins  =  np.histogram(RErange1[xfield].data,bins=binCount)
print(counts)
ag,  bg,  cg  =stats.gamma.fit(counts)
print(ag,  bg,  cg)

ax1  =  fig.add_subplot(3,  1,  1)
ax1.set_ylabel('Galaxy Count')
ax1.set_xlabel(xfield)
counts,  bins  =  np.histogram(RErange1[xfield].data,bins=binCount)
ax1.hist(bins[:-1],bins,  weights=counts)
ax2  =  fig.add_subplot(3,  1,  2)
x  =  np.linspace(stats.gamma.ppf(0.1,  ag),stats.gamma.ppf(0.99,  ag),  243)
ax2.plot(x,  stats.gamma.pdf(x,  ag),'r-',  lw=5,  alpha=0.6,  label='gamma pdf')

param  =  stats.rayleigh.fit(counts)  # distribution fitting
# fitted distribution
xx  =  np.linspace(0,45,1000)
pdf_fitted  =  stats.rayleigh.pdf(xx,loc=param[0],scale=param[1])
pdf  =  stats.rayleigh.pdf(xx,loc=0,scale=8.5)

ax3  =  fig.add_subplot(3,  1,  3)
plot(xx,pdf,'r-',  lw=5,  alpha=0.6,  label='rayleigh pdf')
plot(xx,pdf,'k-',  label='Data')
plt.bar(x[1:],  counts)
plt.show()

[ 55  77  61  80  94  87 102 115 133 133 123 121 133 122 118 152 142 140
  120  96  84  71  39  26  19   9   8   3   0   4]
216.37114598925467 -636.6370861665209 3.3226700375837455

---------------------------------------------------------------------------
ValueError                                 Traceback (most recent call last)
/var/folders/cj/z259fmwd41dgzl8mq8nppwd40000gn/T/ipykernel_8820/1690958570.py  in<module>
44  plot(xx,pdf,'r-',  lw=5,  alpha=0.6,  label='rayleigh pdf')
45  plot(xx,pdf,'k-',  label='Data')
---> 46plt.bar(x[1:],  counts)
47  plt.show()
48  

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/pyplot.py  inbar(x, height, width, bottom, align, data, **kwargs)
2649          x,  height,  width=0.8,  bottom=None,  *,  align='center',
2650          data=None, **kwargs):
-> 2651return gca().bar( 2652          x,  height,  width=width,  bottom=bottom,  align=align,
2653          **({"data": data} if data is not None else {}), **kwargs)

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/__init__.py  ininner(ax, data, *args, **kwargs)
1359      def  inner(ax,  *args,  data=None,  **kwargs):
1360          if  datais  None:
-> 1361return  func(ax,  *map(sanitize_sequence,  args),  **kwargs)
1362  
1363          bound=  new_sig.bind(ax,  *args,  **kwargs)

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/matplotlib/axes/_axes.py  inbar(self, x, height, width, bottom, align, **kwargs)
2302                  yerr=  self._convert_dx(yerr,  y0,  y,  self.convert_yunits)
2303  
-> 2304x, height, width, y, linewidth, hatch = np.broadcast_arrays( 2305              # Make args iterable too.
2306              np.atleast_1d(x), height, width, y, linewidth, hatch)

<__array_function__ internals>  inbroadcast_arrays(*args, **kwargs)

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/lib/stride_tricks.py  inbroadcast_arrays(subok, *args)
536      args=  [np.array(_m,  copy=False,  subok=subok)  for  _min  args]
537  
--> 538shape=  _broadcast_shape(*args)
539  
540      if  all(array.shape==  shapefor  arrayin  args):

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/numpy/lib/stride_tricks.py  in_broadcast_shape(*args)
418      # use the old-iterator because np.nditer does not handle size 0 arrays
419      # consistently
--> 420b=  np.broadcast(*args[:32])
421      # unfortunately, it cannot handle 32 or more arguments directly
422      for  posin  range(32,  len(args),  31):

ValueError: shape mismatch: objects cannot be broadcast to a single shape



========== Art & Ceramics ===========
https://www.instagram.com/ksloan1952/

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/scipy-user/attachments/20210703/4f9f050b/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pjgfhlnhgohdekdc.png
Type: image/png
Size: 90988 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/scipy-user/attachments/20210703/4f9f050b/attachment-0001.png>


More information about the SciPy-User mailing list