title: Maxwellian Distribution in Python Scipy
body: <p>In the article that I am interested in, it states that the data is well represented with a Maxwellian distribution and it also provides a Mean speed (307 km/s) and 1 sigma uncertainty (47 km/s) for the distribution.</p>
  <p>Using the provided values, I have attempted to re-generate the data and then fit it with the Maxwellian distribution using the python scipy.stats.</p>
  <p>As it described in <a href="https://stackoverflow.com/questions/30928338/what-are-the-loc-and-scale-parameters-in-scipy-stats-maxwell">here</a>, maxwell function in scipy takes two input, 1) &quot;loc&quot; which shifts the x variable and 2) &quot;a&quot; parameter which corresponds to the parameter &quot;a&quot; in the maxwell-Boltzmann equation.</p>
  <p>In my case, I have neither of these parameters, so using the Mean and variance (sigma^2) description in <a href="https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution" rel="nofollow noreferrer">wiki page</a>, I have attempted to calculate the &quot;a&quot; and &quot;loc&quot; parameter. Both mean and sigma parameters are only dependent on &quot;a&quot; parameter.</p>
  <p>The first problem I have encountered was the &quot;a&quot; parameter that I get from Mean (a = 192.4) and sigma (a = 69.8) are different from each other.
  The second problem is that I don't know how can I obtain the exact loc (shift) value from Mean and sigma.</p>
  <p>Based on the shape of the distribution (where mean speed values fall in the graph, check figure 2), I tried to guess the &quot;loc&quot; value and together with the &quot;a&quot;  value obtained from sigma (a = 69.8), I have generated and fitted the data. Approximately it seems correct, but I don't know the answer to the questions I mentioned above and I need some expert's guidance on this. I appreciate any help.</p>
  <pre><code>import matplotlib.pyplot as plt
  import math
  from scipy.stats import norm
  import random
  import numpy as np
  import scipy.optimize
  from scipy.stats import maxwell
  
  samplesize = 100000
  
  mean = 307
  sigma = 47
  loc = 175 #my guess
  a_value = np.sqrt((sigma**2 * math.pi)/(3*math.pi - 8)) #calculated based on wiki description
  
  fig, axs = plt.subplots(1)
  v_2d = maxwell.rvs(loc, a_value, size=samplesize) #array corresponding to 2D proper motion obtained from Hubbs
  mean, var, skew, kurt = maxwell.stats(moments='mvsk')
  
  N, bins, patches = plt.hist(v_2d, bins=100, density=True, alpha=0.5, histtype='bar', ec='black')
  maxx = np.linspace(min(v_2d), max(v_2d), samplesize)
  
  axs.plot(maxx, maxwell.pdf(maxx, loc, a_value), color=colorset[6], lw=2, label= r'$\mathdefault{\mu}$ = '+'{:0.1f}'.format(mean)+r' , '+r'$\mathdefault{\sigma}$ = '+'{:0.1f}'.format(sigma))
  
  axs.set(xlabel=r'2-D Maxwellian speed (km s$^{-1}$)')
  axs.set(ylabel='Frequency')
  plt.legend(loc='upper right')
  </code></pre>
  <p><a href="https://i.stack.imgur.com/GqsXT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/GqsXT.png" alt="Distribution based on my guess" /></a></p>
  <p><a href="https://i.stack.imgur.com/br7kP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/br7kP.png" alt="Mean and median speed in a Maxwellian distribution" /></a></p>