Exemplo n.º 1
0
def compute_likeness(A, B, masks):
    """ Predicts the likelihood that each pixel in B belongs to a phase based
    on the histogram of A.

    Parameters
    ------------
    A : ndarray
    B : ndarray
    masks : list of ndarrays

    Returns
    --------------
    likelihoods : list of ndarrays
    """
    # generate the pdf or pmf for each of the phases
    pdfs = []
    for m in masks:
        K, mu, std = exponnorm.fit(np.ravel(A[m > 0]))
        print((K, mu, std))
        # for each reconstruciton, plot the likelihood that this phase
        # generates that pixel
        pdfs.append(exponnorm.pdf(B, K, mu, std))

    # determine the probability that it belongs to its correct phase
    pdfs_total = sum(pdfs)
    return pdfs / pdfs_total
Exemplo n.º 2
0
def standardize(df, method='z-score'):
    n=0
    
    df['standardized_RT'] = np.nan
    print('megvan az ures oszlop')
    subjects = df.Subject.unique()

    if method == 'z-score':

        for subject in subjects:
            n+=1

            x = np.asarray(df[df.Subject == subject].firstRT)
            
            # we check whether we have all datapoints
            if np.isnan(x)[np.isnan(x) == True].size > 0:
                # if we miss some, we leave them out, so that fitting does not crash
                print('WARNING! This subject had missing datapoints.')
                x = [n for n in x  if str(n) != 'nan']

            # get parameters of the subject
            m=np.mean(x)
            stdev=np.std(x)

            # compute standardized scores
            for index, row in df[df.Subject == subject].iterrows():
                raw_RT = row['firstRT']
                standardized_value = (raw_RT - m) / stdev
                df.loc[index, 'standardized_RT'] = standardized_value

            print "we are ready with " + str(n) + " subjects"        

    elif method == 'exgauss':

        for subject in subjects:
            n+=1

            x = np.asarray(df[df.Subject == subject].firstRT)
            
            # we check whether we have all datapoints
            if np.isnan(x)[np.isnan(x) == True].size > 0:
                # if we miss some, we leave them out, so that fitting does not crash
                print('WARNING! This subject had missing datapoints.')
                x = [n for n in x  if str(n) != 'nan']

            # we get the parameters of the subject
            shape_,loc_,scale_=exponnorm.fit(x)
            lambda_=(shape_*scale_)**(-1)
            tau_=1/lambda_

            # compute standardized scores
            for index, row in df[df.Subject == subject].iterrows():
                raw_RT = row['firstRT']
                standardized_value = (raw_RT - loc_) / scale_
                df.loc[index, 'standardized_RT'] = standardized_value

            print "we are ready with " + str(n) + " subjects"        

    return df
Exemplo n.º 3
0
def make_params_df(RTs,all_high_low):

    params_df=pd.DataFrame()

    subjects=[]
    age_groups=[]
    locs=[]
    scales=[]
    ts=[]
    ps=[]
    for group,values in RTs.iteritems():

        for subject in values:

            x = np.asarray(RTs[group][subject])
            print 'Reaction time data of the participant - for fitting'                
            print x

            # we check whether we have all datapoints
            if np.isnan(x)[np.isnan(x) == True].size > 0:
                # if we miss some, we leave them out, so that fitting does not crash
                print('WARNING! This subject had missing datapoints.')
                x = [n for n in x  if str(n) != 'nan']

            # ExGaussian parameters for subject
            shape_,loc_,scale_=exponnorm.fit(x)
            lambda_=(shape_*scale_)**(-1)
            tau_=1/lambda_

            # test goodness of fit with Kolgomorov-Smirnov
            d,p = stats.kstest(x, 'exponnorm', args=(shape_,loc_,scale_))

            subjects.append(subject)
            age_groups.append(group)
            locs.append(loc_)
            scales.append(scale_)
            ts.append(tau_)
            ps.append(p)

    # add the results to the df

    params_df['Subject']            = np.asarray(subjects)
    params_df['age_group']            = np.asarray(age_groups)    
    params_df['mu_'+all_high_low]        = np.asarray(locs)
    params_df['sigma_'+all_high_low]    = np.asarray(scales)
    params_df['tau_'+all_high_low]        = np.asarray(ts)
    params_df['goodness_'+all_high_low]    = np.asarray(ps)

    return params_df
Exemplo n.º 4
0
def main():
	symbol = 'BTCUSDT'
	start = int(datetime.datetime.timestamp(datetime.datetime(2019, 6, 1))) * 1000 - 365 * 24 * 60 * 60 * 1000
	previous = start
	trades = []
	total_trades = 0
	max_trades = 0
	time_step = 1
	with open('../Binance/' + symbol + '.csv', 'r') as csvfile:
		reader = csv.DictReader(csvfile)
		for line in reader:
			if int(line['Timestamp']) > previous + time_step * 60 * 60 * 1000:
				previous += time_step * 60 * 60 * 1000
				trades.append(total_trades)
				if max_trades < total_trades:
					max_trades = total_trades
				total_trades = 0
			total_trades += 1

	mean = np.mean(trades)
	print(mean)

	trades = [t for t in trades if t != 1]

	#mean, scale = norm.fit(np.log(trades))
	a, loc, scale = skewnorm.fit(np.log(trades))
	loc_n, scale_n = norm.fit(np.log(trades))

	k, loc_ne, scale_ne = exponnorm.fit(np.log(trades))

	plt.hist(np.log(trades), bins=100, density=True)
	x = np.linspace(6, 12, 100)
	plt.plot(x, skewnorm.pdf(x, a, loc=loc, scale=scale), label='skewnorm')
	plt.plot(x, norm.pdf(x, loc=loc_n, scale=scale_n), label='norm')
	plt.plot(x, exponnorm.pdf(x, k, loc=loc_n, scale=scale_n), label='Exponentially modified Gaussian')

	plt.xlabel('Log Trades')
	plt.ylabel('Density')

	plt.legend()

	#plt.plot([i for i in range(1, max_trades)], poisson_density(np.array([i for i in range(1, max_trades)]), mean))
	#plt.plot([i for i in range(0, max_trades)], norm.pdf([i for i in range(max_trades)], loc=mean, scale=np.sqrt(mean)))
	plt.savefig(symbol + ' log Trades')

	plt.show()