Latin Hypercube Sampling from a normal distribution (Python)

…衆ロ難τιáo~ 提交于 2020-01-05 07:14:11

问题


How to generate 10 random numbers from normal distribution using latin hypercube sampling technique in python 2.7? The range of the random number should be 5 to 14.

I tried following

import random
from random import randint
iter = 10
segSize = 1 / iter
for i in range(iter):
segMin = i * segSize
point = segMin+ (random.normalvariate(7.5,1)*segSize)
pointValue = (point * (14 - 5)) + 4
print point
print pointValue

Thanks


回答1:


Try this:

def rand:
 import random
 from random import randint
 iter = 10
 segSize = 1/float(iter)
 for i in range(iter):
         segMin = float(i) * segSize
         point = segMin + (random.normalvariate(7.5,1) * segSize)
         pointValue = (point * (14 - 5)) + 4
         print point
         print pointValue

Your issue seems to have been integer multiplication etc, which Python truncates to zero in your division.

When I run it, I get:

0.686848045493
10.1816324094
0.871425699273
11.8428312935
1.08794202088
13.7914781879
1.08502172623
13.7651955361
1.24462345735
15.2016111161
1.10687801576
13.9619021418
1.1394488663
14.2550397967
1.37407532844
16.3666779559
1.54666717385
17.9200045647
1.6465869841
18.8192828569


来源:https://stackoverflow.com/questions/12884361/latin-hypercube-sampling-from-a-normal-distribution-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!