def covariance(list_x, list_y): sum = 0 N = len(list_x) x_mean = zmean.mean(list_x) y_mean = zmean.mean(list_y) x_dev = zdev.stddev(list_x) y_dev = zdev.stddev(list_y) for i in range(0, len(list_y)): sum += (list_x[i] - x_mean) * (list_y[i] - y_mean) covar = sum / (N - 1) cor = round(covar / (x_dev * y_dev), 3) return cor #print(f'Covariance is {covar} and Correlation is {cor}') # x = [10.0, 8.0, 13.0, 9.0, 11.0, 14.0, 6.0, 4.0, 12.0, 7.0, 5.0] # y = [9.14, 8.14, 8.74, 8.77, 9.26, 8.1, 6.13, 3.1, 9.13, 7.26, 4.74] # # covariance(x,y)
def stderr(in_list): """ Calculates standard error of mean of given list :param in_list: list of values :return: float rounded to 5 decimal places """ std_dev = zstddev.stddev(in_list) n = zcount.count(in_list) std_err = std_dev / sqrt(n) return round(std_err, 5)
def csv_reader(file): reader = csv.DictReader(open(file)) result = {} for row in reader: for column, value in row.items(): result.setdefault(column, []).append(float(value)) return result test = csv_reader(dataOne) x = [10.0, 8.0, 13.0, 9.0, 11.0, 14.0, 6.0, 4.0, 12.0, 7.0, 5.0] y = [9.14, 8.14, 8.74, 8.77, 9.26, 8.1, 6.13, 3.1, 9.13, 7.26, 4.74] print("Count") print(zcount.count(x)) print("Mean") print(zmean.mean(x)) print("Mode") print(zmode.mode(x)) print("Median") print(zmedian.median(x)) print("Variance") print(zvariance.variance(x)) print("Standard Deviation") print(zstderr.stdderr(x)) print("Standard Error") print(zstddev.stddev(x)) print("Correlation") print(zcorr.covariance(x, y))
print('----------------------') print('File: ', f) print('----------------------') data = csvReader.read_csv(f) x = data[0] y = data[1] # x list print('x list') print('------') print('Count X: ', zcount.count(x)) print('Mean X: ', zmean.mean(x)) print('Median X ', zmedian.median(x)) print('Mode X: ', zmode.mode(x)) print('Variance X: ', zvariance.variance(x)) print('Standard Deviation X: ', zstddev.stddev(x)) print('Standard Error X: ', zstderr.stderr(x)) # y list print('-------') print('y list:') print('-------') print('Count Y: ', zcount.count(y)) print('Mean Y: ', zmean.mean(y)) print('Median Y ', zmedian.median(y)) print('Mode Y: ', zmode.mode(y)) print('Variance Y: ', zvariance.variance(y)) print('Standard Deviation Y: ', zstddev.stddev(y)) print('Standard Error Y: ', zstderr.stderr(y)) # Correlation
def stdderr(list_in): dev = zs.stddev(list_in) err = dev / math.sqrt(len(list_in)) return round(err, 2)
def test_stddev1(self): test_data = [1, 2, 3, 4, 5] self.assertEqual(round(stdev(test_data), 5), zstddev.stddev(test_data))
def test_stddev3(self): test_data = [-10, -20, -30, -40, -50] self.assertEqual(round(stdev(test_data), 5), zstddev.stddev(test_data))
def test_stddev2(self): test_data = [6, 7, 8, 9, 10] self.assertEqual(round(stdev(test_data), 5), zstddev.stddev(test_data))