Exemple #1
0
def main():

    np.random.seed(47)
    n = 10000
    p = 10
    n_arm = 2
    divide = 1
    coef_ = np.array(
        [np.zeros(p + int((p + 1) * p / 2)) for _ in range(int(n_arm))])
    coef_[0][11] = -6.0
    coef_[0][1] = 4.0

    coef_[1][1] = 4.0
    coef_[1][0] = -6.0
    #coef_[1][11] = 4
    '''
				data
				'''
    R = 50
    size = int((n + 1) / 200)
    X_oracle = np.zeros(R * size).reshape(size, R)
    X_low = np.zeros(R * size).reshape(size, R)
    X_cross = np.zeros(R * size).reshape(size, R)
    X_random = np.zeros(R * size).reshape(size, R)

    for r_ in range(R):

        x = np.random.uniform(-1, 1, p * n).reshape(n, p)
        for j in range(n):
            x[j, :] = x[j, :] / np.sqrt(np.dot(x[j, :], x[j, :])) * 2

        e = np.random.normal(0, 0.05, n_arm * n).reshape(n, n_arm)

        for i in range(n_arm):
            coef_[i] = coef_[i] / np.sqrt(np.dot(coef_[i], coef_[i])) * 1
        #print(coef_)
        X = cross_gene_(x)
        #print(X[0:6, 0:20])

        select_cross_low = bandit_low_cross(x, e, coef_)
        select_cross = bandit_high(X, e, coef_)  #Consider the cross-terms
        select_oracle = bandit_oracle(X, coef_)
        select_random = np.random.randint(0, coef_.shape[0], n)
        '''
								print(select_cross[0][500:600])
								
								print(risk_calculator(X, e, coef_, select_oracle)[10], 'oracle')
								print(risk_calculator(X, e, coef_, select_cross[0])[10], 'cross')
								print(risk_calculator(X, e, coef_, select_cross_low[0])[10], 'cross_low')
								print(risk_calculator(X, e, coef_, select_random), 'random')
								'''

        X_oracle[:, r_] = risk_calculator(X, e, coef_, select_oracle)
        X_low[:, r_] = risk_calculator(X, e, coef_, select_cross_low[0])
        X_cross[:, r_] = risk_calculator(X, e, coef_, select_cross[0])
        X_random[:, r_] = risk_calculator(X, e, coef_, select_random)
    '''
				Save the simulating data
				'''
    '''
Exemple #2
0
def main():
    '''
				high-dimension winning case:
				'''
    np.random.seed(47)
    n = 10000
    p = 100
    n_arm = 6
    divide = 1
    #Sparse example:
    coef_high = np.array([np.zeros(p) for _ in range(int(n_arm))])
    for j in range(n_arm):
        #print(coef_high[j])
        #print(coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)])
        coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)] \
            = np.random.uniform(0,10, 3)
        coef_high[j] = coef_high[j] / np.sqrt(
            np.dot(coef_high[j], coef_high[j]))

    coef_ = coef_high
    '''
				data
				'''
    R = 50
    size = int((n + 1) / 200)
    X_oracle = np.zeros(R * size).reshape(size, R)
    X_low = np.zeros(R * size).reshape(size, R)
    X_high = np.zeros(R * size).reshape(size, R)
    X_bylearning = np.zeros(R * size).reshape(size, R)
    X_random = np.zeros(R * size).reshape(size, R)

    for r_ in range(R):

        x = np.random.uniform(-1, 1, p * n).reshape(n, p)
        for j in range(n):
            x[j, :] = x[j, :] / np.sqrt(np.dot(x[j, :], x[j, :])) * 2

        e = np.random.normal(0, 0.05, n_arm * n).reshape(n, n_arm)

        select_1 = bandit_oracle(x, coef_)
        select_2 = bandit_low(x, e, coef_)
        select_3 = bandit_high(x, e, coef_)
        select_4 = bandit_bylearning(x, e, coef_)
        select_random = np.random.randint(0, coef_.shape[0], n)
        '''
								print(risk_calculator(x, e, coef_, select_1))
								print(risk_calculator(x, e, coef_, select_2[0]), 'low')
								print(risk_calculator(x, e, coef_, select_3[0]), 'high')
								print(risk_calculator(x, e, coef_, select_4[0]), 'bylearning')
								print(risk_calculator(x, e, coef_, select_random), 'random')
								'''
        X_oracle[:, r_] = risk_calculator(x, e, coef_, select_1)
        X_low[:, r_] = risk_calculator(x, e, coef_, select_2[0])
        X_high[:, r_] = risk_calculator(x, e, coef_, select_3[0])
        X_bylearning[:, r_] = risk_calculator(x, e, coef_, select_4[0])
        X_random[:, r_] = risk_calculator(x, e, coef_, select_random)
    '''
				Save the simulating data
				'''
    '''
