根据标准差(sigma)改变正态分布的方差。

huangapple go评论63阅读模式
英文:

Changing variance based on sigma for a normal distribution

问题

我正在创建一个不同运行的随机正态分布数组并将其写入```csv```文件我正在打印每次运行的```均值方差```。以下是当前和预期的输出

当前输出是

```plaintext
Mean = 49.99999999999999
var = 1.0
Mean = 49.99999999999999
var = 1.0
Mean = 49.99999999999999
var = 0.9999999999999996
Mean = 49.99999999999999
var = 1.0000000000000004
Mean = 49.99999999999999
var = 1.0

预期输出是

Mean = 49.99999999999999
var = 10.0
Mean = 49.99999999999999
var = 10.0
Mean = 49.99999999999999
var = 0.9999999999999996
Mean = 49.99999999999999
var = 10.0000000000000004
Mean = 49.99999999999999
var = 10.0

<details>
<summary>英文:</summary>

I am creating an array of random normal distribution for different runs and writing to a ```csv``` file. I am printing the ```mean,var``` for each run. I present the current and expected output. 

from scipy.stats import truncnorm
import numpy as np
import os
import csv
import pandas as pd
import random
import matplotlib.pyplot as plt

Runs=5

for i in range(0,Runs):

mu, sigma = 50.0, 10.0 # mean and standard deviation
Nodes=220
r = (1e-6)*np.random.normal(mu, sigma, Nodes)

sort_r = np.sort(r)
r1=sort_r[::-1]
r1=r1.reshape(1,Nodes)
r2 = r.copy()
np.random.shuffle(r2.ravel()[1:])
r2=r2.reshape(1,Nodes)
maximum = r2.max()
indice1 = np.where(r2 == maximum)

r2[indice1] = r2[0][0]
r2[0][0] = maximum

r2[0][Nodes-1] = maximum 

a = (1e-6) * (50 + (r - r.mean()) / r.std())
mean=a.mean()*1e6
var=a.std()*1e6
print(&quot;Mean =&quot;, mean)
print(&quot;var =&quot;, var)

with open(rf&quot;Radius info_{Nodes}ND.txt&quot;, &#39;w+&#39;) as f: 

    f.write(f&quot;mean = {str(mean)}\n&quot;)
    f.write(f&quot;var = {str(var)}\n&quot;)
    #f.write(f&quot;std = {str(std*1e6)}\n&quot;)

r=pd.Series(r*1e6)
#print(&quot;r =&quot;,r)
r.hist()
plt.xlabel(&quot;Pore throat size [\u03BCm]&quot;,size=15)
plt.ylabel(&quot;Count&quot;,size=15)
plt.title(rf&quot;var={round(var,1)}&quot;,size=25)
plt.savefig(rf&quot;C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D0 nodes_2\Histogram.png&quot;)

def function(run):
parent_folder = str(run + 1)
os.mkdir(parent_folder)

with open(os.path.join(parent_folder, rf&quot;Inv_Radius_{Nodes}_{round(mean,1)}_{round(var,1)}ND.csv&quot;), &#39;w+&#39;) as f:
    np.random.shuffle(r2[0][1:-1])
      #print(f&#39;{run}:{r2}&#39;)
    inv_r=1/r2
      #print(&quot;inv_r =&quot;,[inv_r])
    writer = csv.writer(f)
    writer.writerows(inv_r)

for x in range(Runs):
function(x)


The current output is

Mean = 49.99999999999999
var = 1.0
Mean = 49.99999999999999
var = 1.0
Mean = 49.99999999999999
var = 0.9999999999999996
Mean = 49.99999999999999
var = 1.0000000000000004
Mean = 49.99999999999999
var = 1.0


The expected output is

Mean = 49.99999999999999
var = 10.0
Mean = 49.99999999999999
var = 10.0
Mean = 49.99999999999999
var = 0.9999999999999996
Mean = 49.99999999999999
var = 10.0000000000000004
Mean = 49.99999999999999
var = 10.0


</details>


# 答案1
**得分**: 1

你需要将 r.std() 除以 sigma(目标标准差):

将:

a = (1e-6) * (50 + (r - r.mean()) / r.std())

替换为:

a = (1e-6) * (mu + (r - r.mean()) / (r.std() / sigma))

输出:

均值 = 49.99999999999999
方差 = 9.999999999999998
均值 = 49.99999999999999
方差 = 10.0
均值 = 49.99999999999999
方差 = 10.0
均值 = 49.99999999999999
方差 = 10.0
均值 = 49.999999999999986
方差 = 10.0
英文:

You have to divide r.std() by sigma (the target std):

Replace:

a = (1e-6) * (50 + (r - r.mean()) / r.std())

with:

a = (1e-6) * (mu + (r - r.mean()) / (r.std() / sigma))

Output:

Mean = 49.99999999999999
var = 9.999999999999998
Mean = 49.99999999999999
var = 10.0
Mean = 49.99999999999999
var = 10.0
Mean = 49.99999999999999
var = 10.0
Mean = 49.999999999999986
var = 10.0

huangapple
  • 本文由 发表于 2023年6月5日 21:52:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76407131.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定