Skip to content

Commit

Permalink
imports - 3
Browse files Browse the repository at this point in the history
  • Loading branch information
ric-evans committed Nov 15, 2024
1 parent 410de29 commit 98a0eaf
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 126 deletions.
6 changes: 2 additions & 4 deletions examples/classic_fit_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,15 @@
for c in range(int(charge)):
data.append(time)


def gauss(x, *p):
'''
The gaussian distribution.
'''
amplitude, mean, sigma = p
return amplitude * numpy.exp(-(x - mean) ** 2 / (2. * sigma ** 2))

return amplitude*numpy.exp(-(x-mean)**2/(2.*sigma**2))

hist, bin_edges = numpy.histogram(data)
bin_centers = (bin_edges[:-1] + bin_edges[1:]) / 2
bin_centers = (bin_edges[:-1] + bin_edges[1:])/2
p0 = [1000., 0., 1.]
p1 = [1000., 0., 1.]
print(hist)
Expand Down
13 changes: 6 additions & 7 deletions examples/concept_illustration.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@
'''

import numpy
import pylab # type: ignore[import]
import pylab # type: ignore[import]

import voka.model
import voka.tools.render
import voka.two_sample

Expand All @@ -32,10 +31,10 @@
histograms['ChargeHistogram%d' % i] = numpy.histogram(charge_data, bins=100)

# Render the histograms
i = 0
i=0
for title, histogram in histograms.items():
pylab.figure(i)
i = i + 1
i=i+1
bin_values = histogram[0]
voka.tools.render.draw(bin_values, title)

Expand All @@ -45,7 +44,7 @@
# First compare the histograms with traditional statistical tests
test_sample = histograms['ChargeHistogram0'][0]
benchmark_ensemble = [histograms['ChargeHistogram%d' % i][0]
for i in range(1, 6)]
for i in range(1,6)]
print(benchmark_ensemble)
for benchmark_sample in benchmark_ensemble:
result = voka.two_sample.traditional(test_sample, benchmark_sample)
Expand All @@ -54,8 +53,8 @@
# Second compare the test histogram using voka.
model = voka.model.Voka()
reference_collection = {
"Benchmark%d" % idx:
{"ChargeHistogram": s}
"Benchmark%d" % idx :
{"ChargeHistogram":s}
for idx, s in enumerate(benchmark_ensemble)
}

Expand Down
19 changes: 10 additions & 9 deletions examples/standard_distribution_comparisons.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import numpy
import pylab # type: ignore[import]

# What's the difference here between voka.metrics.chisq
# and scipy.stats.chisquare ?
# import voka.metrics.chisq
Expand All @@ -34,42 +35,42 @@
data = numpy.random.normal(size=SIZE)
histograms['Gaussian%d' % i] = numpy.histogram(data, bins=BINS)

data = numpy.random.normal(size=int(SIZE / 2), loc=-1, scale=0.25) + \
numpy.random.normal(size=int(SIZE / 2), loc=1, scale=0.25)
data = numpy.random.normal(size=int(SIZE/2), loc=-1, scale=0.25) + \
numpy.random.normal(size=int(SIZE/2), loc=1, scale=0.25)
histograms['BiGaussian%d' % i] = numpy.histogram(data, bins=BINS)

data = numpy.random.exponential(scale=10, size=1000)
histograms['Exponential%d' % i] = numpy.histogram(data, bins=BINS)

T_dist = collections.defaultdict(list)
# test_stat = voka.metrics.chisq.NormChiSq()
#test_stat = voka.metrics.chisq.NormChiSq()
test_stat = scipy.stats.chisquare
print("Comparing histograms...")
for i in range(N_HISTOGRAMS):
for j in range(i + 1, N_HISTOGRAMS):
for j in range(i+1, N_HISTOGRAMS):
h1 = histograms["Uniform%d" % i][0]
h2 = histograms["Uniform%d" % j][0]
# T_dist['uniform'].append(test_stat(h1, h2))
#T_dist['uniform'].append(test_stat(h1, h2))
T_dist['uniform'].append(test_stat(h1, h2)[0])

h1 = histograms["ChiSq5%d" % i][0]
h2 = histograms["ChiSq5%d" % j][0]
# T_dist['chisq'].append(test_stat(h1, h2))
#T_dist['chisq'].append(test_stat(h1, h2))
T_dist['chisq'].append(test_stat(h1, h2)[0])

h1 = histograms["Exponential%d" % i][0]
h2 = histograms["Exponential%d" % j][0]
# T_dist['exponential'].append(test_stat(h1, h2))
#T_dist['exponential'].append(test_stat(h1, h2))
T_dist['exponential'].append(test_stat(h1, h2)[0])

h1 = histograms["Gaussian%d" % i][0]
h2 = histograms["Gaussian%d" % j][0]
# T_dist['gaussian'].append(test_stat(h1, h2))
#T_dist['gaussian'].append(test_stat(h1, h2))
T_dist['gaussian'].append(test_stat(h1, h2)[0])

h1 = histograms["BiGaussian%d" % i][0]
h2 = histograms["BiGaussian%d" % j][0]
# T_dist['bigaussian'].append(test_stat(h1, h2))
#T_dist['bigaussian'].append(test_stat(h1, h2))
T_dist['bigaussian'].append(test_stat(h1, h2)[0])

pylab.figure(1)
Expand Down
2 changes: 1 addition & 1 deletion examples/stochastic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

NDOF = None
for i in range(N_HISTOGRAMS):
for j in range(i + 1, N_HISTOGRAMS):
for j in range(i+1, N_HISTOGRAMS):
ch1 = histograms["ChargeHistogram%d" % i][0]
ch2 = histograms["ChargeHistogram%d" % j][0]
th1 = histograms["TimeHistogram%d" % i][0]
Expand Down
57 changes: 27 additions & 30 deletions examples/traditional_two_sample_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import voka.tools.render


# Low p-value is bad here.
# The null hypothesis H_0 is that the two samples
# were sampled from the same underlying distribution.
Expand Down Expand Up @@ -116,14 +115,14 @@ def voka_2sample(sample1, sample2):
except:
print(" skipping kruskal")

# try:
# r = scipy.stats.friedmanchisquare(sample1, sample2)
# result['FriedmanChiSquare'] = {
# 'statistic': r.statistic,
# 'pvalue': r.pvalue
# }
# except ValueError:
# print(" skipping friedmanchisquare")
# try:
# r = scipy.stats.friedmanchisquare(sample1, sample2)
# result['FriedmanChiSquare'] = {
# 'statistic': r.statistic,
# 'pvalue': r.pvalue
# }
# except ValueError:
# print(" skipping friedmanchisquare")

r = scipy.stats.brunnermunzel(sample1, sample2)
result['BrunnerMunzel'] = {
Expand All @@ -133,13 +132,11 @@ def voka_2sample(sample1, sample2):

return result


DENSITY = True


def compare(benchmark_distribution, systematic_distribution, pvalues, density=False):
# Use the same histogram settings for each.
_range = (-5, 5)
_range = (-5,5)
_bins = 100

# First histogram to make samples
Expand All @@ -157,7 +154,6 @@ def compare(benchmark_distribution, systematic_distribution, pvalues, density=Fa
for k, v in comparison.items():
pvalues[k].append(v['pvalue'])


if __name__ == '__main__':

# Tests to perform
Expand All @@ -170,32 +166,32 @@ def compare(benchmark_distribution, systematic_distribution, pvalues, density=Fa
benchmark_center = 0.0
benchmark_size = 100000

widths = numpy.arange(0.5 * benchmark_width,
1.5 * benchmark_width,
widths = numpy.arange(0.5*benchmark_width,
1.5*benchmark_width,
0.001)

centers = numpy.arange(-0.5,
0.5,
0.001)

sizes = numpy.arange(0.5 * benchmark_size,
1.5 * benchmark_size,
sizes = numpy.arange(0.5*benchmark_size,
1.5*benchmark_size,
10)

draw_width_idx = int(0.9 * len(widths))
draw_center_idx = int(0.9 * len(centers))
draw_width_idx = int(0.9*len(widths))
draw_center_idx = int(0.9*len(centers))

benchmark_distribution = numpy.random.normal(loc=benchmark_center,
scale=benchmark_width,
size=benchmark_size)

# Generate systematic distributions
print(80 * '-')
print(80*'-')
figure_number = 1

pvalues_width_systematic: dict = collections.defaultdict(list)
pvalues_width_systematic = collections.defaultdict(list)
for idx, systematic_width in enumerate(widths):
# print('width = %.4f' % systematic_width)
#print('width = %.4f' % systematic_width)
systematic_distribution = numpy.random.normal(loc=benchmark_center,
scale=systematic_width,
size=benchmark_size)
Expand All @@ -207,7 +203,7 @@ def compare(benchmark_distribution, systematic_distribution, pvalues, density=Fa
if idx == draw_width_idx:
print('width = %.4f' % systematic_width)
# Use the same histogram settings for each.
_range = (-5, 5)
_range = (-5,5)
_bins = 100

# First histogram to make samples
Expand All @@ -224,6 +220,7 @@ def compare(benchmark_distribution, systematic_distribution, pvalues, density=Fa
figure_number += 1
voka.tools.render.draw_ratio(systematic_sample, benchmark_sample)


pylab.figure(figure_number)
figure_number += 1
for label, pvs in pvalues_width_systematic.items():
Expand All @@ -236,10 +233,10 @@ def compare(benchmark_distribution, systematic_distribution, pvalues, density=Fa
pylab.xlabel('width')
pylab.ylabel('log(p-value)')

print(80 * '-')
pvalues_center_systematic: dict = collections.defaultdict(list)
print(80*'-')
pvalues_center_systematic = collections.defaultdict(list)
for idx, systematic_center in enumerate(centers):
# print('center = %.4f' % systematic_center)
#print('center = %.4f' % systematic_center)
systematic_distribution = numpy.random.normal(loc=systematic_center,
scale=benchmark_width,
size=benchmark_size)
Expand All @@ -251,7 +248,7 @@ def compare(benchmark_distribution, systematic_distribution, pvalues, density=Fa
if idx == draw_center_idx:
print('center = %.4f' % systematic_center)
# Use the same histogram settings for each.
_range = (-5, 5)
_range = (-5,5)
_bins = 100

# First histogram to make samples
Expand Down Expand Up @@ -279,12 +276,12 @@ def compare(benchmark_distribution, systematic_distribution, pvalues, density=Fa
pylab.xlabel('center')
pylab.ylabel('log(p-value)')

print(80 * '-')
print(80*'-')
pylab.figure(figure_number)
figure_number += 1
pvalues_size_systematic: dict = collections.defaultdict(list)
pvalues_size_systematic = collections.defaultdict(list)
for systematic_size in sizes:
# print('size = %d' % int(systematic_size))
#print('size = %d' % int(systematic_size))
systematic_distribution = numpy.random.normal(loc=benchmark_center,
scale=benchmark_width,
size=int(systematic_size))
Expand Down
Loading

0 comments on commit 98a0eaf

Please sign in to comment.