英文:
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("Mean =", mean)
print("var =", var)
with open(rf"Radius info_{Nodes}ND.txt", 'w+') as f:
f.write(f"mean = {str(mean)}\n")
f.write(f"var = {str(var)}\n")
#f.write(f"std = {str(std*1e6)}\n")
r=pd.Series(r*1e6)
#print("r =",r)
r.hist()
plt.xlabel("Pore throat size [\u03BCm]",size=15)
plt.ylabel("Count",size=15)
plt.title(rf"var={round(var,1)}",size=25)
plt.savefig(rf"C:\Users\USER\OneDrive - Technion\Research_Technion\Python_PNM\Surfactant A-D0 nodes_2\Histogram.png")
def function(run):
parent_folder = str(run + 1)
os.mkdir(parent_folder)
with open(os.path.join(parent_folder, rf"Inv_Radius_{Nodes}_{round(mean,1)}_{round(var,1)}ND.csv"), 'w+') as f:
np.random.shuffle(r2[0][1:-1])
#print(f'{run}:{r2}')
inv_r=1/r2
#print("inv_r =",[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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论