生成遵循分布的平滑随机数 [英] Generating smoothed randoms that follow a distribution

查看:228
本文介绍了生成遵循分布的平滑随机数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个变量,我们称它们为xy,它们在绘制时是图中的分散蓝点. 我已经使用Scipy的curve_fit拟合了它们.

I have two variables, lets call them x and y, which when plotted are the scattered blue points in the graph. I have fitted them using curve_fit from Scipy.

我想生成(假设为500000)平滑的" 随机数,复制后跟xy的分布.

I want to generate (lets say 500000) "smoothed" random numbers replicating the distribution followed by x and y.

通过平滑" ,我的意思是,我不希望随机变量能够完全复制我的数据(xy),如下面的图 所示,使用 red diamonds being my data distribution and the histogram being my generated randoms. (即使是数据波动也可以在此处复制!!! ).我想要一个平滑的"直方图.

By "smoothed" I mean, I don't want randoms that exactly replicate my data (x and y) like in the figure below, with the red diamonds being my data distribution and the histogram being my generated randoms. (even the fluctuations of the data are replicated here!!!!). I want a "smoothed" histogram.

到目前为止,我尝试使用scipy中的curve_fit来拟合点xy.所以现在我知道数据分配是什么了.现在,我需要创建遵循上述 fit /distribution的随机数.

What I have tried so far is to fit the points x and y using curve_fit from scipy. So now I know what the data distribution is. Now I need to create random numbers that follow the above fit/distribution.

P.S我也尝试过创建从0到1的均匀随机数,并试图使点低于拟合曲线,但是我不知道该怎么做!

P.S I have also tried creating uniform randoms from 0 to 1 and trying to get the points below the fitted curve, but I don't know how!

推荐答案

我建议您调整数据分布,然后在其中添加一些随机的噪声",这将产生一些仍遵循分布但随机化的数据无论您需要什么目的.

I propose that you take your data distribution fit and then add some random "noise" to it, this should produce some data that still follows your distribution but is randomised for whatever purpose you require.

下面是一些适合数据分布的代码(在函数curve中),然后使用

Below is some code which takes a data distribution fit (in the function curve) and then randomised the data that is retrieved from it using the numpy.random module.

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

# I don't have your data but let's assume that this function 
# replicates the data distribution you want to work with.
def curve(x):
    return 2. * x + 5.

N = 100
x = np.linspace(0,1,100)
y_fit = curve(x)

# margin controls how "noisy" you want your fit to be.
margin = 0.5

noise = margin*(np.random.random(N)-0.5)
y_ran = y_fit + noise

plt.plot(x, y_fit) # Plot the fitted distribution.
plt.plot(x, y_ran, 'rx') # Plot the noisy data.

plt.show()

请注意,这只会产生100个随机结果,您可以根据需要将代码修改为所需数量.

Note that this only creates 100 randomised results, you could modify the code to make as many as you need if you wished.

这篇关于生成遵循分布的平滑随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