Exemple #3
0
def main():
				np.random.seed(47)
				n = 10000
				p = 100
				n_arm = 6
				divide = 1
				A = [np.empty((0, p), int) for _ in range(int(n_arm / divide))]
			
				for j in range(int(n_arm / divide)):
								A[j] = np.hstack([np.random.uniform(-5,10, p)])		
								A[j] = A[j] / np.sqrt(np.dot(A[j],A[j]))
				coef_ = np.array(A)
				
				
				
				#non-sparse example. High-dimension tends to perform badly
				A = [np.empty((0, p), int) for _ in range(int(n_arm / divide))]
				b_ = 0
				for j in range(int(n_arm / divide)):
								A[j] = np.zeros(p)
								if j < int(n_arm / divide) / 3:
												A[j][:int(p / 3)] = np.hstack([np.random.uniform(b_,10, int(p / 3))])
								elif j > int(n_arm / divide) / 3 and  j <= int(n_arm / divide) / 3 * 2:
												A[j][int(p / 3) + 1:int(2 * p / 3)] = np.hstack([np.random.uniform(b_,10, int(2 * p / 3) - int(p / 3) - 1)])
								else: 
												A[j][int(2 * p / 3) + 1:] = np.hstack([np.random.uniform(b_,10, p - int(2 * p / 3) - 1)])
								A[j] = A[j] / np.sqrt(np.dot(A[j],A[j]))
				coef_ = np.array(A)
				
				
				'''
				data
				'''
				R = 50
				size = int((n+1) / 200)
				X_oracle = np.zeros(R * size).reshape(size, R)
				X_low = np.zeros(R * size).reshape(size, R)
				X_high = np.zeros(R * size).reshape(size, R)
				X_bylearning = np.zeros(R * size).reshape(size, R)
				X_random = np.zeros(R * size).reshape(size, R)
				
				for r_ in range(R):
								
								
								
								
								x = np.random.uniform(-1,1,p * n).reshape(n, p)
								for j in range(n):
												x[j,:] = x[j,:] / np.sqrt(np.dot(x[j,:], x[j,:])) * 2
								
								e = np.random.normal(0,0.05,n_arm * n).reshape(n,n_arm)
								
								
								
								
								
								
								select_1 = bandit_oracle(x, coef_)
								select_2 = bandit_low(x, e, coef_)
								select_3 = bandit_high(x, e, coef_)
								select_4 = bandit_bylearning(x, e, coef_)
								select_random = np.random.randint(0, coef_.shape[0], n)
								'''
								print(risk_calculator(x, e, coef_, select_1).round(4))
								print(risk_calculator(x, e, coef_, select_2[0]).round(4), 'low')
								print(risk_calculator(x, e, coef_, select_3[0]).round(4), 'high')
								print(risk_calculator(x, e, coef_, select_4[0]).round(4), 'bylearning')
								print(risk_calculator(x, e, coef_, select_random), 'random')
								'''
								X_oracle[:, r_] = risk_calculator(x, e, coef_, select_1)
								X_low[:, r_] = risk_calculator(x, e, coef_, select_2[0])
								X_high[:, r_] = risk_calculator(x, e, coef_, select_3[0])
								X_bylearning[:, r_] = risk_calculator(x, e, coef_, select_4[0])
								X_random[:, r_] = risk_calculator(x, e, coef_, select_random)
				
				
				'''
				Save the simulating data
				'''
				'''
Exemple #4
0
def main():
				'''
				high-dimension winning case:
				n = 2000
				p = 200
				n_arm = 6
				divide = 1
				x = np.random.uniform(0,1,p * n).reshape(n, p)#np.abs(np.random.normal(0,0.2,p * n).reshape(n,p))
				
				for j in range(n):
								x[j,:] = x[j,:] / np.sqrt(np.dot(x[j,:], x[j,:])) * 2
			
				e = np.random.normal(0,0.05,n_arm * n).reshape(n,n_arm)
				
				coef_high = np.array([np.zeros(p) for _ in range(int(n_arm / divide))])
				for j in range(n_arm):
								#print(coef_high[j])
								#print(coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)])
								coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)] \
												= np.random.uniform(0,10, 3)
								coef_high[j] = coef_high[j] / np.sqrt(np.dot(coef_high[j], coef_high[j]))

				coef_ = coef_high
				'''
				n = 1000
				p = 100
				n_arm = 6
				divide = 1
				x = np.random.uniform(0,1,p * n).reshape(n, p)
				for j in range(n):
								x[j,:] = x[j,:] / np.sqrt(np.dot(x[j,:], x[j,:])) * 2
			
				e = np.random.normal(0,0.05,n_arm * n).reshape(n,n_arm)
				'''
				low, 
				n = 2000
				p = 100
				n_arm = 6
				divide = 1
				x = np.random.uniform(0,1,p * n).reshape(n, p)
				for j in range(n):
								x[j,:] = x[j,:] / np.sqrt(np.dot(x[j,:], x[j,:])) * 2
			
				e = np.random.normal(0,0.05,n_arm * n).reshape(n,n_arm)
				
				A = [np.empty((0, p), int) for _ in range(int(n_arm / divide))]
			
				for j in range(int(n_arm / divide)):
								A[j] = np.hstack([np.random.uniform(-5,10, p)])		
								A[j] = A[j] / np.sqrt(np.dot(A[j],A[j]))
				coef_ = np.array(A)
				'''
				
				
				#non-sparse example. High-dimension tends to perform badly
				A = [np.empty((0, p), int) for _ in range(int(n_arm / divide))]
				b_ = 0
				for j in range(int(n_arm / divide)):
								A[j] = np.zeros(p)
								if j < int(n_arm / divide) / 3:
												A[j][:int(p / 3)] = np.hstack([np.random.uniform(b_,10, int(p / 3))])
								elif j > int(n_arm / divide) / 3 and  j <= int(n_arm / divide) / 3 * 2:
												A[j][int(p / 3) + 1:int(2 * p / 3)] = np.hstack([np.random.uniform(b_,10, int(2 * p / 3) - int(p / 3) - 1)])
								else: 
												A[j][int(2 * p / 3) + 1:] = np.hstack([np.random.uniform(b_,10, p - int(2 * p / 3) - 1)])
								A[j] = A[j] / np.sqrt(np.dot(A[j],A[j]))
				coef_ = np.array(A)
				#coef_ = np.array(np.vstack([A, [_ / 3 for _ in A]])) * 1 #Cool!
				
				#Sparse example:
				coef_high = np.array([np.zeros(p) for _ in range(int(n_arm))])
				for j in range(n_arm):
								#print(coef_high[j])
								#print(coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)])
								coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)] \
												= np.random.uniform(0,10, 3)
								coef_high[j] = coef_high[j] / np.sqrt(np.dot(coef_high[j], coef_high[j]))

				#coef_ = coef_high

				
				
				select_1 = bandit_oracle(x, coef_)
				select_2 = bandit_low(x, e, coef_)
				select_3 = bandit_high(x, e, coef_)
				select_4 = bandit_bylearning(x, e, coef_)
				
				
				print(risk_calculator(x, e, coef_, select_1))
				print(risk_calculator(x, e, coef_, select_2[0]), 'low')
				print(risk_calculator(x, e, coef_, select_3[0]), 'high')
				print(risk_calculator(x, e, coef_, select_4[0]), 'bylearning')
				select_random = np.random.randint(0, coef_.shape[0], n)
				print(risk_calculator(x, e, coef_, select_random), 'random')
				select_random = np.random.randint(0, coef_.shape[0], n)
				print(risk_calculator(x, e, coef_, select_random), 'random')
				
				
				
				
				
				
				
				'''
				'''
				
				
				
				#Cross-terms;

				
				n = 1000
				p = 10
				n_arm = 2
				divide = 1
				x = np.random.uniform(0,1,p * n).reshape(n, p)
				for j in range(n):
								x[j,:] = x[j,:] / np.sqrt(np.dot(x[j,:], x[j,:])) * 2
			
				e = np.random.normal(0,0.05,n_arm * n).reshape(n,n_arm)
				
				
				coef_ = np.array([np.zeros(p + int((p + 1) * p / 2)) for _ in range(int(n_arm))])
				coef_[0][10] = 4.2
				coef_[0][0] = -2.0
				#coef_[1][1] = -2.0
				coef_[1][11] = 4.0
				
				for i in range(n_arm):
								coef_[i] = coef_[i] / np.sqrt(np.dot(coef_[i], coef_[i]))
				print(coef_)
				X = cross_gene_(x)
				
				select_cross_low = bandit_low_cross(x, e, coef_)
				select_cross = bandit_high(X, e, coef_)
				select_1 = bandit_oracle(X, coef_)
				select_random = np.random.randint(0, coef_.shape[0], n)
				#print(select_1)
				print(risk_calculator_cross(x, e, coef_, select_1), 'oracle')
				print(risk_calculator_cross(x, e, coef_, select_cross[0]), 'cross')
				print(risk_calculator_cross(x, e, coef_, select_cross_low[0]), 'cross_low')
				print(risk_calculator_cross(x, e, coef_, select_random), 'random')
Exemple #5
0
def main():
    ''' 
				high-dimension winning case:
				n = 1000
				p = 100
				n_arm = 3
				divide = 1
				x = np.random.uniform(0,1,p * n).reshape(n, p)#np.abs(np.random.normal(0,0.2,p * n).reshape(n,p))
				
				for j in range(n):
								x[j,:] = x[j,:] / np.sqrt(np.dot(x[j,:], x[j,:])) 
			
				e = np.random.normal(0,0.01,n_arm * n).reshape(n,n_arm)
				
				coef_high = np.array([np.zeros(p) for _ in range(int(n_arm / divide))])
				for j in range(n_arm):
								#print(coef_high[j])
								#print(coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)])
								coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)] \
												= np.random.uniform(0,10, 3)
								coef_high[j] = coef_high[j] / np.sqrt(np.dot(coef_high[j], coef_high[j]))

				coef_ = coef_high
				'''
    n = 2000
    p = 100
    n_arm = 8
    divide = 1
    #x = np.random.normal(0,1,p * n).reshape(n,p)
    x = np.random.uniform(0, 1, p * n).reshape(n, p)
    for j in range(n):
        x[j, :] = x[j, :] / np.sqrt(np.dot(x[j, :], x[j, :]))

    e = np.random.normal(0, 0.01, n_arm * n).reshape(n, n_arm)
    '''
				low, not so good an example
				n = 2000
				p = 100
				n_arm = 8
				divide = 1
				x = np.random.normal(0,1,p * n).reshape(n,p)
				#x = np.random.uniform(0,1,p * n).reshape(n, p)
				for j in range(n):
								x[j,:] = x[j,:] / np.sqrt(np.dot(x[j,:], x[j,:])) 
			
				e = np.random.normal(0,0.01,n_arm * n).reshape(n,n_arm)
				
				A = [np.empty((0, p), int) for _ in range(int(n_arm / divide))]
			
				for j in range(int(n_arm / divide)):
								A[j] = np.hstack([np.random.uniform(-5,10, p)])		
								A[j] = A[j] / np.sqrt(np.dot(A[j],A[j]))
				coef_ = np.array(A)
				'''

    #non-sparse example. High-dimension tends to perform badly
    A = [np.empty((0, p), int) for _ in range(int(n_arm / divide))]
    b_ = 0
    for j in range(int(n_arm / divide)):
        #A[j] = np.hstack([np.random.uniform(0,10, p)])
        A[j] = np.zeros(p)
        if j < int(n_arm / divide) / 3:
            #print(A[j][:int(p / 2)])
            A[j][:int(p / 3)] = np.hstack(
                [np.random.uniform(b_, 10, int(p / 3))])
        elif j > int(n_arm / divide) / 3 and j <= int(n_arm / divide) / 3 * 2:
            A[j][int(p / 3) + 1:int(2 * p / 3)] = np.hstack(
                [np.random.uniform(b_, 10,
                                   int(2 * p / 3) - int(p / 3) - 1)])
        else:
            A[j][int(2 * p / 3) + 1:] = np.hstack(
                [np.random.uniform(b_, 10, p - int(2 * p / 3) - 1)])
        A[j] = A[j] / np.sqrt(np.dot(A[j], A[j]))
    coef_ = np.array(A)
    #coef_ = np.array(np.vstack([A, [_ / 3 for _ in A]])) * 1 #Cool!

    coef_high = np.array([np.zeros(p) for _ in range(int(n_arm))])
    for j in range(n_arm):
        #print(coef_high[j])
        #print(coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)])
        coef_high[j][np.random.choice(range(p), 3, replace=False).astype(int)] \
            = np.random.uniform(0,10, 3)
        coef_high[j] = coef_high[j] / np.sqrt(
            np.dot(coef_high[j], coef_high[j]))

    #coef_ = coef_high
    for elem in coef_:
        print(elem.round(4))

    select_1 = bandit_oracle(x, coef_)
    select_2 = bandit_low(x, e, coef_)
    select_3 = bandit_high(x, e, coef_)
    select_4 = bandit_bylearning(x, e, coef_)
    #select_5 = bandit_low(x, e, coef_) These algorithm possess some randomness
    #random

    print(risk_calculator(x, e, coef_, select_1))
    print(risk_calculator(x, e, coef_, select_2[0]), 'low')
    print(risk_calculator(x, e, coef_, select_3[0]), 'high')
    print(risk_calculator(x, e, coef_, select_4[0]), 'bylearning')
    select_random = np.random.randint(0, coef_.shape[0], n)
    print(risk_calculator(x, e, coef_, select_random), 'random')
    select_random = np.random.randint(0, coef_.shape[0], n)
    print(risk_calculator(x, e, coef_, select_random), 'random')
    #print(risk_calculator(x, e, coef_, select_5[0]))
    #print(select_2[1].round(4))
    #print(select_4[0])
    #print(select_2[0], 'low')
    #print(select_3[0], 'high')
    for elem in select_3[1]:
        pass  #print(elem.round(4))

    for elem in select_4[1]:
        for _ in elem:
            pass  #print(_.round(4))