0

I have a weighted histogram generated by

import numpy as np
import matplotlib.pyplot as plt

data = np.loadtxt('count_rate_data.txt')
hist_bin = [118,121,124,127,130,133,136,139,142,145,148,151,154,157,160,163,166,169,172,175,178,181,184,187,189]
weights=np.ones_like(data)/float(len(data))

plt.hist(data, hist_bin, weights=weights)
plt.grid()

plt.show()

I would like to fit a normal distribution that is weighted to the same extent. How do I do that? I know how to fit a normal distribution to an unweighted histogram. But I am unsure how to fit a normal histogram to a weighted histogram.

Taenyfan
  • 13
  • 1
  • 4

1 Answers1

0

The following steps worked for me.

  • Get the weighted sums:

    sums, bins = np.histogram(data, hist_bin, weights=weights)
    
  • Make a continuous distribution out of it:

    hist_dist = scipy.stats.rv_histogram((sums, bins))
    
  • Create a random sample from this distribution:

    weighted_data = hist_dist.rvs(size=100000)
    
  • And fit another distribution of your choice, such as the normal one:

    mean, stdev = scipy.stats.norm.fit(weighted_data)    
    
adr
  • 1,731
  • 10
  • 18