1

I took some weights from coworkers, bootstrapped 1000 runs where each run samples 3 values from the original data and appends the minimum, and now I'm trying to curve fit these values to an exponential fit. When I plot the histogram, it isn't a pretty exponential value, but I think I'm doing something wrong because I just get out a flat line.

The value sample below contains the 1000 samples (each the minimum value of a sample of 3 from the original data)

figure = plt.figure(figsize=(10, 6)) # first element is width, second is height.
axes = figure.add_subplot(1, 1, 1)
#axes.hist( sample, density=True, color="dimgray") # a density
axes.set_ylabel( "Density")
axes.set_xlabel( "Weights")
axes.set_title( "Coworker Weights")

ys, bins = np.histogram(sample,bins = 7)
axes.plot(bins[:-1],ys)

print(bins,ys)

This plot comes out to next picture which is something ugly, but I still think it should fit to a curve:

enter image description here

Next I try to fit the data:

def func(x, a, b, c):
    return a * np.exp(-b * x) + c

weights = np.array(weights)
popt, pcov = curve_fit(func, bins[:-1], ys)
print(popt)

figure = plt.figure(figsize=(10, 6)) # first element is width, second is height.
axes = figure.add_subplot(1, 1, 1)
plt.plot(weights, func(weights, *popt))

The weights array is my original data. The output I get is just a flat line, and the popt coefficients are [1,1,143]. What am I doing wrong?

Sean Mahoney
  • 131
  • 7
  • 1
    It's because your bins are so far out (starting at 170) and your function doesn't include translation in the `x` direction. Your parameter `c` shifts it vertically; `b` *stretches* it horizontally but doesn't shift it, and `a` stretches it vertically. Introduce an additive parameter inside the exponential function to shift the exponential over horizontally (i.e. `np.exp(-b * x + d)` or something). Alternatively, just subtract the minimum value from your bins so they start at 0 (but parameterizing the shift is probably better in general). – alkasm Oct 13 '18 at 00:47
  • Hmm, I tried adding a +d to the exponential, and it's still flat. The new popt value for that d just comes out to 1 – Sean Mahoney Oct 13 '18 at 02:27
  • There should be a warning you get when it didn't fit properly (and you'll see something like all 1s or mostly 1s in that case). You can send an initial guess for the parameters with the argument `p0` (check the docs) which should help you converge. Overall I'm guessing it's just because (despite the shape certainly being exponential), the function is actually quite far off---needs to be shifted and scaled a lot. Can you post some minimal example arrays for others to try with? – alkasm Oct 13 '18 at 03:07
  • Thanks for the reply Alexander. The bins and ys array I used to try and fit are: x values: [168. 177. 186. 195. 204. 213. 222] y values: [757 136 61 0 37 4 5] The male weights data I drew my bootstrap samples from is: male_weights = [235,205,207,212,189,231,226,182,234,252,219,172,178,169,193,168,172] – Sean Mahoney Oct 13 '18 at 03:40

0 Answers0