dqx13503925528 2018-06-13 18:10
浏览 329
已采纳

如何在go-lang中创建一组正态分布的数字,围绕特定数字设置

Creating a normally distributed set of numbers in Go can be accomplished as follows.

func getNormDistro(x int){
    for i := 0; i < x; i++{
        fmt.Print("   ",int(normalInverse(80,10)), "   ")
    }
}

func normalInverse(mu float32, sigma float32)(float32){
    return float32(rand.NormFloat64() * float64(sigma) + float64(mu))
}

Running this:

getNormDistro(10)

prints this:

   86      74      79      94      73      92      66      77      74      78    

The only problem is the Distribution is not consistent and has a random aspect to it. This means the curve generated might not fit the normal distribution cure as closely as possible.

How would one make a function that does follow a normal distribution as closely as possible in an algorithmic fashion that has no random component in the output?

So, I know this is possible because you can easily do it in excel. Excel has this function =NORMINV(E3,C3,D3) that takes 3 perimeters the %chance of occurrence(as a decimal), the mean of your distribution, and the standard deviation.This function returns the value of the percent chance that is entered.

If you iterate through the percent chance of occurrence with a specific mean and standard deviation at .5 you would have your mean and if your iteration was evenly spaced between 0% and 100% a standard distribution would be crated with no random component.

    Data Set    Mean    Standard Deviation  % Chance
56.73652126 80  10  0.01
56.73652126 80  10  0.02
61.19206392 80  10  0.03
62.49313929 80  10  0.04
63.55146373 80  10  0.05
64.45226405 80  10  0.06
65.24208972 80  10  0.07
65.9492844  80  10  0.08
66.59244966 80  10  0.09
67.18448434 80  10  0.1
67.7347188  80  10  0.11
68.25013208 80  10  0.12
68.73608871 80  10  0.13
69.19680659 80  10  0.14
69.63566611 80  10  0.15
70.05542117 80  10  0.16
70.45834747 80  10  0.17
70.84634912 80  10  0.18
71.22103705 80  10  0.19
71.58378766 80  10  0.2
71.93578753 80  10  0.21
72.27806786 80  10  0.22
72.61153151 80  10  0.23
72.93697437 80  10  0.24
73.2551025  80  10  0.25
73.56654595 80  10  0.26
73.87187009 80  10  0.27
74.17158493 80  10  0.28
74.4661528  80  10  0.29
74.75599487 80  10  0.3
75.04149653 80  10  0.31
75.32301201 80  10  0.32
75.60086834 80  10  0.33
75.87536871 80  10  0.34
76.14679534 80  10  0.35
76.41541207 80  10  0.36
76.68146654 80  10  0.37
76.94519212 80  10  0.38
77.20680966 80  10  0.39
77.46652897 80  10  0.4
77.72455023 80  10  0.41
77.98106521 80  10  0.42
78.23625835 80  10  0.43
78.49030785 80  10  0.44
78.74338653 80  10  0.45
78.99566279 80  10  0.46
79.24730138 80  10  0.47
79.49846417 80  10  0.48
79.74931092 80  10  0.49
80  80  10  0.5
80.25068908 80  10  0.51
80.50153583 80  10  0.52
80.75269862 80  10  0.53
81.00433721 80  10  0.54
81.25661347 80  10  0.55
81.50969215 80  10  0.56
81.76374165 80  10  0.57
82.01893479 80  10  0.58
82.27544977 80  10  0.59
82.53347103 80  10  0.6
82.79319034 80  10  0.61
83.05480788 80  10  0.62
83.31853346 80  10  0.63
83.58458793 80  10  0.64
83.85320466 80  10  0.65
84.12463129 80  10  0.66
84.39913166 80  10  0.67
84.67698799 80  10  0.68
84.95850347 80  10  0.69
85.24400513 80  10  0.7
85.5338472  80  10  0.71
85.82841507 80  10  0.72
86.12812991 80  10  0.73
86.43345405 80  10  0.74
86.7448975  80  10  0.75
87.06302563 80  10  0.76
87.38846849 80  10  0.77
87.72193214 80  10  0.78
88.06421247 80  10  0.79
88.41621234 80  10  0.8
88.77896295 80  10  0.81
89.15365088 80  10  0.82
89.54165253 80  10  0.83
89.94457883 80  10  0.84
90.36433389 80  10  0.85
90.80319341 80  10  0.86
91.26391129 80  10  0.87
91.74986792 80  10  0.88
92.2652812  80  10  0.89
92.81551566 80  10  0.9
93.40755034 80  10  0.91
94.0507156  80  10  0.92
94.75791028 80  10  0.93
95.54773595 80  10  0.94
96.44853627 80  10  0.95
97.50686071 80  10  0.96
98.80793608 80  10  0.97
100.5374891 80  10  0.98
103.2634787 80  10  0.99
110.9023231 80  10  0.999
117.1901649 80  10  0.9999
122.6489079 80  10  0.99999
127.5342431 80  10  0.999999
49.09767694 80  10  0.001
42.80983515 80  10  0.0001
37.35109206 80  10  0.00001
32.46575691 80  10  0.000001
28.00662418 80  10  0.0000001
23.87998756 80  10  0.00000001
  • 写回答

1条回答 默认 最新

  • douhuzhi0907 2018-06-15 19:27
    关注

    The mathematical description of a random distribution is usually via its probability distribution function. Which for a continuous distribution like the normal distribution is either expressed as a probability density function (the bell-shaped curve you might know) or a cumulative distribution function which is its integral. So perhaps you want to compute that function.

    On the other hand, your use of the term inverse both in your implementation and in the Excel function you cite suggests that you are in fact looking for the inverse operation: given a probability, find the value where the cumulative probability function reaches that value. That is called the quantile for the given probability.

    Unfortunately the normal distribution has a pretty complicated formula that is hard to compute. Its quantile function isn't easier. Therefore instead of trying to get that computation right, I'd suggest you use some library providing that functionality. A bit of web search suggests this:

    https://godoc.org/gonum.org/v1/gonum/stat/distuv#Normal.Quantile

    func (n Normal) Quantile(p float64) float64
    

    Quantile returns the inverse of the cumulative probability distribution.

    A look at the underlying implementation suggests that I wouldn't want to work this out myself. Also note that it will be an approximation (looks like a piecewise combination of Padé approximants), and I don't see any mention of error bounds at first glance. Other libraries might have better aproximations, or better guarantees.

    If you indeed want an array of numbers following this distribution, pick probabilities equidistant from (0,1) (which could mean either from 1/(n+1) through n/(n+1) or 1/(2n), 3/(2n), … (2n−1)/(2n) depending on application or taste) and compute quantiles for these. If you really want integers, you can round afterwards. But you might also want to check whether a continuous distribution is the right thing for you, or whether you would be better off with a discrete distribution like the binomial distribution.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助