Example #1
0
def get_p(i):

    data, sim_scale = simulate_data(K, J, L, C, beta=beta)

    data['Subj'] = pd.factorize(data['Subj'])[0] + 1
    myThurst = thurstonian(design_formula='~0+Condition',
                           data=data,
                           subject_name="Subj")

    return myThurst.sra_p_value(level_dict={'Condition': 'C2'})[0]
Example #2
0
import numpy as np
import pandas as pd

from multiprocessing import Process

if __name__ == '__main__':

    #Load and preprocess data
    K = 3  #Number of items
    J = 10  #Number of participants
    L = 1  #Number of trials
    C = 2  #Number of conditions

    beta = np.array([[0.0, 1.0, 2.0], [0.0, 0.1, 0.2]])

    data, sim_scale = simulate_data(K, J, L, C, beta=beta, seed=4354356)

    data['Subj'] = pd.factorize(data['Subj'])[0] + 1
    myThurst = thurstonian(design_formula='~0+Condition',
                           data=data,
                           subject_name="Subj")

    myThurst.pre_sample()

    P = [Process(target=run_sample, kwargs={'temp_fnum': i}) for i in range(4)]

    for p in P:
        p.start()

    for p in P:
        p.join()
Example #3
0
@signal_base.function
def tm_prior(weights):
    return weights * 10**40


# base par and tim file
parfile = 'J1713+0747.par'
timfile = 'J1713+0747.tim'

thetas = [0.05, 0.1, 0.15]
for theta in thetas:

    # simulate data
    idx = random.getrandbits(32)
    sigma_out = 1e-6
    simulate_data(parfile, timfile, theta=theta, idx=idx, sigma_out=sigma_out)

    # grab simulated data
    simparfile = 'simulated_data/outlier/{}/{}/J1713+0747.par'.format(
        theta, idx)
    simtimfile = 'simulated_data/outlier/{}/{}/J1713+0747.tim'.format(
        theta, idx)
    psr = Pulsar(simparfile, simtimfile)

    simparfile = 'simulated_data/no_outlier/{}/{}/J1713+0747.par'.format(
        theta, idx)
    simtimfile = 'simulated_data/no_outlier/{}/{}/J1713+0747.tim'.format(
        theta, idx)
    psr2 = Pulsar(simparfile, simtimfile)

    psrs = [psr, psr2]
Example #4
0
    #-------------------------------------#
    #-------------------------------------#
    #######################################

    N = 5000

    #######################################
    #-------------------------------------#
    #----------Generate test data---------#
    #-------------------------------------#
    #-------------------------------------#
    #-------------------------------------#
    #######################################

    beta = np.array([[0.0, 1.0, 2.0], [0.0, -0.1, -0.2]])
    data = simulate_data(K, J, L, C, beta=beta, seed=8547346)
    data['Subj'] = pd.factorize(data['Subj'])[0] + 1
    y = data[['Y1', 'Y2', 'Y3']].values
    X = np.asarray(patsy.dmatrix('~Condition', data=data))
    subj = data['Subj'].values
    stan_data = dict(N=y.shape[0],
                     K=y.shape[1],
                     C=X.shape[1],
                     J=J,
                     rater=subj,
                     X=X,
                     beta_sd_prior=K * 3,
                     scale_sd_prior=0.5)

    #Generate all data simulations
    fit = gen.sampling(data=stan_data,
Example #5
0
    return np.sum(A(x)) / x.shape[0]


#Load and preprocess data
K = 5  #Number of items
J = 30  #Number of participants
L = 1  #Number of trials
C = 1  #Number of conditions

beta = np.array([[0, 0.5, 1]])

all_sra = []
rand_sra = []
for i in range(1000):
    data = simulate_data(K, J, L, C, beta=beta)

    y = data[['Y1', 'Y2', 'Y3']]
    y.to_csv("y_test.csv", index=False)

    y = y.values

    y_unif = np.array([np.random.permutation(range(3)) + 1 for i in range(J)])

    all_sra.append(np.sqrt(sra(y)))
    rand_sra.append(np.sqrt(sra(y_unif)))

plt.hist(np.sqrt(rand_sra), color='r', alpha=0.25, normed=True)
plt.hist(np.sqrt(all_sra), color='b', alpha=0.25, normed=True)

#s_l = S(y, 1, 0)
    #Set plotting preferences
    sns.set_context("paper")    
    sns.set_style("whitegrid")
    
    output_folder = "article/Figures/"
    
    #Load and preprocess data  
    K = 3 #Number of items
    J = 5 #Number of participants
    L = 1 #Number of trials
    C = 2 #Number of conditions
    
    beta = np.array([[0.0, 1.0, 2.0],
                     [0.0, 0.0, 0.0]])        
    
    data, sim_scale = simulate_data(K, J, L, C, beta = beta)

    data['Subj'] = pd.factorize(data['Subj'])[0]+1
    myThurst = thurstonian(design_formula = '~0+Condition', data = data, subject_name = "Subj")   
        
    
    #Note: When running the sampler use a cmdline console, not iPython from Spyder. Unfortunately this fails due to some quirks in Spyder
    resample = True
    if resample:   
                       
        #Prepare for sampling using multiple cores
        myThurst.pre_sample()        
        
        #Sample with multiprocessing
        P = [Process(target=PyThurstonian.run_sample, args = (5000,), kwargs = {'temp_fnum': i})  for i in range(4)]     
           
    
    #Load and preprocess data  
    K = 15
    J = 20 #Number of participants
    L = 1 #Number of trials
    C = 2 #Number of conditions
    
    beta = [np.arange(0, 15), np.arange(0, 15)]
    beta[1] = beta[1][::-1]

    beta = np.array(beta) *0.1
    
#    beta = np.zeros((C, K))
    
    
    data = simulate_data(K, J, L, C, beta = beta, seed = 234254324)


    data['Subj'] = pd.factorize(data['Subj'])[0]+1
    myThurst = thurstonian(design_formula = '~Condition', data = data, subject_name = "Subj")        
    
    resample = True
    
    if resample:   
        myThurst.sample(iter = 5000, chains = 1) #Sample from the posterior    
        myThurst.save_samples('MCMC/complex_samples') #Save the samples for later
        
    else:    
        #Load old samples
        myThurst.load_samples('MCMC/complex_samples')