Accept or Reject an Experiment?
Stephen Lukacs (2) iquanta.org/instruct/python
"""
reference: https://iquanta.org/instruct/python ::: Statistics 4: Student's t Test ::: Stephen Lukacs, Ph.D. ©2023-02-14
"""
from py4web import URL, request
from yatl.helpers import *
from iquanta.mcp import is_str_float, str_to_float
from iquanta.chmpy import Ttest, students
BR, B = TAG['br/'], TAG['b']
#demo_data = "0.1190\n0.09847\n0.09852"
#demo_accepted = "0.1"
demo_data = "6.18\n6.28\n4.85\n6.49"
demo_accepted = "4.85"
rtn = FORM(_action=None, _method="post")
if ('txtfile' in request.forms):
txt, data = request.forms['txtfile'], [ ]
for l in txt.strip().split('\n'):
if is_str_float(l.strip()):
data.append(str_to_float(l.strip()))
#rtn.append(CAT(data, BR()))
if ('accepted' in request.forms):
if is_str_float(request.forms['accepted']):
accepted = str_to_float(accepted)
rtn.append(STYLE("input[type=text] { width: 70px; text-align: center; border-radius: 7px; } textarea { margin: 0px; width: 295px; height: 200px; border-radius: 5px; } p { margin: 2px 0px; padding: 8px; border-radius: 10px; border: 2px solid silver; }"))
rtn.append(CAT(DIV("Enter the dataset to be tested below...", BR(), TEXTAREA(txtfile if ('txtfile' in locals()) else demo_data, _name="txtfile"), _style="float:left;"), DIV(BR(), "What is the accepted or true value?", BR(), INPUT(_type="text", _name="accepted", _value=accepted if ('accepted' in locals()) else demo_accepted, _style="width: 130px;"), *[BR()]*2, INPUT(_type="submit", _value="Upload"), ", or, just Upload to run the demonstration.", _style="float:left; margin: 5px;"), DIV(_style="float:none;clear:both;")))
if ('data' in locals()) and ('accepted' in locals()):
CIs = (50, 80, 90, 95, 99, 99.5, 99.9,)
Ttests = [ Ttest(data, accepted, ci) for ci in CIs ]
t = Ttests[0]
p = P()
#p.append(CAT(str(t), BR()))
p.append(CAT(B(XML(f'With x̄ = {t[1]:.4g} and σ = {t[2]:.4g}, the dataset can be rejected '), BR(), f'unless you want to be {"only" if (t[4] < 50.) else "at least"} {t[4]:.2f}%, or higher, confident in its acceptance, or:', BR(), _style="font-size:18pt; font-weight:bold;")))
for i, (ci, t) in enumerate(zip(CIs, Ttests), 1):
#p.append(CAT(XML(t), BR()*2,))
if t[7]:
p.append(SPAN(XML(f'<b>at {ci}% confidence</b> the dataset <b>must be accepted</b>, bias not detected, and results comparable to the accepted.<br/>'), _style="font-size:14pt;"))
else:
p.append(SPAN(XML(f'<b>at {ci}% confidence</b> the dataset <b>should be rejected</b>, bias detected, and results significantly different than the accepted.<br/>'), _style="font-size:14pt;"))
rtn.append(p)
dv = DIV(H3("Understanding Student's t Test"), "The Student's t test was first proposed by William Sealy Gosse, a.k.a., Student, in 1908. He proposed that any one-dimensional set of measurements can be compared to an accepted or true set of measurements based on their statistical closeness. The Student's t test uses statistical analysis to determine if the current experimental measurements match up with the previously accepted or true experimental measurement. If accepted, then the experiment shows no bias and is thus comparable and valid to the previous experiments. If rejected, then the experiment is not comparable and should be redesigned, rerun, or disregarded altogether.", *[BR()]*2, r"It is a three step process. First, calculate the Student's t value using the equation: $$t = \frac{\mid accepted - \bar{x} \mid}{ \sigma/\sqrt{n} }$$ where the accepted is the previously accepted or true measurements to be tested against, \(\bar{x}\), \(\sigma\), and n are the average or mean, the standard deviation, and the count of the dataset, respectively, being compared and tested.", *[BR()]*2, "Second, look up the Student's critical value for the count of datapoints, n, and the confidence or certainty level, CL, required in the below table. And finally, third, if the above t value is less than or equal to the critical value, then the datapoint must be accepted, in which the experiment at that confidence show no bias and its result is not significantly different than the accepted or true value. If the t value is greater than the critical value, then you should reject and disregard the experiment, that bias is detected, and the results of the experiment are significantly different from the accepted or true experiment.", *[BR()]*2, _style="")
s = students()
st = s.find('table#students')[0]
st['_style'] = "margin: auto;"
dv.append(CAT(s.find('style')[0], H4(". .. Student's Critical Values .. ."), st))
dv.append(CAT("lecture by Stephen Lukacs, Ph.D., ©2011 - 2023; updated: March 7, 2023. all data confirmed via ", A("lecture_data_analysis.nb", _href=URL('static', "pdf/lecture_data_analysis8.pdf"), _target="data_analysis"), "."))
rtn.append(dv)