I am new to programming in general, however I am trying really hard for a project to randomy choose some outcomes depending on the proability of that outcome happening for lotteries that i have generated. However in my last step i still have an error that I cant seem to fix. It would be a great help if you could tell me what I did wrong or missed. Thank you very much in advance!!!
First I created the lotteries
import numpy as np
# calculate probability weighting function
p = np.arange(0.01, 1, 0.001, dtype = float) # probalities equaly spread between 0 & 1 in steps of 0.01
alpha = 0.5
def w(alpha, p):
return np.exp(-(-np.log(p))**alpha) # probability weighting function
w = w(alpha, p)
# calculate inverse of probability weighting function: p = exp(-log^2(w))
def P(w):
return np.exp(np.log2(w))
probability_of_winning = P(w)
probability_of_winning = np.round([probability_of_winning], decimals=2) # rounds to 2 decimal points
probability_of_winning = probability_of_winning.squeeze() # reduce dementions
probability_of_losing = 1 - probability_of_winning
probability_of_losing = probability_of_losing.squeeze()
# calculate best outcome of lottery
E = 10
E = float(E)
E = np.zeros((1, len(p))) + E
b = 0
b = float(b)
b = np.zeros((1, len(p))) + b
def A(E, b, probability_of_winning):
return (E - b * (1 - probability_of_winning)) / probability_of_winning
a = A(E, b, probability_of_winning)
a = np.round([A(E, b, probability_of_winning)], decimals=2) # decimals = 2 wenn a in €, wenn a in tokens dann decimals = 0
Next I created 2 seperate matrix for the lottery probabilities and payoffs
# matrix for probability of winning/ losing the lottery in colums
array_probabilities = (probability_of_winning_in_percent, probability_of_losing_in_percent) # creates tuple capturing vectors for matrix colums
matrix_of_probabilities = np.vstack(array_probabilities).T.squeeze() # np.vstack().T creates the matrix and .squeeze solves isue with to many dimensions
# matrix for outcoms of winning/ losing the lottery in colums
a=a.squeeze() # reduce size of dimensions of a
array_outcomes = (a, b)
matrix_of_outcomes = np.vstack(array_outcomes).T
# seperate the matrix into rows to give outcomes and probabilities per lottery
lottery_outcome_pairs = np.vsplit(matrix_of_outcomes, len(p))
lottery_probability_pairs = np.vsplit(matrix_of_probabilities, len(p))
# transform the seperate matrix from list to array for computation
lottery_outcome_pairs = np.array(lottery_outcome_pairs).astype(np.float)
lottery_probability_pairs = np.array(lottery_probability_pairs).astype(np.float)
# reduce size of array dimensions
lottery_outcome_pairs = lottery_outcome_pairs.squeeze()
lottery_probability_pairs = lottery_probability_pairs.squeeze()
Then I needed to sum up the probabilities to 1, i struggeled with this but now it works
# normalized the probabilities and sums them up to 1
nominalized_probability_pairs = [lottery_outcome_pairs / np.sum(lottery_outcome_pairs) for
lottery_outcome_pairs in np.vsplit(lottery_probability_pairs, len(p)) ]
number_of_displayed_realizations = 5
But I get an error in the next line of code. my error is: TypeError: cannot perform reduce with flexible type
realisations = np.concatenate([np.random.choice(lottery_outcome_pairs[i].ravel(), size=number_of_displayed_realizations , p=nominalized_probability_pairs[i].ravel()) for i in range(len(lottery_outcome_pairs))])
for this error I tried using floats but it doesn't work. how can I get rid of this error?
thank you so much for your help in advance
Aucun commentaire:
Enregistrer un commentaire