简体   繁体   中英

Python random library: Simulating from Pareto distribution (using shape & scale params)

According to the Python docs, random.paretovariate(alpha) simulates from the Pareto distribution where alpha is the shape parameter. But the Pareto distribution takes both a shape and scale parameter.

How can I simulate from this distribution specifying both parameters?

You can use NumPy instead:

from numpy import random

pareto = random.pareto(a=4, size=(4, 8))

print(pareto)

>>>[[0.32803729 0.03626127 0.73736579 0.53301595 0.33443536 0.12561402
  0.00816275 0.0134468 ]
 [0.21536643 0.15798882 0.52957712 0.06631794 0.03728101 0.80383849
  0.01727098 0.03910042]
 [0.24481661 0.13497905 0.00665971 0.41875676 0.20252262 0.13701287
  0.06929994 0.05350275]
 [0.93898544 0.02621125 0.0873763  0.15660287 0.31329102 3.95332518
  0.09149938 0.08415795]]

You can also nicely graph the data using matplotlib and seaborn :

from numpy import random
import matplotlib.pyplot as plt
import seaborn

seaborn.distplot(random.pareto(a=4, size=1000), kde=False)

plt.show()

图形

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM