Exemplo n.º 1
0
    def _addOne(self, _data_struct: DataStruct):
        index_value = _data_struct.index()[0]
        self.buf.append(_data_struct.getColumn(self.use_key)[0])

        if len(self.data) > self.period:
            const_std = statistics.pstdev(self.buf[-self.period:])
            self.dynamic_n *= const_std / self.prev_std
            self.dynamic_n = max(self.min_n, self.dynamic_n)
            self.dynamic_n = min(self.max_n, self.dynamic_n)
            tmp_n = int(round(self.dynamic_n))

            mean = statistics.mean(self.buf[-tmp_n:])
            std = statistics.pstdev(self.buf[-tmp_n:])

            self.data.addRow(
                [index_value, mean + self.rate * std,
                 mean, mean - self.rate * std],
                self.keys
            )

            self.prev_std = const_std
        else:
            if len(self.data) == self.period:
                self.prev_std = statistics.pstdev(self.buf)

            self.data.addRow(
                [index_value, None, None, None],
                self.keys
            )
Exemplo n.º 2
0
def output(nums, filesize, filetypec, filetypes, filetime):
    fsum = sum(filesize)
    print(('%s. %s' % (', '.join('%d %s' % vals for vals in filter(_ig1, zip(nums, ('files', 'directories', 'links', 'mount points', 'errors')))), '%s data.' % sizeof_fmt(fsum))).lstrip('. '))
    if not filesize:
        return
    if len(filesize) > 2:
        favg = fsum / len(filesize)
        stdev = statistics.pstdev(filesize, favg)
        print('File size: max %s, mean %s, median %s, stdev %s' % tuple(map(sizeof_fmt, (max(filesize), favg, statistics.median(filesize), stdev))))
        print(' µ+σ (68%): ' + sizeof_fmt(favg + stdev) +
              ', µ+2σ (95%): ' + sizeof_fmt(favg + stdev * 2))
        print('Modification time:')
        print(' min    ' + time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(min(filetime))))
        print(' max    ' + time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(max(filetime))))
        tavg = statistics.mean(filetime)
        print(' mean   ' + time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(tavg)))
        print(' median ' + time.strftime('%Y-%m-%d %H:%M:%S %Z', time.localtime(statistics.median(filetime))))
        print(' stdev  ' + timestring(statistics.pstdev(filetime, tavg)))
        print('File type by number:')
        mcomm = filetypec.most_common(5)
        count = sum(filetypec.values())
        print('\n'.join(' % 6s: %.2f%%' % (k or '<N/A>', v/count*100) for k, v in mcomm))
        print(' Others: %.2f%%' % ((count - sum(v for k, v in mcomm)) / count * 100))
        print('File type by size:')
        mcomm = filetypes.most_common(5)
        count = sum(filetypes.values())
        print('\n'.join(' % 6s: %.2f%%' % (k or '<N/A>', v/count*100) for k, v in mcomm))
        print(' Others: %.2f%%' % ((count - sum(v for k, v in mcomm)) / count * 100))
Exemplo n.º 3
0
def insertNormalizedModelInDB(idUser, idString, keystroke, isTest = False):
	insertNormalizedRecord = replaceIfIsTest("INSERT INTO `mdl_user#isTest_keystroke_normalized`(`id_user`, `id_string`) VALUES (%s, %s)", isTest);
	updateNormalizedRecord = replaceIfIsTest("UPDATE `mdl_user#isTest_keystroke_normalized` ", isTest);
	
		
	executeSqlInDB(insertNormalizedRecord, (idUser, idString));
	
	keyDimensionsExtractor = KeystrokeDimensionsExtractor(keystroke);
	
	#extracting dimensions
	timePressed = keyDimensionsExtractor.getTimePressed();
	#geting avarage and standardDeviation
	timePressedAverage = statistics.mean(timePressed);
	timePressedstandardDeviation = statistics.pstdev(timePressed);
	
	latencies = keyDimensionsExtractor.getLatencies();
	latenciesAverage = statistics.mean(latencies);
	latenciesStandardDeviation = statistics.pstdev(latencies);
	
	dbModel = {
		'id_user': idUser,
		'id_string': idString,
		'press_average': timePressedAverage,
		'latency_avarage': latenciesAverage,
		'press_standard_deviation': timePressedstandardDeviation,
		'latency_standard_deviation': latenciesStandardDeviation,
	}
	
	#update in table created before
	updateNormalizedRecord = updateNormalizedRecord + (" SET `press_average`= %(press_average)s,`latency_avarage`= %(latency_avarage)s, `press_standard_deviation`= %(press_standard_deviation)s,`latency_standard_deviation`= %(latency_standard_deviation)s " 
		" WHERE `id_user`= %(id_user)s AND `id_string`= %(id_string)s");
	executeSqlInDB(updateNormalizedRecord, dbModel);
Exemplo n.º 4
0
    def nutritionfacts(self):

        # print keys
        svgdata = ""
        frame_x = self.width * self.bins + 100 - 90
        frame_y = (self.graphheight + 700) // 2 + 25 - self.graphheight
        for i, s in enumerate([l for l in self.points if l[2]]):
            mu = "μ = —"
            sigma = "σ = —"
            if len(s[0]) != 0:
                xmean = stat.mean([t[0] for t in s[0]])
                xsigma = stat.pstdev([t[0] for t in s[0]], xmean)

                ymean = stat.mean([t[1] for t in s[0]])
                ysigma = stat.pstdev([t[1] for t in s[0]], ymean)

                mu = "μ = (" + str(round(xmean, 4)) + ", " + str(round(ymean, 4)) + ")"
                sigma = "σ = (" + str(round(xsigma, 4)) + ", " + str(round(ysigma, 4)) + ")"

            line_y = frame_y + i * 65
            svgdata += circle(frame_x - 4, line_y + 3, 2, s[1])
            svgdata += circle(frame_x + 4, line_y + 4, 2, s[1])
            svgdata += circle(frame_x - 1, line_y + 10, 2, s[1])

            svgdata += text(frame_x + 20, line_y + 10, s[2], align=-1, color=s[1], font="Neue Frutiger 65")
            svgdata += text(frame_x + 28, line_y + 25, "n = " + str(len(s[0])), align=-1, color=s[1])
            svgdata += text(frame_x + 28, line_y + 40, mu, align=-1, color=s[1])

            svgdata += text(frame_x + 28, line_y + 55, sigma, align=-1, color=s[1])
        self._frostbyte(svgdata)
Exemplo n.º 5
0
def morpheme_stdev(trie: MorphemeTrie, reverse_trie: MorphemeTrie) -> (float, float, float):
    trie_mpr, reverse_trie_mpr = (list(trie.morphemes_per_word()),
                                    list(reverse_trie.morphemes_per_word()))
    stdev_trie = pstdev(trie_mpr)
    stdev_reverse_trie = pstdev(reverse_trie_mpr)
    stdev_combined = pstdev(trie_mpr + reverse_trie_mpr)
    return stdev_trie, stdev_reverse_trie, stdev_combined
Exemplo n.º 6
0
def normalize_geojson(js_data):
	pols = []
	subs = []
	for js in js_data:
		pols.append(js.get_avg_polarity())
		subs.append(js.get_avg_subjectivity())
		
	max_pol = max(pols)
	min_pol = min(pols)
	max_sub = max(subs)
	min_sub = min(subs)
	old_range_pol = (max_pol - min_pol)
	old_range_sub = (max_sub - min_sub)

	for js in js_data:
		sub = (js.mAvgSubjectivity - min_sub) / old_range_sub
		sub -= .5
		sub *= 1.25
		sub += stats.pstdev(subs)/2
		sub += .5
		sub = max(0, sub)
		sub = min(1, sub)
		js.mAvgSubjectivity = sub

		pol = (js.mAvgPolarity - min_pol) / old_range_pol
		pol -= .5
		pol *= 1.25
		pol += stats.pstdev(pols)/2
		pol += .5
		pol = max(0, pol)
		pol = min(1, pol)
		js.mAvgPolarity = pol
	return js_data
Exemplo n.º 7
0
def sd_extreme_ex (a,b,c,d,e):

	normal_sd = pstdev([a,b,c,d,e])
	min_sd = 1000000000
	max_sd = 0
	val1 = 0
	val2 = 0
	val3 = 0
	val4 = 0
	val5 = 0
	for i in list(range(0,11)):#31
		for j in list(range(0,11)):
			for k in list(range(0,11)):
				for p in list(range(0,11)):
					for q in list(range(0,11)):
						val1 = a - i*0.1
						val2 = b - j*0.1
						val3 = c - k*0.1
						val4 = d - p*0.1
						val5 = e - q*0.1
				

						sd = pstdev([val1,val2,val3,val4,val5])
						if (sd>=max_sd):
							max_sd = sd
						if (sd<=min_sd):
							min_sd = sd
						#print('.',end="",flush=True)

	print(max_sd-normal_sd)					
	print(min_sd-normal_sd)
def pearson(A, B):
    M = len(A)
    assert M == len(B)
    A_mean = statistics.mean(A)
    A_stdev = statistics.pstdev(A)
    B_mean = statistics.mean(B)
    B_stdev = statistics.pstdev(B)
    cross_mean = sum(A[i] * B[i] for i in range(M)) / M
    return (cross_mean - A_mean * B_mean) / (A_stdev * B_stdev)
def calculate_mean(TE_program):
    my_directory = sys.argv[1] 
    files=''
    items = []
    for results_file in os.listdir(my_directory):
        match = re.findall("(FAMILY_TFPN_ALL*)",results_file)
        if len(match) >0:
            print "yes"
            print results_file
            files+=str(" {results_file}".format(**locals()))
    TPR_fam={}
    FDR_fam={}
    TPR_fam = defaultdict(list)
    FDR_fam = defaultdict(list)

    fam_found = {}

    #get rid of leading space...next tiem append space after file:
    files= files[1:]
    files_to_test = files.split(' ')
    for sim_file in files_to_test:
        OPEN_SIM_FILE = open(sim_file, "r")
        for line in OPEN_SIM_FILE:
            if re.search(TE_program,line):
                line = line.rstrip('\n')
                items= re.split("[\t]",line)   # WILL NEED TO CHANGE THESE
                M1 = items[0]
                fam = items[1]
                TPR = items[5]
                FDR = items[6]

                if TPR !="NA":
                    TPR_fam[fam].append(TPR)
                    fam_found[fam] = 0
                FDR_fam[fam].append(FDR)
    for key in FDR_fam.keys():
        print key
        print FDR_fam[key]

    for key in sorted(all_families.keys()):
        if key in fam_found.keys():
            TPR_fam[key] = map(float, TPR_fam[key]) #convert strings in list to integers
            mean_TPR = statistics.mean(TPR_fam[key])
            standard_deviation_TPR = statistics.pstdev(TPR_fam[key])
        else:
            mean_TPR = "NA"
            standard_deviation_TPR = "NA"

        FDR_fam[key] = map(float, FDR_fam[key]) 
        print key
        print FDR_fam[key]
        mean_FDR = statistics.mean(FDR_fam[key])
        standard_deviation_FDR = statistics.pstdev(FDR_fam[key])
        print "The mean_TPR is {mean_TPR}".format(**locals())
        print "The standard deviation TPR is {standard_deviation_TPR}".format(**locals())
        OUT.write ("{M1}\t{key}\t{mean_TPR}\t{mean_FDR}\t{standard_deviation_TPR}\t{standard_deviation_FDR}\n".format(**locals()))
Exemplo n.º 10
0
def eliminate_sd(alphabet,v):
	new_alphabet = []
	for i in alphabet:
		sd_min = pstdev(i[0])-0.5
		sd_max = pstdev(i[0])+0.5
		#if intersect(v-1,v,sd_min,sd_max):
		if intersect(v*0.5-0.5,v*0.5,sd_min,sd_max):
			new_alphabet.append(i)
	#print(new_alphabet)
	return (new_alphabet)
Exemplo n.º 11
0
def printOverTime(label, this_acc_over_time, this_conf_over_time):
	print('\n\n' + str(label))
	for numEvents in this_acc_over_time:
		accMean = st.mean(this_acc_over_time[numEvents])
		accStd = st.pstdev(this_acc_over_time[numEvents])

		confMean = st.mean(this_conf_over_time[numEvents])
		confStd = st.pstdev(this_conf_over_time[numEvents])

		print(str(numEvents) + '\t' + str(accMean) + '\t' + str(accStd) + '\t' + str(confMean) + '\t' + str(confStd))
Exemplo n.º 12
0
def get_meanCV(file):
    CSV_file = pandas.read_csv(file)
    expt_samples = len(CSV_file)
    DNAs = 7
    replicates = expt_samples/DNAs

    if "A13" in CSV_file["Well"].values:
        plate_map = Container(None, _CONTAINER_TYPES['384-pcr'])
    else:
        plate_map = Container(None, _CONTAINER_TYPES['96-pcr'])

    start = 0
    replicate_locs = []
    for i in range (0,DNAs-1):
        loc = [plate_map.humanize(s) for s in range(start, start + replicates)]
        replicate_locs.append(loc)
        start += replicates

    DNA_Ct = []
    for h in replicate_locs:    
        for x in h:
            Replicate_Ct_DNA = []
            data_source = open(file)    
            replicate_locations = h
            for line in data_source:
                split_line=line.split(',')
                wellID=split_line[0]
                Ct=split_line[3]
                for w in replicate_locations:
                    if w == wellID:
                        try:
                            Replicate_Ct_DNA.append(float(Ct))
                        except:
                            Replicate_Ct_DNA.append(0.0)
        DNA_Ct.append(Replicate_Ct_DNA)

    percentageCV = []
    for n in DNA_Ct:
        try:
            percentageCV.append(((stats.pstdev(n)/stats.mean(n))*100))
        except ZeroDivisionError as err:
            percentageCV.append(0.0)
    meanCV = stats.mean(percentageCV)
    for n in DNA_Ct:
        line = []
        line.append(stats.mean(n))
        line.append(stats.pstdev(n))
        mean_SD.append(line)
    writer = csv.writer(open('./output/mean_SD.csv', 'w'))
    writer.writerows(mean_SD)
    return meanCV
    def __init__(self, data_list):
        # Null values are counted as 0
        list_total = []
        # Without null values
        list = []
        self.total_filled = 0
        self.total_not_filled = 0
        self.quintilesX = []
        self.quintilesY = []

        for data in data_list:
            if data != "":
                list_total.append(int(data))
                list.append(int(data))
                self.total_filled += 1
            else:
                list_total.append(0)
                self.total_not_filled += 1

        if list != []:
            self.mean = round(mean(list), 2)
            self.standard_deviation = round(pstdev(list, self.mean), 2)
            minimum = min(list)
            maximum = max(list)

            quintile_length = math.floor((maximum - minimum + 1) / 5)
            # First 4 quintiles
            first = minimum
            for i in range(1, 5):
                second = first + quintile_length
                quintile_x = "[" + str(first) + ", " + str(second) + ")"
                self.quintilesX.append(quintile_x)
                quintile_y = 0
                for num in list:
                    if (first <= num) and (num < second):
                        quintile_y += 1
                self.quintilesY.append(quintile_y)
                first = second
            # Last quintile
            self.quintilesX.append("[" + str(first) + ", " + str(maximum) + "]")
            quintile_y = 0
            for num in list:
                if (first <= num) and (num <= maximum):
                    quintile_y += 1
            self.quintilesY.append(quintile_y)
        else:
            self.mean = 0
            self.standard_deviation = 0
        self.total_mean = round(mean(list_total), 2)
        self.total_standard_deviation = round(pstdev(list_total, self.total_mean), 2)
Exemplo n.º 14
0
    def calculate_feature_statistics(self, series_key='series'):
        for key in self.sound_files[0].analysis[series_key]:
            self.feature_statistics[key] = {
                'min': None,
                'max': None,
                'mean': None,
                'standard_deviation': None
            }

        for feature in self.feature_statistics:
            series = []
            for sf in self.sound_files:
                if isinstance(sf.analysis[series_key][feature][0], list):
                    series += Standardizer.join_lists(sf.analysis[series_key][feature])
                else:
                    series += sf.analysis[series_key][feature]

            if len(series) == 0:
                continue

            self.feature_statistics[feature]['min'] = min(series)
            self.feature_statistics[feature]['max'] = max(series)
            self.feature_statistics[feature]['mean'] = statistics.mean(series)
            self.feature_statistics[feature]['standard_deviation'] = statistics.pstdev(series)

        return self.feature_statistics
Exemplo n.º 15
0
    def _stat(nums, prefix, spec='aimsd'):
        """Compute general statistics for a given list of numbers.

        :param nums:        a list of numbers
        :type nums:         int or float
        :param prefix:      string prefix to append to dict keys returned
        :type prefix:       str
        :param spec:        features to compute, a=max, i=min, m=mean,
                            s=stdev, d=median, e=entropy
        :type spec:         str
        :return:            dictionary of features computed
        :rtype:             dict
        """
        if not nums:
            return {}

        d = {}
        if 'a' in spec:
            d[prefix + '_largest'] = max(nums)
        if 'i' in spec:
            d[prefix + '_smallest'] = min(nums)
        if 'm' in spec:
            d[prefix + '_mean'] = statistics.mean(nums)
        if 's' in spec:
            d[prefix + '_sd'] = statistics.pstdev(nums)
        if 'd' in spec:
            d[prefix + '_median'] = statistics.median(nums)
        if 'e' in spec:
            d[prefix + '_entropy'] = 0.0
            for val in nums:
                if val <= 0.0:
                    continue
                d[prefix + '_entropy'] += val * math.log(val, 2)
            d[prefix + '_entropy'] = -d[prefix + '_entropy']
        return d
Exemplo n.º 16
0
def print_score_dist(comments):
    # Print the distribution of scores.
    # The min, max, mean, median, std-dev.
    # Return the statistics in a hash.
    scores = [c['score'] for c in comments]

    min_score = min(scores)
    max_score = max(scores)
    mean_score = statistics.mean(scores)
    median_score = statistics.median(scores)
    pstdev_score = statistics.pstdev(scores)

    print("Comment score distribution")
    print("==========================")
    print("Min: {}, Max: {}".format(min_score, max_score))
    print("Mean: {:.4f}, Median: {}".format(mean_score, median_score))
    print("Population stdev: {:.4f}".format(pstdev_score))
    print()
    #TODO: Show chart here.

    return {
        'min': min_score,
        'max': max_score,
        'mean': mean_score,
        'median': median_score,
        'pstdev': pstdev_score,
    }
Exemplo n.º 17
0
def statistics_for_time_points(time_points: list, header: str) -> str:
    time_in_seconds = [t.total_seconds() for t in time_points]

    mean_time = time.strftime("%H:%M", time.gmtime(st.mean(time_in_seconds)))
    median_time = time.strftime("%H:%M", time.gmtime(st.median(time_in_seconds)))
    std_deviation = time.strftime("%H:%M", time.gmtime(st.pstdev(time_in_seconds)))
    try:
        mode_time = time.strftime("%H:%M", time.gmtime(st.mode(time_in_seconds)))
    except st.StatisticsError:
        mode_time = "-"
    min_time = time.strftime("%H:%M", time.gmtime(min(time_in_seconds)))
    max_time = time.strftime("%H:%M", time.gmtime(max(time_in_seconds)))

    value_width = 5
    key_width = len(header) - value_width

    row_format = "\n{{:<{key_width}}}{{:>{value_width}}}".format(key_width=key_width, value_width=value_width)
    delimiter = "\n" + "-" * len(header)

    stats_string = header
    stats_string += delimiter

    stats_string += row_format.format("Mean:", mean_time)
    stats_string += row_format.format("Median:", median_time)
    stats_string += row_format.format("Standard deviation:", std_deviation)
    stats_string += row_format.format("Mode:", mode_time)
    stats_string += row_format.format("Earliest:", min_time)
    stats_string += row_format.format("Latest:", max_time)
    stats_string += delimiter
    stats_string += "\n{} values".format(len(time_in_seconds))
    return stats_string
Exemplo n.º 18
0
def main():
	rain_prob = 0.3
	rain_distribution = {"mu": 35, "sigma": 5}
	sunny_distribution = {"mu": 50, "sigma": 8}

	avg_earnings = []
	sigma_up = []
	sigma_down = []
	h_w = []

	r = range(20,100)
	it = 1000
	for x in r:
		earnings_arr = []
		for i in range(it):
			earnings_arr.append(canillita(rain_prob, x, rain_distribution, sunny_distribution))

		avg = sum(earnings_arr)/it
		sigma = statistics.pstdev(earnings_arr)
		h_w.append((2 * sigma)/math.sqrt(it))
		sigma_up.append(avg + h_w[-1])
		sigma_down.append(avg - h_w[-1])
		avg_earnings.append(avg)
		print(str(avg) + '\n')	

	best_avg = max(avg_earnings)
	best_index = avg_earnings.index(best_avg)
	print('Best:')
	print('Earnings: ' + str(best_avg))
	print('Qty: ' + str(best_index + min(r)))
	print('Half width: ' + str(h_w[best_index]))
	mp.plot(r, avg_earnings, r, sigma_down, r, sigma_up)
	mp.show()
 def __calcTCPStatVals(self, UP, DN):
     if UP:
         upMean = mean(UP)
         upStDev = pstdev(UP)
     else:
         upMean = -1
         upStDev = -1
     if DN:
         dnMean = mean(DN)
         dnStDev = pstdev(DN)
     else:
         dnMean = -1
         dnStDev = -1
     #END IF/ELSEs
     return [upMean, upStDev, (upMean-upStDev),
             dnMean, dnStDev, (dnMean-dnStDev)]
Exemplo n.º 20
0
def get_morpheme_occurrence(trie: MorphemeTrie, reverse_trie: MorphemeTrie) -> (float,):
    """calculates data on how often each morpheme occurs in the corpus"""
    morphemes_ltr = Counter(chain.from_iterable(trie.morphemes))
    morphemes_rtl = Counter(chain.from_iterable(reverse_trie.morphemes))
    morpheme_occurrences_ltr = morphemes_ltr.values()
    morpheme_occurrences_rtl = morphemes_rtl.values()

    mean_occurrence_ltr = mean(morpheme_occurrences_ltr)
    mean_occurrence_rtl = mean(morpheme_occurrences_rtl)

    stdev_morphemes_ltr = pstdev(morpheme_occurrences_ltr, mu=mean_occurrence_ltr)
    stdev_morphemes_rtl = pstdev(morpheme_occurrences_rtl, mu=mean_occurrence_rtl)

    occurrence_data = ((mean_occurrence_ltr, mean_occurrence_rtl),
                        (stdev_morphemes_ltr, stdev_morphemes_rtl))
    return occurrence_data
def con_test(stat_list, player, stat_name): # Computes actual data points from data given via consist_test
    if stat_list:
        con_stat = (mean(stat_list), pvariance(stat_list), pstdev(stat_list))
        return con_stat
    else:
        print('NO DATA FOR: '+player+', '+stat_name)
        return 0
Exemplo n.º 22
0
def plot(data_name):
    data = {}
        
    # Load the data
    with open(data_name + '.csv', 'r') as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            for i in range(0,len(row)):
                data.setdefault(i,[])
                data[i].append(float(row[i]))
    
    # Compute means
    means = []
    for (t,values) in data.items():
        m = statistics.mean(values)
        d = statistics.pstdev(values)
        means.append(m)

    # Bag the results
    x = [int(round(x)) for x in numpy.logspace(0.1, 3, num=10, endpoint=True)]
    y = []
    for index_x in range(0, len(x)):
        start = x[index_x-1]-1 if index_x > 0 else 0
        end = x[index_x]
        y.append(statistics.mean(means[start:end]))

    # Plot        
    f = interp1d(x, y, kind='cubic')
    fig = plt.figure(figsize=(10, 5))
    plt.xscale('log')
    plt.xlabel('Number of entries in the journal')
    plt.ylabel('Response time in seconds')
    plt.plot(x, y)
    plt.plot(x, y, 'ro')
    plt.savefig(data_name + '.png',dpi=150)
Exemplo n.º 23
0
 def _addOne(self, _data_struct: DataStruct):
     index_value = _data_struct.index()[0]
     self.buf.append(_data_struct.getColumn(self.use_key)[0])
     self.data.addDict({
         self.idx_key: index_value,
         self.ret_key: statistics.pstdev(self.buf),
     })
Exemplo n.º 24
0
def log_tests(no_of_tests):
    global apicall

    print("Tests will finish around:", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time() + no_of_tests*6)))

    results = run_test(no_of_tests)

    for i, res in enumerate(results):
        res_mean = mean(res)
        res_pstdev = pstdev(res, res_mean)
        over_one_sec = 0
        timeout = 0


        fname = "result" + str(i) + ".csv"
        with open(fname, "w+") as f:
            for r in res:
                print(i, r)
                f.write(str(r) + "\n")
                if r > 0.5:
                    over_one_sec += 1
                if r == -1.0:
                    timeout += 1
                    
        #Remove all timeouts/errors (-1.0)
        res = list(filter(lambda x: x >= 0, res))
        
        fname2 = "result-summary.csv"
        with open(fname2, "a") as f2:
            f2.write(apicall[i] + "; " + str(len(res)) + "; " + str(res_mean) + "; " + str(res_pstdev) + "; " + str(over_one_sec) + "; " + str(max(res)) + "; " + str(timeout) + "\n")
Exemplo n.º 25
0
def stats_helper(list):
    """
    https://docs.python.org/3/library/statistics.html#statistics.pvariance
    :param list:
    :return:
    """

    mean = statistics.mean(list)
    mode = None

    try:
        mode = statistics.mode(list)
    except statistics.StatisticsError:
        # no unique mode
        pass

    return {
        'mean': mean,
        'variance': statistics.pvariance(list, mu=mean),
        'standard_deviation': statistics.pstdev(list, mu=mean),
        'median': statistics.median(list),
        'median_low': statistics.median_low(list),
        'median_high': statistics.median_high(list),
        'median_grouped': statistics.median_grouped(list),
        'mode': mode
    }
Exemplo n.º 26
0
 def RunSeason(self):
     import time
     
     print "Simulating approx. " + str(self.finaltotal) + " games."
     print "Mean " + str(2 * self.finaltotal / self.nplayers) + " games per player." 
     print "Standard deviation " + str(pstdev([P.ngames for P in self.liveplayers]))
     
     start_time = time.clock()
     
     while len(self.liveplayers) >= 2:
         self.PlayAGame()
         
         barlength = 38
         blocks = barlength * self.totalgames / self.finaltotal
         percent = 100*self.totalgames / self.finaltotal 
         text = "\rPercent: [{0}] {1}%".format( "=" * blocks + " " * (barlength - blocks), percent)
             
         sys.stdout.write(text)
         sys.stdout.flush()
         
     end_time = time.clock()
     elapsed_time = end_time - start_time
             
     self.histogram = [len(R) for R in self.deadranks]
     print "\n" + str(self.totalgames) + " games simulated in " + str(elapsed_time) + " seconds (" + str(self.totalgames / elapsed_time) + " games per second)."
Exemplo n.º 27
0
def print_stats(times, nodes):
    print("Nodes:")
    for node in nodes:
        print("    {}:{}:{}".format(node['address'], node['port'], node['job_slots']))
    pprint(times)
    stats = dict((key, dict(mean=statistics.mean(data), stdev=statistics.pstdev(data))) for key, data in times.items())
    pprint(stats)
 def calc_mean_std(self):
     '''
     Using power rankings data, calculates the mean and std for each team.
     Returns a dict of tuples in the form {Team1: (mean, std)}
     '''
     mean_std_dict = {team: (statistics.mean(self.pr_data[team]), statistics.pstdev(self.pr_data[team])) for team in self.pr_data}
     return mean_std_dict
Exemplo n.º 29
0
def concept_to_concept_threshold_char(concept,name):
	#print "Concept_to_Concept ", name
	
	conList = []
	docs = [(name + concept)]
	
        for x in range(0,len(concept)):
  	    docs.append((name+concept[0:x]))

        tfidf_matrix = tfidf_vectorizer.fit_transform(docs)
        
        matrix = cosine_similarity(tfidf_matrix[0:1], tfidf_matrix)
        #print matrix
	   
	for row in matrix:
            for x in row[1:]:
		conList.append(x)

	
	mean = statistics.mean(conList)
	stdev = statistics.pstdev(conList)

	thld = 1 - (Num_Deviations * stdev)
	#print abs(mean_confidence_interval(mean,stdev))
	#thld1 = thld - abs(mean_confidence_interval(mean,stdev)) 

	#if thld == 1:
	    #return
	#print statistics.pstdev(conList)

	#print thld
	#out = [thld,thld1]
	#wr.writerow(out)
	return thld
Exemplo n.º 30
0
def calculate_daily_stats(daily_ret):
    '''
    For a simulation of 1-day returns, calculate mean and std dev
    '''
    return {
        "mean": stats.mean(daily_ret), 
        "standard deviation": stats.pstdev(daily_ret)
        }
num = statistics.median_low(a)
print(num)

a = [1, 2, 3, 4, 5]
num = statistics.median_high(a)
print(num)

a = [1, 2, 3, 4]
num = statistics.median_grouped(a)
print(num)

a = [1, 2, 3, 4, 5, 4]
num = statistics.mode(a)
print(num)

a = [1, 2, 3, 4, 5, 1]
num = statistics.mode(a)
print(num)

a = [1, 2, 3, 4]
num = statistics.stdev(a)
print(num)

a = [1, 2, 3, 4]
num = statistics.pvariance(a)
print(num)

a = [1, 2, 3, 4]
num = statistics.pstdev(a)
print(num)
Exemplo n.º 32
0
conferences2[1] = scaler1.fit_transform(conferences2[1])
conferences2[1] = [round(x[0]) for x in conferences2[1]]
scaler2 = MinMaxScaler((0, 18))
participants2[1] = [[x] for x in participants[1]]
participants2[1] = scaler2.fit_transform(participants2[1])
participants2[1] = [round(x[0]) for x in participants2[1]]

xformatter = mdates.DateFormatter('%H:%M', tz=timezone(timedelta(hours=9)))

part_per_conf = []
for i in range(len(conferences[1])):
    if int(participants[1][i]) != 0:
        part_per_conf.append(conferences[1][i] / participants[1][i])

avg = statistics.mean(part_per_conf)
std = statistics.pstdev(part_per_conf)

last_conf_id = 0
stagnant_conf_num = 0
conferences_time = {}
active_conferences = []
for i, n in enumerate(conferences2[1]):
    if len(active_conferences) < n:
        stagnant_conf_num = 0
        curr_total_participants = 0
        for ac in active_conferences:
            for p in ac['participants']:
                curr_total_participants += p['num']
        expected_total_participants = participants2[1][i]
        approx_part_per_conf = (expected_total_participants -
                                curr_total_participants) / (
Exemplo n.º 33
0
def test_node_load_consistent_time(tconf, change_checkpoint_freq,
                                   disable_node_monitor_config, looper,
                                   txnPoolNodeSet, tdirWithPoolTxns,
                                   allPluginsPath, poolTxnStewardData, capsys):

    # One of the reason memory grows is because spylog grows
    client, wallet = buildPoolClientAndWallet(poolTxnStewardData,
                                              tdirWithPoolTxns,
                                              clientClass=TestClient)
    looper.add(client)
    looper.run(client.ensureConnectedToNodes())

    client_batches = 300
    txns_per_batch = 25
    time_log = []
    warm_up_batches = 10
    tolerance_factor = 2
    print_detailed_memory_usage = False
    from pympler import tracker
    tr = tracker.SummaryTracker()
    node_methods_to_capture = [
        TestNode.executeBatch, TestNode.recordAndPropagate,
        TestNode.domainDynamicValidation, TestNode.domainRequestApplication
    ]
    times = {
        n.name: {meth.__name__: []
                 for meth in node_methods_to_capture}
        for n in txnPoolNodeSet
    }

    for node in txnPoolNodeSet:
        for meth in node_methods_to_capture:
            meth_name = meth.__name__
            patched = timeit(getattr(node, meth_name),
                             times[node.name][meth_name])
            setattr(node, meth_name, patched)

    for i in range(client_batches):
        s = perf_counter()
        sendReqsToNodesAndVerifySuffReplies(looper,
                                            wallet,
                                            client,
                                            txns_per_batch,
                                            override_timeout_limit=True)
        t = perf_counter() - s
        with capsys.disabled():
            print('{} executed {} client txns in {:.2f} seconds'.format(
                i + 1, txns_per_batch, t))
            print('--------Memory Usage details start')
            for node in txnPoolNodeSet:
                # print(sys.getsizeof(node))
                print('---Node {}-----'.format(node))
                # print('Requests {}'.format(asizeof.asizeof(node.requests, detail=1)))
                print(
                    get_memory_usage(node,
                                     print_detailed_memory_usage,
                                     get_only_non_empty=True))
                for r in node.replicas:
                    print('---Replica {}-----'.format(r))
                    print(
                        get_memory_usage(r,
                                         print_detailed_memory_usage,
                                         get_only_non_empty=True))

            # if i % 3 == 0:
            #     tr.print_diff()
            print('--------Memory Usage details end')
            for node in txnPoolNodeSet:
                for meth in node_methods_to_capture:
                    ts = times[node.name][meth.__name__]
                    print('{} {} {} {}'.format(node, meth.__name__, mean(ts),
                                               ts))

        if len(time_log) >= warm_up_batches:
            m = mean(time_log)
            sd = tolerance_factor * pstdev(time_log)
            assert m > t or abs(t - m) <= sd, '{} {}'.format(abs(t - m), sd)
        time_log.append(t)
        # Since client checks inbox for sufficient replies, clear inbox so that
        #  it takes constant time to check replies for each batch
        client.inBox.clear()
        client.txnLog.reset()
Exemplo n.º 34
0
idadeordenada = sorted(df['AgeAtHeartAttack'].tolist())

idadeMedia = df['AgeAtHeartAttack'].mean()
idadeModa = df['AgeAtHeartAttack'].mode()
idadeMediana = statistics.median(idade)
idadePontoMedio = (idadeordenada[0] +
                   idadeordenada[len(idadeordenada) - 1]) / 2

print("Média de pessoas que sofreram de ataques cardíacos no estudo")
print("Média = " + str(idadeMedia))
print("Moda = " + str(idadeModa[0]))
print("Mediana = " + str(idadeMediana))
print("Ponto Médio = " + str(idadePontoMedio))

idadeAmplitude = idadeordenada[len(idadeordenada) - 1] - idadeordenada[0]
idadeDesvioPadrao = statistics.pstdev(idade)
idadeVariancia = statistics.pvariance(idade)
idadeCoeficienteVariacao = (idadeDesvioPadrao / idadeMedia) * 100

print("\nMedidas de dispersão da idade dos pacientes registrados")
print("Amplitude = " + str(idadeAmplitude))
print("Desvio Padrão = " + str(idadeDesvioPadrao))
print("Variância = " + str(idadeVariancia))
print("Coeficiente de Variação = " + str(round(idadeCoeficienteVariacao, 2)) +
      "%\n")

idade = df['AgeAtHeartAttack']
idade_descri = idade.describe()

q1 = idade_descri['25%']
mediana = idade_descri['50%']
Exemplo n.º 35
0
 def testStdev(self, stdev):
     if stat.pstdev(self.List) == stdev:
         return True
     else:
         return False
for r in all_records:
    d = r.date
    d = d.split('-')
    d = '/'.join(d[1:])
    m = r.tmin
    if measure == 'avg':
        m = r.tavg
    elif measure == 'max':
        m = r.tmax
    temps_by_date[d].append(float(m))

all_stat_results = []
StatResults = namedtuple('StatResults', 'date avg stdev temp_to_use')
for d, temps in temps_by_date.items():
    avg = statistics.mean(temps)
    stdev = statistics.pstdev(temps)
    temp_to_use = int(round(avg + stdev_multiplier * stdev))
    avg = int(round(avg))
    stdev = round(stdev, 1)
    stat_results = StatResults(d, avg, stdev, temp_to_use)
    all_stat_results.append(stat_results)

x = []
y = []
for sr in all_stat_results:
    month, day = sr.date.split('/')
    month = int(month)
    day = int(day)
    m = sr.temp_to_use
    x.append(m)
    y.append(get_distance_from_mid_jan(month, day))
def main():

    ExcelFileName = "Data.xlsx"
    workbook = xlsxwriter.Workbook(ExcelFileName)
    worksheet = workbook.add_worksheet()
    HorizAlign = workbook.add_format()
    HorizAlign.set_align('center')
    LastRowAvailable = 0

    worksheet.write(LastRowAvailable, 0, "Cycle")
    worksheet.write(LastRowAvailable, 1, "Stats")

    for Class in range(Classes):
        worksheet.write(LastRowAvailable, 2 + Class, "R" + str(Class))

    LastRowAvailable += 1

    for root, dirs, files in os.walk(
            'C:\Taima_Furuyama\Mestrado_UNIFESP\Programacao\Python\Simulacoes\TestesEstatisticos\ENVELOPE'
    ):
        SourceFiles = [_ for _ in files if _.endswith('.xlsx')]

        NumberOfFiles = len(SourceFiles)
        print(NumberOfFiles)

        PercentArray = [[[None for i in range(NumberOfFiles)]
                         for x in range(Classes)] for y in range(Cycles)]

        for xlsfile in SourceFiles:
            SourceFile = xlrd.open_workbook(os.path.join(root, xlsfile))
            SourceSheet = SourceFile.sheet_by_index(0)

            for Cycle in range(Cycles):  #Ciclos
                for Class in range(Classes):  #Classes R
                    RParticles = SourceSheet.cell_value(rowx=Cycle + 1,
                                                        colx=Class + 8)
                    CycleTotal = SourceSheet.cell_value(rowx=Cycle + 1, colx=1)

                    Percent = (RParticles / CycleTotal) * 100

                    PercentArray[Cycle][Class].pop(0)
                    PercentArray[Cycle][Class].append(Percent)

#        print(PercentArray[9][10][0])
#        print(len(PercentArray[9][10]))

        MeanArray = [[None for x in range(Classes)] for y in range(Cycles)]
        MedianArray = [[None for x in range(Classes)] for y in range(Cycles)]
        StdDevArray = [[None for x in range(Classes)] for y in range(Cycles)]
        MinArray = [[None for x in range(Classes)] for y in range(Cycles)]
        MaxArray = [[None for x in range(Classes)] for y in range(Cycles)]
        VarArray = [[None for x in range(Classes)] for y in range(Cycles)]

#        print(MeanArray)

# these 2 FOR LOOPS calculate the statistical parameters
    for Cycle in range(Cycles):
        for Class in range(Classes):
            MeanArray[Cycle].pop(0)
            Mean = statistics.mean(PercentArray[Cycle][Class])
            MeanArray[Cycle].append(Mean)

            MedianArray[Cycle].pop(0)
            Median = statistics.median(PercentArray[Cycle][Class])
            MedianArray[Cycle].append(Median)

            StdDevArray[Cycle].pop(0)
            StdDev = statistics.pstdev(PercentArray[Cycle][Class])
            StdDevArray[Cycle].append(StdDev)

            MinArray[Cycle].pop(0)
            Min = min(PercentArray[Cycle][Class])
            MinArray[Cycle].append(Min)

            MaxArray[Cycle].pop(0)
            Max = max(PercentArray[Cycle][Class])
            MaxArray[Cycle].append(Max)

            VarArray[Cycle].pop(0)
            Var = statistics.pvariance(PercentArray[Cycle][Class])
            VarArray[Cycle].append(Var)

#    print(MeanArray)
#    print(MedianArray)
#    print(StdDevArray)
#    print(MinArray)
#    print(MaxArray)

# these 2 FOR LOOPS write the data to a Excel file
    for Cycle in range(Cycles):
        worksheet.write(LastRowAvailable, 0, Cycle)

        for Class in range(Classes):
            worksheet.write(LastRowAvailable, 1, "Mean")
            worksheet.write(LastRowAvailable + 1, 1, "Median")
            worksheet.write(LastRowAvailable + 2, 1, "StdDev")
            worksheet.write(LastRowAvailable + 3, 1, "Min")
            worksheet.write(LastRowAvailable + 4, 1, "Max")
            worksheet.write(LastRowAvailable + 5, 1, "Var")

            worksheet.write(LastRowAvailable, Class + 2,
                            MeanArray[Cycle][Class])
            worksheet.write(LastRowAvailable + 1, Class + 2,
                            MedianArray[Cycle][Class])
            worksheet.write(LastRowAvailable + 2, Class + 2,
                            StdDevArray[Cycle][Class])
            worksheet.write(LastRowAvailable + 3, Class + 2,
                            MinArray[Cycle][Class])
            worksheet.write(LastRowAvailable + 4, Class + 2,
                            MaxArray[Cycle][Class])
            worksheet.write(LastRowAvailable + 5, Class + 2,
                            VarArray[Cycle][Class])

        LastRowAvailable += 6

    workbook.close()
Exemplo n.º 38
0
 def updatePopStdDev(self, index):
     self.updatePopList()
     self.popStdDevs[index] = statistics.pstdev(self.populationLists[index])
    urlformat = "https://www.basketball-reference.com/leagues/NBA_{0}_games-{1}.html".format(year, month)
    urllist.append(urlformat)
pointdifferentials = []
for u in urllist:
    url = u
    html = urlopen(url)
    soup = BeautifulSoup(html, features='html.parser')
    visitor = soup.findAll('td', {'data-stat': 'visitor_pts'})
    home = soup.findAll('td', {'data-stat': 'home_pts'})
    convertText(visitor)
    convertText(home)
    for a in range(len(visitor)):
        pointdifferentials.append(abs(visitor[a]-home[a]))
#print(pointdifferentials)
average_pointdifferential = average(pointdifferentials)
population_deviation = stat.pstdev(pointdifferentials)
#print(population_deviation)
print("Teams this season won their games by an average of", str(average_pointdifferential), "points.")
#This is the section for the statistics for a team that season.
team = input('Enter team abbreviation (e.g. ATL for Atlanta Hawks): ')
teamurl = 'https://www.basketball-reference.com/teams/{0}/{1}_games.html'.format(team, year)
html = urlopen(teamurl)
soup = BeautifulSoup(html, features='html.parser')
regseason = soup.findAll('div', {'id': 'all_games'})
for games in regseason:
    selfscore = games.find_all('td', {'data-stat': 'pts'})
    oppscore = games.find_all('td', {'data-stat': 'opp_pts'})
    selfscores = []
    oppscores = []
    for score1, score2 in zip(selfscore, oppscore):
        selfscores.append(int(score1.text))
names = [
    'NB', 'KNN', 'SVC', 'RF', 'LR', 'LSTM_onFly', 'LSTM_Pre', 'LSTM_Bi', 'GRU'
]

accuMean = []
f1Mean = []
precisionMean = []
recallMean = []

accuSD = []
f1SD = []
precisionSD = []
recallSD = []

for i in range(accuDF.shape[1]):
    accuSD.append(st.pstdev(accuDF.iloc[:, i]))
    f1SD.append(st.pstdev(f1DF.iloc[:, i]))
    precisionSD.append(st.pstdev(precDF.iloc[:, i]))
    recallSD.append(st.pstdev(recDF.iloc[:, i]))

    accuMean.append(st.mean(accuDF.iloc[:, i]))
    f1Mean.append(st.mean(f1DF.iloc[:, i]))
    precisionMean.append(st.mean(precDF.iloc[:, i]))
    recallMean.append(st.mean(recDF.iloc[:, i]))

df_metric100 = pd.DataFrame({
    'Accuracy_Mean': accuMean,
    'Accuracy_SD': accuSD,
    'Precision_Mean': precisionMean,
    'Precision_SD': precisionSD,
    'Recall_Mean': recallMean,
Exemplo n.º 41
0
import statistics
import codecs
import ast

# initializing list
test_list = [11.43, 0.0, 3.21]

print('sum:', sum(test_list))
average = round(sum(test_list) / len(test_list), 2)
res = round(statistics.pstdev(test_list), 2)

print(str(average) + '$\pm$' + str(res))
'''
67.93/3.31
'''


def compute(test_list):
    average = round(sum(test_list) / len(test_list), 2)
    res = round(statistics.pstdev(test_list), 2)
    # print(str(average)+'$'+"\\"+'pm$'+str(res))
    return str(average) + '$\\pm$' + str(res)


def extract(flag):
    filenames = [
        'log.nobase.entail.v2.' + flag + '.seed.42.txt',
        'log.nobase.entail.v2.' + flag + '.seed.16.txt',
        'log.nobase.entail.v2.' + flag + '.seed.32.txt'
    ]
    result_lists = []
Exemplo n.º 42
0
	if Nb_Simulation == 1 :
		print('Montant disponible au départ = ' , MiseDepart , ' €')
		print(' Après ' + str(i) + ' lancé(s) vous disposez de : ' + str(round(Prime,2))+'€' )
		fig = plt.figure(0)
		fig.canvas.set_window_title('Jeu Pile ou Face')
		plt.axis([0, Nb_Lance, 0, PrimeMax+MiseDepart])
		plt.plot(ListeLance,ListePrime )
		plt.title("Evolution des gains")
		plt.xlabel('Nb Lancé(s)'+  ' (Après ' + str(i) + ' lancé(s) vous disposez de : ' + str(round(Prime,2))+'€)' )
		plt.ylabel('Montant en € - (Montant initial = ' + str(MiseDepart) + '€)')
		fig.show()
	elif Nb_Simulation > 1 :
		if mean(ListeGains)>0:
			TxtMoyenne = 'gain'+ ' est de : ' + str(round(mean(ListeGains),2))+'€'
		else :
			TxtMoyenne = 'perte'+ ' est de : ' + str(round(mean(ListeGains),2)*-1)+'€'
		print(' Après ' + str(j) + ' simulation(s) de '+str(i)+ ' lancé(s) avec un montant initial de ' \
			+ str(MiseDepart) + ' € : '+ ' en Moyenne votre ' + TxtMoyenne  + ' avec un Ecart-type de : ' \
			+ str(round(pstdev(ListeGains),2))+'€' )



'''
Commentaires : 

# en faisant comme ci-dessous, tu te permets de lancer ton programme plusieurs fois

'''

stoch_process(3,5) #3 simulations , 5 lancés
stoch_process(2,8) #2 simulations , 8 lancés
Exemplo n.º 43
0
def stdev_percent(numbers):
    return 100 * pstdev(numbers) / mean(numbers)
Exemplo n.º 44
0
def desvio(execution_times):
    return str(sts.pstdev(execution_times))
Exemplo n.º 45
0
def main():

    section = 'default'
    secrets = configparser.RawConfigParser()
    secrets.read('./secrets.ini')

    config = {
        "apiKey": secrets.get(section, 'firebase_api_key'),
        "authDomain": secrets.get(section, 'firebase_auth_domain'),
        "databaseURL": secrets.get(section, 'firebase_database_url'),
        "storageBucket": secrets.get(section, 'firebase_storage_bucket')
    }

    firebase = pyrebase.initialize_app(config)

    firebase_user = secrets.get(section, 'firebase_user')
    firebase_pass = secrets.get(section, 'firebase_pass')

    auth = firebase.auth()
    user = auth.sign_in_with_email_and_password(firebase_user, firebase_pass)

    db = firebase.database()

    count = 0
    detector_avg = 0
    detector_std = 0
    accel_vals = []
    detected_count = 0
    state = 0

    det = get_detector(1, DETECTOR_THRESHOLD)

    try:
        while True:
            d = det()
            detector_count = d[0]

            accel_avg = get_value(RANDOM_ACCEL_MIN, RANDOM_ACCEL_MAX)
            accel_vals.append(accel_avg)

            if accel_avg > ACCEL_THRESHOLD:
                detected_count += 1

            # end of interval reached
            if detector_count == DETECTOR_THRESHOLD:
                # calculate STD and AVG
                detector_avg = int(statistics.mean(accel_vals))
                detector_std = int(statistics.pstdev(accel_vals))

                if ((detected_count >= DETECTED_THRESHOLD)
                        and ((detector_std > DETECTOR_STD_THRESHOLD_1) or
                             ((detector_std > DETECTOR_STD_THRESHOLD_2) and
                              (detector_avg < DETECTOR_AVG_THRESHOLD)))):
                    state = 1
                else:
                    state = 0

                accel_vals = []

            # send to Firebase
            feed(db, user, accel_avg, detector_count, detected_count,
                 detector_std, detector_avg, state)

            if detector_count == DETECTOR_THRESHOLD:
                detected_count = 0

            count += 1
            time.sleep(5)
    except KeyboardInterrupt:
        print("\nAnd we are done. Data sets sent: {}".format(count))
Exemplo n.º 46
0
def main():

    bar_colors = [
        colors.cnames['hotpink'], colors.cnames['deepskyblue'],
        colors.cnames['cornflowerblue']
    ]
    bar_labels = ['optimized', 'fixed1', 'fixed2']

    fig = plt.figure(figsize=(30, 10), dpi=220)

    plt.subplots_adjust(wspace=0.4)
    plt.rcParams['font.family'] = 'arial'
    rc('font', weight='bold')

    # 左のグラフの描画
    plt.subplot(1, 3, 1)

    method_num = 3
    user_num = 2

    raw_data = [[[] for i in range(user_num)] for j in range(method_num)]
    data = [[] for i in range(method_num)]
    yerr = [[] for i in range(method_num)]

    with open('experiment_data.csv', 'r') as f:
        reader = csv.reader(f)
        next(reader)

        for row in reader:
            for i in range(method_num):
                for j in range(user_num):
                    raw_data[i][j].append(row[8 + i + j * method_num])

        for i in range(method_num):
            for q in raw_data[i]:
                q_data = [int(x) for x in q]
                data[i].append(statistics.mean(q_data))
                yerr[i].append(
                    statistics.pstdev(q_data) / math.sqrt(len(q_data)))

    x = np.arange(user_num)
    bar_width = 0.2

    plt.grid(which='major', axis='y', color='black', linestyle='--', alpha=0.3)
    plt.tick_params(bottom=False)

    for i in range(method_num):
        plt.bar(x + i * bar_width,
                data[i],
                color=bar_colors[i],
                width=bar_width,
                yerr=yerr[i],
                capsize=6,
                label=bar_labels[i],
                align='center')

    plt.ylim([50, 90])
    plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%.2f'))
    plt.ylabel('Mean accuracy rate [%]', fontsize=44, weight='bold')
    plt.yticks([50.00, 60.00, 70.00, 80.00, 90.00], fontsize=44)

    plt.xlabel('(a)', fontsize=44, weight='bold')
    plt.xticks(x + bar_width, ['main user', 'bystander'], fontsize=44)

    plt.legend(bbox_to_anchor=(0.6, 1.2),
               loc='upper left',
               borderaxespad=0,
               fontsize=44,
               ncol=3)

    # 中央のグラフの描画
    plt.subplot(1, 3, 2)

    raw_data = [[] for i in range(method_num)]
    with open('experiment_data.csv', 'r') as f:
        reader = csv.reader(f)
        next(reader)

        for row in reader:
            for i in range(method_num):
                raw_data[i].append(int(row[14 + i]) / 1000)

    data = []
    yerr = []
    for i in range(method_num):
        data.append(statistics.mean(raw_data[i]))
        yerr.append(
            statistics.pstdev(raw_data[i]) / math.sqrt(len(raw_data[i])))

    x = np.arange(method_num)
    bar_width = 1.0

    plt.grid(which='major', axis='y', color='black', linestyle='--', alpha=0.3)
    plt.tick_params(bottom=False)

    plt.bar(x,
            data,
            color=bar_colors,
            width=bar_width,
            yerr=yerr,
            capsize=6,
            align='center')

    plt.ylim([130, 180])
    plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%d'))
    plt.ylabel('Mean time [s]', fontsize=44, weight='bold')
    plt.yticks([130, 140, 150, 160, 170, 180], fontsize=44)

    dummy = ['a'] * method_num
    plt.xlim([-3, 5])
    plt.xlabel('(b)', fontsize=44, weight='bold')
    plt.xticks(x, dummy, fontsize=44, color=colors.cnames['white'])

    # 右のグラフの描画
    plt.subplot(1, 3, 3)

    raw_data = [[] for i in range(method_num)]
    with open('experiment_data.csv', 'r') as f:
        reader = csv.reader(f)
        next(reader)

        for row in reader:
            for i in range(method_num):
                raw_data[i].append(int(row[17 + i]))

    data = []
    yerr = []
    for i in range(method_num):
        data.append(statistics.mean(raw_data[i]))
        yerr.append(
            statistics.pstdev(raw_data[i]) / math.sqrt(len(raw_data[i])))

    plt.grid(which='major', axis='y', color='black', linestyle='--', alpha=0.3)
    plt.tick_params(bottom=False)

    plt.bar(x,
            data,
            color=bar_colors,
            width=bar_width,
            yerr=yerr,
            capsize=6,
            align='center')
    plt.ylim([10, 50])
    plt.gca().yaxis.set_major_formatter(FormatStrFormatter('%d'))
    plt.ylabel('Mean # of item selections', fontsize=44, weight='bold')
    plt.yticks([10, 20, 30, 40, 50], fontsize=44)

    plt.xlim([-3, 5])
    plt.xlabel('(c)', fontsize=44, weight='bold')
    plt.xticks(x, dummy, fontsize=44, color=colors.cnames['white'])

    add_asterisk(0, 2, x, bar_width, data, yerr)
    add_asterisk(1, 2, x, bar_width, data, yerr)

    output_file_name = 'task_variables_result.eps'
    plt.savefig(output_file_name, bbox_inches='tight', pad_inches=0.2)

    # latexでのずれを防ぐため,一度pdfにしてepsに戻すスクリプトを実行
    subprocess.run(['./epsbound.sh', output_file_name])
Exemplo n.º 47
0
def calculate_std(param):
    user_list = list(param["elements"])
    std_value = float(pstdev(user_list))
    return {"std": round(std_value, 3)}
Exemplo n.º 48
0
# of the two
median([50, 52, 53])
### 52
median([51, 50, 52, 53])
### 51.5

# mode - item that occurs most often
>>> mode([51, 50, 52, 53, 51, 51])		     
51

# Standard Deviation - divides by N-1
stdev([51, 50, 52, 53, 51, 51])
### 1.0327955589886444

# Population Standard Deviation - divides by N
pstdev([51, 50, 52, 53, 51, 51])  
### 0.9428090415820634

# See Transcripts for explanation of difference between Population Std Dev and
# Std dev

#  list concatenation 
s = [10, 20, 30]
t = [40, 50, 60]
u = s + t
u		     
### [10, 20, 30, 40, 50, 60]  numpy would do an element by element addition

# Slicing - first two items
u[:2]
### [10, 20]
Exemplo n.º 49
0
    def algoritmo(self):
        """if self.RNI.getint(0) == 1:
            logging.info("Numero de RE")
            isContinue = True
        elif self.RVR.getint(0) == 1:
            logging.info("Desviacion")
        else:
            print(self.RVR.info)
            isContinue = False
            print("sin sistema de paro")"""
        paro = 1
        menores = []
        if self.isReady:
            _individuos = int(self.txtNumIndividuos.get())
            _origen = int(self.txtOrigen.get())
            _ciudades = self.ciudades
            _repeticiones = int(self.txtRepeticiones.get())
            _mejorGrafica = [-1]
            _ciudadesText = self.txtNumCiudades.get()
            logging.info("Iniciando Algoritmo...")
            start_time = time()

            Poblacion = []

            for i in range(_individuos):
                Poblacion.append(Funciones.individuo(_ciudades, _origen))

            for ind in range(_individuos):
                print("Individuo ", (ind + 1), ": ", Poblacion[ind])

            Fitness = Funciones.evaluar(Poblacion, len(Poblacion), _ciudades, self.listaCiudades)

            secuencia = 1
            limite = _repeticiones
            res = 'x'
            isContinue = True

            while isContinue:
                logging.info("Repeticion " + str(secuencia))

                PosPadres = Funciones.torneo(_individuos, Fitness)
                logging.info("Posiciones padres: " + str(PosPadres))

                _probCruce = float(self.txtProbCruce.get())
                _probMut = 1 / _ciudades
                Hijos = []

                if round(random.random(), 3) <= _probCruce:
                    Hijos = Funciones.cruce(Poblacion, PosPadres, _ciudades)
                else:
                    for i in range(2):
                        Hijos.append(Poblacion[PosPadres[i]])

                if round(random.random(), 3) <= _probMut:
                    Funciones.mutacion(Hijos, _ciudades)
                    logging.debug("MUTACION AQUI")

                FitnessHijos = Funciones.evaluar(Hijos, len(Hijos), _ciudades, self.listaCiudades)

                Mejores = Funciones.seleccionDirecta(Poblacion, PosPadres, Fitness, Hijos, FitnessHijos)
                Funciones.remplazo(Poblacion, PosPadres, Fitness, Mejores)
                # TODO: Mejor de todos

                # if secuencia >= limite:
                #  Hilo_respuesta = threading.Thread(name="HRespuesta", target=self.respuesta())
                # Hilo_respuesta.start()
                # res = self.resp
                # logging.debug("respuesta " + str(res))
                # if res is True:
                #   limite += _repeticiones
                #  res = 'x'
                # elif res is not 'x':
                #   isContinue = False

                _mejor = Funciones.mejor(Poblacion, Fitness)

                if _ciudadesText == 'test':
                    if _mejor[0] is 14:
                        isContinue = False

                if _mejorGrafica[0] is not _mejor[0]:
                    _mejorGrafica = _mejor
                    Grafica.grafica(1, _mejorGrafica[1], _mejorGrafica[0], secuencia, self.listaCiudades,
                                    (_ciudades + 1), 15)
                menores.append(_mejor[0])
                if len(menores) > 10000:
                    if paro == 1:
                        if menores.count(_mejor[0]) >= 1000:
                            isContinue = False
                            print(menores.count((_mejor[0])))
                    elif paro == 2:
                        desviacion = stats.pstdev(Fitness)
                        print(desviacion)
                        if desviacion <= 0.5:
                            isContinue = False

                secuencia += 1

            print("<--TERMINADO-->")

            for i in range(_individuos):
                print("Fitness ", i, ": ", Fitness[i], " --> Individuo: ", Poblacion[i])

            _mejor = Funciones.mejor(Poblacion, Fitness)
            print("Individuo ", _mejor[2], ": ", _mejor[1])
            print("Fitness: ", _mejor[0])

            elapsed_time = time() - start_time
            print(elapsed_time)
        else:
            print("Necesito Ciudades :(")
    for day in range(days):
        header += "%02d | " % day
    print(header)

    for nurse in range(nurses):
        s = "   %d   | " % (nurse + 1)
        for day in range(days):
            s += ("   | ", "XX | ")[samples.first.sample[index(nurse, day)]]
        print(s)

    ## Check Hamming distances between consecutive states
    distances = []
    for i in range(len(samples.record.sample) - 1):
        h = HammingDistance(samples.record.sample[i],
                            samples.record.sample[i + 1])
        distances.append(h)

    mean_h = mean(distances)
    std_h = pstdev(distances)

    hDistances[nurses, days] = (mean_h, std_h)

    # Fraction of solutions in ground state
    numGs = sum(r.num_occurrences for r in samples.record
                if isclose(r.energy, gs, abs_tol=1e-3))
    p = numGs / numSamples
    gsFractions
[nurses, days] = p

print(gsFractions)
print(hDistances)
def P(X, Y,n):
    covariance = [(i - mean(X)) * (j - mean(Y)) for i, j in zip(X, Y)]
    pearson_correlation = sum(covariance) / (n * pstdev(X, mean(X)) * pstdev(Y, mean(Y)))
    return pearson_correlation
Exemplo n.º 52
0
            tObject, tAmbient = MLX90615.Read_MLX90615_Temperatures()
            sleep_ms(TIME_MS_BETWEEN_TEMPERATURE_MEASUREMENTS)
            if (MIN_TEMPERATURE_ACCEPTABLE <= tObject <=
                    MAX_TEMPERATURE_ACCEPTABLE):
                measurements += 1
                listTObject.append(tObject)
                listTAmbient.append(tAmbient)

            print("Medindo, fique parado")

            # testa condição para habilitar flag_exit; ???
        if (measurements == NUM_TEMPERATURE_MEASUREMENTS):
            meanTObjecto = statistics.mean(listTObject)
            meanTAmbient = statistics.mean(listTAmbient)

            stadevTObjecto = statistics.pstdev(listTObject)
            stadevTAmbient = statistics.pstdev(listTAmbient)

            #if( (max(listTObject) < meanTObjecto+5) and (min(listTObject) > meanTObjecto-5)):
            if (stadevTObjecto > 5):
                print("Tente de novo, fique parado a 3 cm")
            else:
                print("Media da temperatura = {} °C".format(meanTObjecto /
                                                            100))
                print("Incerteza = {}".format(stadevTObjecto / 100))
                if (meanTObjecto > 3750):
                    print("FEBRE! Procurar o sistema de saúde")

                # Enviar dados via IoT - Thingspeak
                ts.uploadData(meanTObjecto / 100.0, stadevTObjecto,
                              meanTAmbient / 100.0, stadevTAmbient, numMedida)
Exemplo n.º 53
0
    1.7831648335686623E-7, 1.1702631416449307, 2.4139623633345764E-10,
    8.746781077206833E-12, 4.191207381154527E-10, 0.004335392309483765,
    7.762821496726247E-10, 1.1866063687193673E-12, 3.2673172025710073E-6
]
TempoDois_pontos = [
    193.0, 51.0, 39.0, 45.0, 34.0, 37.0, 37.0, 42.0, 41.0, 33.0, 34.0, 33.0,
    42.0, 35.0, 34.0, 30.0, 32.0, 33.0, 31.0, 30.0, 31.0, 30.0, 29.0, 31.0,
    30.0, 41.0, 30.0, 31.0, 31.0, 30.0
]

mediaBlend = 0.0
mediaDoisPontos = 0.0
mediaTempoBlend = 0.0
mediaTempoDois_pontos = 0.0

for x in range(0, len(Blend)):
    mediaTempoBlend += TempoBlend[x]
    mediaTempoDois_pontos += TempoDois_pontos[x]

mediaBlend = mediaBlend / len(Blend)
mediaTempoBlend = mediaTempoBlend / len(TempoBlend)
mediaDoisPontos = mediaTempoDois_pontos / len(TempoDois_pontos)

print('Blend:')
print('Desvio padrao: ' + str(statistics.pstdev(Blend)))
print('Media do tempo: ' + str(mediaTempoBlend))

print('\nDois_pontos:')
print('Desvio padrao: ' + str(statistics.pstdev(Dois_pontos)))
print('Media do tempo:' + str(mediaTempoDois_pontos))
Exemplo n.º 54
0
 def pstdev(self):
     '返回DataStruct.price的总体标准差 Population standard deviation'
     return self.price.groupby(
         level=1).apply(lambda x: statistics.pstdev(x))
Exemplo n.º 55
0
def compute(test_list):
    average = round(sum(test_list) / len(test_list), 2)
    res = round(statistics.pstdev(test_list), 2)
    # print(str(average)+'$'+"\\"+'pm$'+str(res))
    return str(average) + '$\\pm$' + str(res)
from numpoisson.numpoisson import NumPoissonGeometry

print('Start')
npg = NumPoissonGeometry(3, 'x')
P_so3 = {(1, 2): 'x3', (1, 3): '-x2', (2, 3): 'x1'}

num_bivector_res = dict()
j = 2
for mesh_path in [
        '3Qmesh_10_2.npy', '3Qmesh_10_3.npy', '3Qmesh_10_4.npy',
        '3Qmesh_10_5.npy', '3Qmesh_10_6.npy', '3Qmesh_10_7.npy'
]:
    print(f'step {j}')
    tiempos = dict()
    with open(mesh_path, 'rb') as f:
        mesh = np.load(f)
    for k in range(25):
        A = datetime.datetime.now()
        npg.num_bivector(P_so3, mesh, pt_output=True)
        B = datetime.datetime.now()
        tiempos[k] = (B - A).total_seconds()
    promedio = stat.mean(tiempos.values())
    desviacion = stat.pstdev(tiempos.values())
    tiempos['promedios'] = promedio
    tiempos['desviacion'] = desviacion
    num_bivector_res[f'10**{j}'] = tiempos
    j = j + 1

print(num_bivector_res)
print('Finish')
Exemplo n.º 57
0
    def run(self):
        params = NEAT.Parameters()
        params.PopulationSize = self.args.population_size
        params.AllowClones = self.args.allow_clones
        params.MutateAddNeuronProb = self.args.add_neuron_probability
        params.MutateAddLinkProb = self.args.add_link_probability
        params.MutateRemLinkProb = self.args.remove_link_probability
        params.MutateRemSimpleNeuronProb = self.args.remove_simple_neuron_probability
        params.TimeConstantMutationMaxPower = 1.0
        params.MutateNeuronTimeConstantsProb = 0.2
        params.MutateNeuronBiasesProb = 0.03
        params.MinNeuronTimeConstant = 1.0
        params.MaxNeuronTimeConstant = 2.0
        params.MinNeuronBias = -1.0
        params.MaxNeuronBias = 1.0
        params.Elitism = 0.1  # fraction of population

        num_inputs = BeerTrackerGenotype.num_input_nodes + 1  # always add one extra input, see http://multineat.com/docs.html
        num_hidden_nodes = 2
        num_outputs = 2
        genome = NEAT.Genome(
            0,  # ID
            num_inputs,
            num_hidden_nodes,
            num_outputs,
            self.args.fs_neat,
            NEAT.ActivationFunction.TANH,  # OutputActType
            NEAT.ActivationFunction.UNSIGNED_SIGMOID,  # HiddenActType
            0,  # SeedType
            params  # Parameters
        )
        pop = NEAT.Population(
            genome,
            params,
            True,  # whether the population should be randomized
            2.0,  # how much the population should be randomized,
            self.args.seed
        )

        for generation in range(1, self.args.num_generations + 1):
            print '--------------------------'
            generation_start_time = time.time()
            print('generation {}'.format(generation))
            # retrieve a list of all genomes in the population
            genotypes = NEAT.GetGenomeList(pop)

            genotype_fitness_values = []

            for genotype in genotypes:
                fitness = self.evaluate(genotype, generation)
                genotype_fitness_values.append(
                    (fitness, genotype)
                )
                genotype.SetFitness(fitness)

            genotype_fitness_values.sort()

            flat_fitness_list = [x[0] for x in genotype_fitness_values]
            max_fitness = flat_fitness_list[-1]
            min_fitness = flat_fitness_list[0]
            avg_fitness = statistics.mean(flat_fitness_list)
            fitness_std_dev = statistics.pstdev(flat_fitness_list)
            stats_item = {
                'generation': generation,
                'min_fitness': min_fitness,
                'max_fitness': max_fitness,
                'avg_fitness': avg_fitness,
                'fitness_std_dev': fitness_std_dev,
            }
            self.log.append(stats_item)
            pprint.pprint(stats_item)

            if self.args.visualize and generation % self.args.visualize_every == 0:
                net = NEAT.NeuralNetwork()
                genotype_fitness_values[-1][1].BuildPhenotype(net)  # build phenotype from best genotype
                img = np.zeros((500, 500, 3), dtype=np.uint8)
                NEAT.DrawPhenotype(img, (0, 0, 500, 500), net)
                cv2.imshow("NN", img)
                cv2.waitKey(1)

                nn = neat_net_wrapper.NeatNetWrapper(net)

                seed = generation
                bt = BeerTracker(
                    nn=nn,
                    seed=seed
                )
                bt.gfx = self.beer_tracker_gfx
                bt.run()
                print bt.world.agent.num_small_misses, 'small miss(es)'
                print bt.world.agent.num_large_misses, 'large miss(es)'
                print bt.world.agent.num_partial_captures, 'partial capture(s)'
                print bt.world.agent.num_small_captures, 'small capture(s)'
                print bt.world.agent.num_large_captures, 'large capture(s)'

                net.Save('best_neat_net.txt')

            # advance to the next generation
            pop.Epoch()
            print("Generation execution time: %s seconds" % (time.time() - generation_start_time))
Exemplo n.º 58
0
    random_list_position_points = [[0 for i in range(6)] for i in range(4)]
    for i in range(4):
        for j in range(6):
            random_list_position_points[i][j] = position_points[j][i]

    random_list_how_stops = [[0 for i in range(6)] for i in range(15)]
    for i in range(15):
        for j in range(6):
            random_list_how_stops[i][j] = how_stops[j][i]

    for i in range(14):
        average = 0
        for j in range(5):
            mean = statistics.mean(random_list_position_values[j])
            pstdev = statistics.pstdev(random_list_position_values[j])
            position_values[i + 6][j] = random.uniform(mean - 2 * pstdev,
                                                       mean + 2 * pstdev)
            average += (position_values[i + 6][j]**2)
        average = math.sqrt(average)
        for j in range(5):
            position_values[i + 6][j] = round(
                position_values[i + 6][j] / average, 4)

    for i in range(14):
        average = 0
        for j in range(4):
            mean = statistics.mean(random_list_position_points[j])
            pstdev = statistics.pstdev(random_list_position_points[j])
            position_points[i + 6][j] = random.uniform(mean - 2 * pstdev,
                                                       mean + 2 * pstdev)
Exemplo n.º 59
0
print(x)

x = statistics.median_high(example_list)
print(x)

x = statistics.mode([1, 1, 2, 3, 4, 3, 3, 3, 3])
print(x)

x = statistics.mode([
    "a",
    "b",
    "c",
    "d",
    "d",
    "a",
    "a",
])
print(x)

x = statistics.pstdev([2, 2, 2, 6])
print(x)

x = statistics.pvariance([2, 2, 2, 6])
print(x)

x = statistics.stdev([2, 2, 2, 6])
print(x)

x = statistics.variance([2, 2, 2, 6])
print(x)
Exemplo n.º 60
0
if nb_of_elements <= 0:
    print('Input should be strictly positive, giving up.')
    sys.exit()

seed(arg_for_seed)
L = [randint(-50, 50) for _ in range(nb_of_elements)]

print('\nThe list is:', L)

mean_l = sum(L) / len(L)

s = 0
for e in L:
    s += ((e - mean_l)**2)

stddev_l = (s / len(L))**0.5

L.sort()
if not len(L) % 2:
    median_l = (L[len(L) // 2] + L[len(L) // 2 - 1]) / 2
else:
    median_l = L[len(L) // 2]

print('\nThe mean is {0:.2f}.'.format(mean_l))
print('The median is {0:.2f}.'.format(median_l))
print('The standard deviation is {0:.2f}.'.format(stddev_l))
print('\nConfirming with functions from the statistics module:')
print('The mean is {0:.2f}.'.format(mean(L)))
print('The median is {0:.2f}.'.format(median(L)))
print('The standard deviation is {0:.2f}.'.format(pstdev(L)))