I am new to python and coding in general. i want use a numpy function to randomly select a value from a vector of lottery outcomes depending on another vector that captures the probability weights, that were generated from a probability weighting function.
This is the code I used so far that works without problem
import numpy as np
# calculate probability weighting function
p = np.arange(0.01, 1, 0.001, dtype = float)
p = p.transpose()
alpha = input("alpha = ")
alpha = float(alpha)
alpha = np.zeros((1, len(p))) + alpha # vector of same length as p with all entries being 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_in_percent = probability_of_winning * 100
probability_of_losing = 1 - probability_of_winning
probability_of_losing_in_percent = probability_of_losing * 100
# calculate best outcome of lottery
E = input("endownment/ expected value of lottery = ") # endownment & expected value of lottery
E = float(E)
E = np.zeros((1, len(p))) + E
b = input("worst outcome of lottery is = ") # worst outcome of lottery
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)
I than created two matrixes to have the coresponding outcomes and probabilities for losing or winning the lottery in the colums
# matrix for probability of winning/ losing the lottery in colums
array_probabilities = (probability_of_winning_in_percent, probability_of_losing_in_percent)
matrix_of_probabilities = np.vstack(array_probabilities).T.squeeze()
# 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
then I separeted the rows to receive the outcome and probability pairs for each lottery
lottery_outcome_pairs = np.vsplit(matrix_of_outcomes, len(p))
lottery_probability_pairs = np.vsplit(matrix_of_probabilities, len(p))
then I wanted to use the numpy function np.random.choice to generate randomly outcomes form the lottery_outcome_vector given the weights of the lottery_probability_pairs with:
ralizations = np.random.choice(lottery_outcome_pairs, 5, p = lottery_probability_pairs)
however here i receive the following error:
ValueError: a must be 1-dimensional
how can I fix my code? thank you very much in advance
Aucun commentaire:
Enregistrer un commentaire