def testprobabilitieslogit(self): prob = zeros((4,3)) obs_utility = self.data.calculate_equation(self.coefficients[0]) [shape_param] = [1,]*genlogistic.numargs prob[:,0] = genlogistic.cdf(self.thresholds[0] - obs_utility, shape_param) prob[:,1] = (genlogistic.cdf(self.thresholds[1] - obs_utility, shape_param) - genlogistic.cdf(self.thresholds[0] - obs_utility, shape_param)) prob[:,2] = 1 - genlogistic.cdf(self.thresholds[1] - obs_utility, shape_param) prob_model = self.model.calc_probabilities(self.data) prob_diff = all(prob == prob_model) self.assertEqual(True, prob_diff)
def testprobabilitieslogit(self): prob = zeros((4, 3)) obs_utility = self.data.calculate_equation(self.coefficients[0]) [shape_param] = [1, ] * genlogistic.numargs prob[:, 0] = genlogistic.cdf( self.thresholds[0] - obs_utility, shape_param) prob[:, 1] = (genlogistic.cdf(self.thresholds[1] - obs_utility, shape_param) - genlogistic.cdf(self.thresholds[0] - obs_utility, shape_param)) prob[:, 2] = 1 - genlogistic.cdf(self.thresholds[1] - obs_utility, shape_param) prob_model = self.model.calc_probabilities(self.data) prob_diff = all(prob == prob_model) self.assertEqual(True, prob_diff)
def calc_probabilities(self, data): """ The method returns the selection probability associated with the the different choices. Inputs: data - DataArray object """ [shape_param] = [1,]*genlogistic.numargs observed_utility = self.calc_observed_utilities(data) num_choices = self.specification.number_choices probabilities = zeros((data.rows, num_choices)) lower_bin = 0 for i in range(num_choices-1): value = self.thresholds[i] - observed_utility if self.distribution == 'logit': upper_bin = genlogistic.cdf(value, shape_param) else: upper_bin = norm.cdf(value) probabilities[:,i] = upper_bin - lower_bin lower_bin = upper_bin probabilities[:,i+1] = 1 - upper_bin return probabilities
def calc_probabilities(self, data): """ The method returns the selection probability associated with the the different choices. Inputs: data - DataArray object """ [shape_param] = [ 1, ] * genlogistic.numargs observed_utility = self.calc_observed_utilities(data) num_choices = self.specification.number_choices probabilities = zeros((data.rows, num_choices)) lower_bin = 0 for i in range(num_choices - 1): value = self.thresholds[i] - observed_utility if self.distribution == 'logit': upper_bin = genlogistic.cdf(value, shape_param) else: upper_bin = norm.cdf(value) probabilities[:, i] = upper_bin - lower_bin lower_bin = upper_bin probabilities[:, i + 1] = 1 - upper_bin return probabilities
def transform_length(self, length): p_l = 0.3 scale_l = genlogistic.ppf(0.99, p_l) - genlogistic.ppf(0.01, p_l) loc_l = genlogistic.ppf(0.01, p_l) length_score = 1 - genlogistic.cdf( (1 - length / 15) * scale_l, p_l, -loc_l) return length_score
def calc_probabilities(self, data): """ The method returns the selection probability associated with the the different choices. Inputs: data - DataArray object """ [shape_param] = [1, ] * genlogistic.numargs observed_utility = self.calc_observed_utilities(data) num_choices = self.specification.number_choices probabilities = df(columns=self.choices, index=data.index) lower_bin = 0 for i in range(num_choices - 1): value = self.thresholds[i] - observed_utility if self.distribution == 'logit': upper_bin = genlogistic.cdf(value, shape_param) else: upper_bin = norm.cdf(value) choice = self.choices[i] probabilities.loc[:, choice] = upper_bin - lower_bin lower_bin = upper_bin choice = self.choices[i+1] #Last ordered choice probabilities.loc[:, choice] = 1 - upper_bin return probabilities
def logistic(x, mu, sig, amp, skew): from scipy.stats import genlogistic return amp * genlogistic.cdf(x, skew, mu, sig)
def transform_coverage(coverage): p_c = 0.3 scale_c = genlogistic.ppf(0.99,p_c)-genlogistic.ppf(0.01,p_c) loc_c = genlogistic.ppf(0.01,p_c) coverage_score = genlogistic.cdf(coverage*scale_c,p_c,-loc_c) return coverage_score
genlogistic.pdf(x, c), 'r-', lw=5, alpha=0.6, label='genlogistic pdf') # Alternatively, the distribution object can be called (as a function) # to fix the shape, location and scale parameters. This returns a "frozen" # RV object holding the given parameters fixed. # Freeze the distribution and display the frozen ``pdf``: rv = genlogistic(c) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = genlogistic.ppf([0.001, 0.5, 0.999], c) np.allclose([0.001, 0.5, 0.999], genlogistic.cdf(vals, c)) # True # Generate random numbers: r = genlogistic.rvs(c, size=1000) # And compare the histogram: ax.hist(r, density=True, histtype='stepfilled', alpha=0.2) ax.legend(loc='best', frameon=False) plt.show()
if (j % 200 == 0): print(j*100.0/points,"% done") generate_series(N, r1, r10, alpha, beta, delta, gamma) q1r10[j] = np.quantile(r10, 0.01) #remember sample samples.append(q1r10) #Fit the sample to generalized logistic distribution, #with shape parameter lamb, as well as location and shape parameters [lamb, loc, sc] = genlogistic.fit(q1r10) #compute the location of the mode: maximum of probability density function glmax = loc + sc * np.log(lamb) print("Gen.logistic fit parameters:", lamb, loc, sc, " with max=",glmax) #Calculate chi-square test statistic: k = 33 #number of bins in histogram #use numpy histogram for the expected value observed, hist_binedges = np.histogram(q1r10, bins=k) #use the cumulative density function (c.d.f.) #of the distribution for the expected value cdf = genlogistic.cdf(hist_binedges, lamb, loc, sc) expected = len(q1r10) * np.diff(cdf) #use scipy.stats chisquare function, where #ddof is the adjustment to the k-1 degrees of freedom, #which is the number of distribution parameters chisq, pval = st.chisquare(observed, expected, ddof=3) print("Chi-square = ", chisq) #remember fit results fitresult.append([[lamb, loc, sc], glmax, chisq])