python code for the standard error of a single measurement, the standard error of the result of a series of measurements, confidence interval of measurements and relative error in python

To calculate the standard error of a single measurement and the standard error of the result of a series of measurements, you can use the stdev function from the statistics module in Python.

main.py
import statistics

# Standard error of a single measurement
data = [4, 5, 6, 4, 5, 7, 5, 6]
se = statistics.stdev(data) / len(data)**0.5
print(se)

# Standard error of the result of a series of measurements
data1 = [4, 5, 6, 4, 5, 7, 5, 6]
data2 = [3, 5, 4, 6, 4, 5, 4, 5]
se_results = (statistics.stdev(data1) + statistics.stdev(data2)) / (len(data1) + len(data2))**0.5
print(se_results)
389 chars
13 lines

To calculate the confidence interval of measurements, you can use the t.interval function from the scipy.stats module in Python.

main.py
from scipy.stats import t

# Confidence interval of measurements
data = [4, 5, 6, 4, 5, 7, 5, 6]
conf_int = t.interval(0.95, len(data)-1, loc=statistics.mean(data), scale=statistics.stdev(data)/len(data)**0.5)
print(conf_int)
226 chars
7 lines

To calculate the relative error, you can use the formula (measured value - true value) / true value.

main.py
# Relative error
measured_value = 5.5
true_value = 5
rel_err = (measured_value - true_value) / true_value
print(rel_err)
121 chars
6 lines

related categories

gistlibby LogSnag