def uniform_demand_distribution(C, t, low=0, high=25): n = int(C.shape[0]) U = np.linalg.cholesky(C) raw_demand = np.random.normal(size=(t, n)) shifted_demand = np.dot(raw_demand, U.T) flat_demand = flatten_matrix(shifted_demand) true_std = np.std(flat_demand) true_mean = np.mean(flat_demand) normalized_demand = (shifted_demand - true_mean) / true_std new_demand = randint.ppf(norm.cdf(normalized_demand), low, high) return new_demand.T
def generate_graph_data(self): ageGroup = self.tableModel.data[self.selected_item_index.row()][0] parameter = self.tableModel.data[self.selected_item_index.row()][1] p1 = self.temporaryParametersDict[ageGroup][parameter]["p1"] p2 = self.temporaryParametersDict[ageGroup][parameter]["p2"] distributionType = self.temporaryParametersDict[ageGroup][parameter][ "distributionType"] xyDict = {"x": [], "y": []} try: if distributionType == 'Binomial': xyDict["x"] = np.arange(binom.ppf(0.01, int(p1), p2 / 100), binom.ppf(0.99, int(p1), p2 / 100)) xyDict["y"] = binom.pmf(xyDict["x"], int(p1), p2 / 100) elif distributionType == 'Geometric': xyDict["x"] = np.arange(geom.ppf(0.01, p1 / 100), geom.ppf(0.99, p1 / 100)) xyDict["y"] = geom.pmf(xyDict["x"], p1 / 100) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Laplacian': xyDict["x"] = np.arange(dlaplace.ppf(0.01, p1 / 100), dlaplace.ppf(0.99, p1 / 100)) xyDict["y"] = dlaplace.pmf(xyDict["x"], p1 / 100) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Logarithmic': xyDict["x"] = np.arange(logser.ppf(0.01, p1 / 100), logser.ppf(0.99, p1 / 100)) xyDict["y"] = logser.pmf(xyDict["x"], p1 / 100) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Neg. binomial': xyDict["x"] = np.arange(nbinom.ppf(0.01, p1, p2 / 100), nbinom.ppf(0.99, p1, p2 / 100)) xyDict["y"] = nbinom.pmf(xyDict["x"], p1, p2 / 100) elif distributionType == 'Planck': xyDict["x"] = np.arange(planck.ppf(0.01, p1 / 100), planck.ppf(0.99, p1 / 100)) xyDict["y"] = planck.pmf(xyDict["x"], p1 / 100) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Poisson': xyDict["x"] = np.arange(poisson.ppf(0.01, p1), poisson.ppf(0.99, p1)) xyDict["y"] = poisson.pmf(xyDict["x"], p1) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) elif distributionType == 'Uniform': if p1 - 0.5 * p2 < 0: p2 = p1 min = p1 - 0.5 * p2 max = p1 + 0.5 * p2 xyDict["x"] = np.arange(randint.ppf(0.01, min, max), randint.ppf(0.99, min, max)) xyDict["y"] = randint.pmf(xyDict["x"], min, max) elif distributionType == 'Zipf (Zeta)': xyDict["x"] = np.arange(zipf.ppf(0.01, p1), zipf.ppf(0.99, p1)) xyDict["y"] = zipf.pmf(xyDict["x"], p1) if p2 != 0: self.tableModel.setData( self.selected_item_index.sibling( self.selected_item_index.row(), 3), 0, Qt.EditRole) self.update_graph(xyDict) except Exception as E: log.error(E)
ax.plot([], [], "g", label=str_esp_var_emp) # Histogramme ax.hist(r, normed=True, histtype='stepfilled', alpha=0.2) ax.legend(loc='best', frameon=False) # ============================================= # # ============= UNIFORME DISCRETE ============= # # ============================================= # fig, ax = plt.subplots(1, 1) low, high = 7, 31 mean, var, skew, kurt = randint.stats(low, high, moments='mvsk') x = np.arange(randint.ppf(0.01, low, high), randint.ppf(0.99, low, high)) ax.plot(x, randint.pmf(x, low, high), 'bo', ms=8, label='randint pmf') ax.vlines(x, 0, randint.pmf(x, low, high), colors='b', lw=5, alpha=0.5) rv = randint(low, high) ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf') ax.legend(loc='best', frameon=False) # ============================================= # # ================== NORMALE ================== # # ============================================= # fig, ax = plt.subplots(1, 1) mean, var, skew, kurt = norm.stats(moments='mvsk')
from scipy.stats import randint import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: low, high = 7, 31 mean, var, skew, kurt = randint.stats(low, high, moments='mvsk') # Display the probability mass function (``pmf``): x = np.arange(randint.ppf(0.01, low, high), randint.ppf(0.99, low, high)) ax.plot(x, randint.pmf(x, low, high), 'bo', ms=8, label='randint pmf') ax.vlines(x, 0, randint.pmf(x, low, high), colors='b', lw=5, alpha=0.5) # Alternatively, the distribution object can be called (as a function) # to fix the shape and location. This returns a "frozen" RV object holding # the given parameters fixed. # Freeze the distribution and display the frozen ``pmf``: rv = randint(low, high) ax.vlines(x, 0, rv.pmf(x), colors='k', linestyles='-', lw=1, label='frozen pmf') ax.legend(loc='best', frameon=False) plt.show()
def ppf(self, dist, p): return randint.ppf(p, *self._get_params(dist))
fontsize=16) plt.xticks(np.arange(-1, 20, 1)) plt.yticks(np.arange(0, 1.1, 0.1)) plt.legend(loc='upper left', shadow=True) plt.show() # ### Uniform (Discrete) Distribution # In[6]: #Uniform (Discrete) Distribution from scipy.stats import randint low, high = 1, 10 x = np.arange(randint.ppf(0.01, low, high), randint.ppf( 0.99, low, high)) #Percent Point Function (inverse of cdf — percentiles) print("Mean : ", randint.stats(low, high, moments='m')) print("Variance : ", randint.stats(low, high, moments='v')) print("Prob. Mass Func. : ", randint.pmf(x, low, high)) print("Cum. Density Func.: ", randint.cdf(x, low, high)) CDF = randint.cdf(x, low, high) fig = plt.figure(figsize=(20, 10)) plt.subplot(221) plt.plot(x, randint.pmf(x, low, high), 'go', ms=8, label='PMF') plt.vlines(x, 0, randint.pmf(x, low, high), colors='g', lw=5, alpha=0.5) plt.xlabel("Sample Space of Discrete Uniform Distribution", fontsize=14) plt.ylabel("PMF", fontsize=14)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ lois de distribution uniforme discrète """ __author__ = "Dominique Lefebvre" __copyright__ = "Copyright 2019 - TangenteX.com" __version__ = "1.0" __date__ = "20 septembre 2019" __email__ = "*****@*****.**" from scipy.stats import randint from scipy import arange from matplotlib.pylab import figure, show, plot, grid # loi uniform - variable aléatoire discrète a = 1 b = 1 x = arange(randint.ppf(1.0,a,b) fig1 = figure(figsize=(14,8)) ax = fig1.add_subplot(111,autoscale_on=False, xlim=(0,1), ylim=(0,1)) grid(True) plot(x) show()