I have this function called error, which estimates value based on previous value and rewards in trials. The difference between the estimated value and true value is then calculated, giving the error. I need to run this code 1000 times with different lists of trials, but currently it runs too slowly. How would I optimize this code?
Additional information: alpha is a parameter. Reward and true_value are lists. Value always starts at 0.5, but I remove this starting number before calculating error
def loopfunction(alpha, reward, true_value): difference =  value = [0.5] for index, lr in enumerate(reward): value.append(lever_update(alpha, value[-1], reward[index])) value.pop(0) zip_object = zip(value, true_value) for value_i, true_value_i in zip_object: difference.append(value_i-true_value) absdiff = [abs(number) for number in difference] return absdiff
The lever_update function, which calculates value is here:
def lever_update(alpha, value, reward): value += alpha * (reward - value) return(value)