calculator of the standard error of measurement, the standard error of a series of measurements, the confidence interval of measurements and the relative error in python

To calculate the standard error of measurement in python, we can use the stats.sem() function from the scipy.stats package.

main.py
import scipy.stats as stats

data = [1, 2, 3, 4, 5]
sem = stats.sem(data)
print(sem)
85 chars
6 lines

To calculate the standard error of a series of measurements, we can use the numpy.std() function from the numpy package.

main.py
import numpy as np

data = [1, 2, 3, 4, 5]
std = np.std(data, ddof=1)  # using ddof=1 calculates sample standard deviation
sem = std / np.sqrt(len(data))
print(sem)
165 chars
7 lines

To calculate the confidence interval of measurements, we can use the t.interval() function from scipy.stats package.

main.py
import scipy.stats as stats

data = [1, 2, 3, 4, 5]
alpha = 0.05  # significance level
df = len(data) - 1  # degrees of freedom
mean = np.mean(data)
sem = stats.sem(data)
ci = stats.t.interval(1-alpha, df, loc=mean, scale=sem)
print(ci)
237 chars
10 lines

To calculate the relative error in Python, we can define a function to calculate it as follows:

main.py
import math

def relative_error(true_value, measured_value):
     return abs(true_value - measured_value) / float(true_value)
126 chars
5 lines

We can then use this function as follows:

main.py
true_value = 10
measured_value = 9.5
error = relative_error(true_value, measured_value)
print(error)
101 chars
5 lines

gistlibby LogSnag