def test_variance(self): """Assume degree of freedom = 0""" l = [1, 2, 3, 4, 5, 5, 4, 3, 3, 1, 2] my_value = st.variance(l) numpy_value = np.var(l) self.assertEquals(my_value, numpy_value)
def testVariance(self): data = [1, 2, 3] assert stats.mean(data) == 2 self.assertEqual(stats.pvariance(data), 2/3) self.assertEqual(stats.variance(data), 1.0) self.assertEqual(stats.pstdev(data), math.sqrt(2/3)) self.assertEqual(stats.stdev(data), 1.0)
def directional_variance(data: List[Vector], w: Vector) -> float: """ Given a dataset and a vector w from which to take a direction, return the variance in the data along that direction """ dir_w = direction(w) # key insight: the dot product of two orthogonal vectors is zero. dot product against a magnitude vector is the portion # of the magnitude of the query vector in THAT direction dot_projections: Vector = [dot(v, dir_w) for v in data] return variance(dot_projections) # books code doesn't center mean but that should be ok, we already centered it
def clustered_mismatch_variance(self, cluster_count: int) -> float: """ Returns the variance between the mismatch clusters. The raw cluster mismatches can be retrieved with the `clustered_mismatches` method. `cluster_count` controls the number of clusters used. """ return variance( np.array(self.clustered_mismatches(cluster_count=cluster_count)), sample=False, )
def print_testcase(variablePrefix, i, count, values_mean, values_stdDev, values_ordering, weights_mean, weights_stdDev, weights_ordering): from stats import mean, variance, sqrt_float # calculate values values = generate_numbers(count, values_mean, values_stdDev, values_ordering) weights = generate_weights(count, weights_mean, weights_stdDev, weights_ordering) m = mean(values, weights) v = variance(values, weights, m) stdDev = sqrt_float(v) weightedValues = [] for value, weight in zip(values, weights): weightedValues.append(value) weightedValues.append(weight) # output values print("// Generated values with parameters:") print("// Count = {}".format(count)) print("// values_mean = {:e}; values_stdDev = {:e}; values_ordering = {}". format(values_mean, values_stdDev, values_ordering)) print( "// weights_mean = {:e}; weights_stdDev = {:e}; weights_ordering = {}". format(weights_mean, weights_stdDev, weights_ordering)) print("private static final long {}Mean{} = {}; // approx ~ {:e}".format( variablePrefix, i, export(float(m)), float(m))) print("private static final long {}StdDev{} = {}; // approx ~ {:e}".format( variablePrefix, i, export(stdDev), stdDev)) print("private static final long[] {}WeightedValues{} = {};".format( variablePrefix, i, export_series(weightedValues))) print("")
def print_testcase(variablePrefix, i, count, values_mean, values_stdDev, values_ordering, weights_mean, weights_stdDev, weights_ordering): from stats import mean, variance, sqrt_float # calculate values values = generate_numbers(count, values_mean, values_stdDev, values_ordering) weights = generate_weights(count, weights_mean, weights_stdDev, weights_ordering) m = mean(values, weights) v = variance(values, weights, m) stdDev = sqrt_float(v) weightedValues = [] for value, weight in zip(values, weights): weightedValues.append(value) weightedValues.append(weight) # output values print("// Generated values with parameters:") print("// Count = {}".format(count)) print("// values_mean = {:e}; values_stdDev = {:e}; values_ordering = {}" .format(values_mean, values_stdDev, values_ordering)) print("// weights_mean = {:e}; weights_stdDev = {:e}; weights_ordering = {}" .format(weights_mean, weights_stdDev, weights_ordering)) print("private static final long {}Mean{} = {}; // approx ~ {:e}" .format(variablePrefix, i, export(float(m)), float(m))) print("private static final long {}StdDev{} = {}; // approx ~ {:e}" .format(variablePrefix, i, export(stdDev), stdDev)) print("private static final long[] {}WeightedValues{} = {};" .format(variablePrefix, i, export_series(weightedValues))) print("")
def getSignalCoherenceError(downSampledFrequency, rawData = rawData): downSampled = fftDataExtraction.downSample(rawData, rawSps, downSampledFrequency, interpolate = True)#[int(random()*100):] #downSampled = downSampled[:int(downSampledFrequency)/2] #downSampled = downSampled[:40] highSamples = downSampled[::2] lowSamples = downSampled[1::2] times = map(lambda x: x/downSampledFrequency, range(len(downSampled))) highTimes = times[::2] lowTimes = times[1::2] differences = map(lambda x: (x[0]-x[1])**1.0, zip(highSamples, lowSamples)) slope, yint = stats.lineOfBestFit(highTimes, differences) #return slope #return abs(slope) #return 1.0 / slope #return max(differences) - min(differences) #derivSquared = sum([(differences[i+1] - differences[i])**2.0 for i in range(len(differences)-1)]) #return derivSquared return stats.variance(differences)
median = st.median(A) print("A's median = ", median) quantile = st.quantile(A, 0.2) print("A's 20% quantile = ", quantile) quantile = st.quantile(A, 0.9) print("A's 90% quantile = ", quantile) mode = st.mode(A) print("A's mode = ", mode) data_range = st.data_range(A) print("A's range = ", data_range) variance = st.variance(A) print("A's variance = ", variance) standard_deviation = st.standard_deviation(A) print("A's standard deviation = ", standard_deviation) interquartile_range = st.interquartile_range(A) print("A's interquartile range of 25% ~ 75% = ", interquartile_range) x = [-2, -1, 0, 1, 2] y = [2, 1, 0, 1, 2] correlation = st.correlation(x, y) print("correlation = ", correlation)
def test_variance_single_value_is_zero(self): self.assertEqual(0.0, variance([0.0])) self.assertEqual(0.0, variance([99.999]))
import random import matplotlib.pyplot as plt import stats import numpy import math def generoValoresEmpiricos(matrizValores): x = random.random() for i in range(0, len(matrizValores)): if (matrizValores[i][3] >= x): x = matrizValores[i][0] return x cantNum = 1000 matrizValores = [[1, 15, 0.15, 0.15], [2, 15, 0.15, 0.3], [3, 17, 0.17, 0.52], [4, 12, 0.12, 0.64], [5, 20, 0.2, 0.84], [6, 16, 0.16, 1]] for j in range(0, 2): valores = [] for i in range(0, cantNum): valores.append(generoValoresEmpiricos(matrizValores)) print("\nMuestra Nro ", j + 1) print("La Esperanza de la muestra es =", stats.mean(valores)) print("La Varianza es = ", stats.variance(valores)) plt.hist(valores, edgecolor='black', linewidth=1, alpha=0.5) plt.show()
covariances = [] for i in range(1, sampleCount): covariance = 0 X1 = sampleList[i - 1] #list of values in the first sample X2 = sampleList[i] #list of values in the second sample u1 = sampleMeans[i - 1] #mean of the first sample u2 = sampleMeans[i] #mean of the second sample for index in range(0, sampleLength): covariance += ((X1[index] - u1) * (X2[index] - u2)) / sampleLength covariances = covariances + [covariance] #Now we calculate correlations between each sample to see if they approach zero. #If they approach zero the initial transient is vanished correlations = [] for i in range(1, sampleCount): X1_variance = stats.variance(sampleList[i - 1]) X2_variance = stats.variance(sampleList[i]) try: correlation = covariances[i - 1] / math.sqrt( X1_variance * X2_variance) correlations = correlations + [correlation] except: #Division with zero has occured, so correlation set to zero correlations = correlations + [0] print(correlations) """ Here we look at the average queue lengths with each of the configurations. """ """ interarrival_time is either exp(25) or exp(22.5)
def test_variance_typical_values(self): """variance of some typical values""" self.assertEqual(0.0, variance([10.0, 10.0, 10.0, 10.0, 10.0])) self.assertEqual(2.0, variance([1, 2, 3, 4, 5])) self.assertEqual(8.0, variance([10, 2, 8, 4, 6]))
def testVar(self): sx2 = stats.variance(self.xdata) sy2 = stats.variance(self.ydata) self.assertAlmostEqual(sx2, 11004687 / 983040, places=self.places) self.assertAlmostEqual(sy2, 4647 / 3840, places=self.places)
don't have to write lots of code over and over for each problem. """ # CONFIG PROJECT_ROOT = '..' # relative location pointing to utils/ and stats.py # REST OF FILE import sys # allows import of project files (idk how else to do this) sys.path.insert(1, PROJECT_ROOT) from utils.webassign import array_from_shitstring from stats import variance, standard_deviation data = array_from_shitstring( "85 105 130 160 180 195 134 145 214 105 145 151 153 135 87 99 94 119 129" ) # put your data here data.sort() print("Oxidation induction time (min): {}".format(data)) data_variance = variance(data) data_standard_deviation = standard_deviation(data) print("Sample variance: {}".format(data_variance)) print("Standard deviation: {}".format(data_standard_deviation)) data_to_hours = [value / 60 for value in data] data_variance_in_hours = variance(data_to_hours) standard_deviation_in_hours = standard_deviation(data_to_hours) print("Sample variance (hrs): {}".format(data_variance_in_hours)) print("Standard deviation (hrs): {}".format(standard_deviation_in_hours))
def test_variance_single_value_is_zero(self): """variance of a singleton list should be zero""" self.assertEqual(0.0, variance([0.0])) self.assertEqual(0.0, variance([99.999]))
def testVariance(self): self.assertEquals(self.population_variance, stats.variance(self.data, True)) self.assertEquals(self.sample_variance, stats.variance(self.data[:len(self.data) / 2]))
def register_sample (app, app_id, tag, day_of_the_week = None): user = User.objects.filter(app = app, app_id = app_id).first() ## Clear all the events of that tag ## Then recreate them from the new data events = Event.objects.filter(user = user, tag = tag) if day_of_the_week is not None: events = events.filter(day_of_week = day_of_the_week) for event in events: event.is_active = False event.save() app = importlib.import_module(user.app + "." + user.app) event_times = getattr(app, "%s_times" % tag)(app_id, day_of_the_week = day_of_the_week) if len(event_times) < 2: return if len(event_times[0]) < 2: return pmf = stats.event_pmf(event_times, 1440) pmf_average = stats.average(pmf) if pmf_average < minimum_pmf_mean(tag): ## All weak probabilities. Only outlier events. return pmf_variance = stats.variance(pmf, average = pmf_average) pmf_std = stats.standard_deviation(pmf, variance = pmf_variance) in_event = False event_start_minutes = [] event_end_minutes = [] event_probabilites = [] for minute in range(0,1440): if pmf[minute] > pmf_average + pmf_variance: if in_event is False: event_start_minutes.append(minute) in_event = True else: if in_event is True: event_end_minutes.append(minute) in_event = False if len(event_start_minutes) > len(event_end_minutes): ## Assume the last event started at night and ends in the morning event_start_minutes[0] = event_start_minutes[len(event_start_minutes) - 1] del event_start_minutes[len(event_start_minutes) - 1] ## If events are too close together, combined them. for index in range(0, len(event_end_minutes)): if index + 1 >= len(event_start_minutes): break event_end_time = event_end_minutes[index] next_event_start_time = event_start_minutes[index + 1] time_between_event = next_event_start_time - event_end_time if time_between_event < minimum_time_between_event(tag): del event_end_minutes[index] del event_start_minutes[index + 1] for index in range(0, len(event_start_minutes)): start_minute = event_start_minutes[index] end_minute = event_end_minutes[index] if start_minute < end_minute: event_probability_set = pmf[start_minute:end_minute] else: event_probability_set = pmf[start_minute:1439] event_probability_set.extend(pmf[0:end_minute]) event_average_probablity = stats.average(event_probability_set) event_probability_variance = stats.variance(event_probability_set, average = event_average_probablity) fringe_start_time = start_minute - fringe_time_for_event(tag) if fringe_start_time < 0: fringe_start_time = 1440 + fringe_start_time fringe_end_time = end_minute + fringe_time_for_event(tag) if fringe_end_time > 1440: fringe_end_time = fringe_end_time - 1440 if fringe_end_time > fringe_start_time: fringe_pmf = pmf[fringe_start_time:fringe_end_time] else: fringe_pmf = pmf[fringe_start_time:1439] fringe_pmf.extend(pmf[0:fringe_end_time]) fringe_average_probability = stats.average(fringe_pmf) fringe_variance = stats.variance(fringe_pmf, average = fringe_average_probability) start_hour = float(start_minute)/60.0 end_hour = float(end_minute)/60.0 e = Event.objects.create(user = user, tag = tag, start_time = start_hour, end_time = end_hour, day_of_week = day_of_the_week, probability = event_average_probablity, probability_variance = event_probability_variance, fringe_probability = fringe_average_probability, fringe_variance = fringe_variance) e.save()
d1 = Dog('fido', 3, 'male') d2 = Dog('sally', 5, 'female') print('before', d1) d1.eat() print('after eating', d1) d1.play() print('after playing', d1) # instructions # read in dogs.txt # create a dog for # every entry # add all dogs # to a dogs array # find the average age # of these dogs # and the standard deviation doglist = [] dogages = [] with open('dogs.txt') as f: for line in f: dog = line.strip().split(',') doglist.append(Dog(dog[0], int(dog[1]), dog[2])) dogages.append(int(dog[1])) avg_age = s.mean(dogages) age_std_dev = s.variance(dogages)**(1 / 2) print(doglist, avg_age, age_std_dev)
import stats def cargoArchivos(): datos1=pd.read_excel("Datos 01.xlsx",header=None) listaDatos1={col:datos1[col].dropna().tolist() for col in datos1.columns}[0] datos2=pd.read_excel("Datos 02.xlsx",header=None) listaDatos2={col:datos2[col].dropna().tolist() for col in datos2.columns}[0] datos3=pd.read_excel("Datos 03.xlsx",header=None) listaDatos3={col:datos3[col].dropna().tolist() for col in datos3.columns}[0] return listaDatos1,listaDatos2,listaDatos3 datos1,datos2,datos3=cargoArchivos() print("\nDatos Muestra 1") esperanzaDatos1 = stats.mean(datos1) varianzaDatos1 = stats.variance(datos1) lambdaa=esperanzaDatos1 print("La Esperanza Matematica de la muestra es = ",esperanzaDatos1) print("La Varianza Matematica de la muestra es = ",varianzaDatos1) print("El lambda de la muestra es = ",lambdaa) plt.hist(datos1, edgecolor='black', linewidth=1,density=1,label=(f'Lambda = {round(lambdaa,0)}')) y = [((math.exp(-6) * (6 ** x)) / (math.factorial(x))) for x in datos1] plt.plot(datos1,y,'o') plt.grid(True) plt.ylim(0,0.25) plt.text(9,0.15,"Amarrilo Dist.Poisson Lambda=6",color='black') plt.legend(title="Parametros") plt.title("Datos Muestra 1 - Distribucion Poisson") plt.xlabel("Intervalos") plt.ylabel("Frecuencia") plt.show()
def testVariance(self): self.assertEquals(self.population_variance, stats.variance(self.data, True)) self.assertEquals(self.sample_variance, stats.variance(self.data[:len(self.data)/2]))
def test_variance_throws_exception(self): with self.assertRaises(TypeError): var = variance([])
def test_variance(self): self.assertEqual(stats.variance([1, 2, 3, 4, 5]), 2.5)
""" This file contains some default imports and commonly used functions so that you don't have to write lots of code over and over for each problem. """ # CONFIG PROJECT_ROOT = '..' # relative location pointing to utils/ and stats.py # REST OF FILE import sys # allows import of project files (idk how else to do this) sys.path.insert(1, PROJECT_ROOT) from utils.webassign import array_from_shitstring_floats from stats import get_range, variance, standard_deviation o2_consumption = array_from_shitstring_floats( '29.6 49.4 31.0 28.4 28.8 25.4 34.0 29.8 23.8 30.1') print("O2 Consumption: {}".format(o2_consumption)) print("Sample range: {}".format(get_range(o2_consumption))) print("Sample variance: {}".format(variance(o2_consumption))) print("Standard deviation: {}".format(standard_deviation(o2_consumption)))
import numpy as np import stats # Load the column at the ith column, at the i-1 index def load_col(fname, i): return np.loadtxt(fname, usecols=(i-1,)) y = load_col("ludoarray.txt", 3) print("Column 3 (y) is: {}".format(y)) # Calculate the mean, variance, and std_dev of y mean = stats.mean(y) variance = stats.variance(y) std_dev = stats.std_dev(y) # Print the result of the calculations print("The mean of y is {:f} \nThe variance of y is {:f} \nThe std deviation of \ y is {:f}".format(mean,variance,std_dev))
def test_variance_throws_exception(self): """variance of an empty list should raise an exception""" with self.assertRaises(ValueError): var = variance([])
#내가 만든 directory에 path만들기 import sys sys.path.append('C:\\mypython') sys.path #잘못된 path 지우기 sys.path.remove('C:\\mypython') ######################################### [문제84] stats 모듈에 평균, 분산, 표준편차함수를 사용할수 있는 프로그램을 생성하세요. >>> import stats >>> stats.mean(1,2,3,4,5) 3.0 >>> stats.variance(1,2,3,4,5) 2.5 >>> stats.stddev(1,2,3,4,5) 1.5811388300841898 #stats에 각 함수를 포함하도록 하기 ########## c:\python\stats.py 답 import math def mean (*arg): return sum(arg)/len(arg) def variance(*arg): total = 0 m = mean(*arg)
import sys # allows import of project files (idk how else to do this) sys.path.insert(1, '..') from utils.webassign import array_from_shitstring from stats import mean, deviation_from_mean, variance, standard_deviation youngs_mod = array_from_shitstring(" 116.6 115.8 114.9 115.3 115.6 ") youngs_mod.sort() print("Young's Mod: {}".format(youngs_mod)) sample_mean = mean(youngs_mod) print("Young's Mod (mean): {}".format(sample_mean)) print("Deviation from mean:") deviation_list = deviation_from_mean(youngs_mod) for data_point, deviation in zip(youngs_mod, deviation_list): print("Sample: {0}, deviation from mean: {1}".format( data_point, deviation)) my_variance = variance(youngs_mod) print("Sample variance: {}".format(my_variance)) print("Standard deviation: {}".format(standard_deviation(youngs_mod)))
import stats stats = stats.Stats() list1 = [1,3,5,6] list2 = [1,3,4,5] total1 = stats.total(list1) total2 = stats.total(list2) mean1 = stats.mean(list1) mean2 = stats.mean(list2) mode1 = stats.mode(list1) mode2 = stats.mode(list2) median1 = stats.median(list1) median2 = stats.median(list2) variance1 = stats.variance(list1) variance2 = stats.variance(list2) standard_deviation1 = stats.SD(list1) standard_deviation2 = stats.SD(list2) covariance_pop = stats.covariance(list1, list2) covariance_sample = stats.covariance(list1, list2, True) correlation = stats.correlation(list1, list2) skewness_pop1 = stats.skewness(list1) skewness_pop2 = stats.skewness(list2) skewness_sample1 = stats.skewness(list1, True) skewness_sample2 = stats.skewness(list2, True) kurtosis_pop1 = stats.kurtosis(list1) kurtosis_pop2 = stats.kurtosis(list2) kurtosis_sample1 = stats.kurtosis(list1, True) kurtosis_sample2 = stats.kurtosis(list2, True)