def fit_sigma(df, i):
"""
Find the largest allowable standard deviation, given the possible values Tactual can take.
"""
Tmeasured, Tactual, _, _ = get_values(df)
Tm = Tmeasured[i]
# Get the possible values, and bin those with this measured value
possible_values = sorted(pd.unique(df.Tactual))
edges = [(possible_values[i] + possible_values[i+1])/2 for i in range(len(possible_values)-1)]
bins = [0] + edges + [9e9]
good = df.loc[df.Temperature == Tm]
values, _= np.histogram(good.Tactual.values, bins=bins)
mean = np.mean(good.Tactual.values)
std = np.std(good.Tactual.values, ddof=1)
if std > 0:
return std
sigma_test = np.arange(500, 10, -10) #Just test a bunch of values
idx = np.searchsorted(bins, mean)
idx = np.argmin(abs(np.array(bins) - mean))
x1 = bins[idx-2] if idx > 2 else -1
x2 = bins[idx-1]
x3 = bins[idx]
x4 = bins[idx+1] if idx < len(bins)-2 else np.inf
N = len(good)
probs = [get_probability(x1, x2, x3, x4, N, mean, s) for s in sigma_test]
for s, p in zip(sigma_test, probs):
if p > 0.5:
return s
# If we get here, just return a guess value
return 200.0
#raise ValueError('No probability > 0!')
评论列表
文章目录