2016-04-01 7 views
0

Ich möchte Daten in Python generieren, als ob es einige experimentelle Punkte war. Ich möchte laute exponentielle Abnahme bekommen, mit Rauschen und es sind normalerweise verteilte Fehler. Wie dieses Bild, aber exponentiell: noisy polynomial data. Wird es in Ordnung, wenn ich einfach exponentielle Kurve nehmen und etwas Gaußsche Rauschen, um es hinzuzufügen und auch zufällige Fehler wie dieseGenerieren von zufälligen Daten in Python passend gegebenen Funktion

import numpy as np 
errors = np.random.normal(0,1,100) 

Oder es kann in einer intellektuellen Weise erzeugen getan werden?

Antwort

0

Dies ist die Lösung für das Problem. Ich weiß nicht, ob es richtig ist, aber ich tat es trotzdem:

import numpy as np 
import matplotlib.pyplot as plt 
import scipy.stats as st 
import random 
from scipy.optimize import curve_fit 

#number of data points 
n = 50 

#function 
def func(data): 
    return 10*np.exp(-0.5*data) 

def fit(data, a, b): 
    return a*np.exp(b*data) 

#define interval 
a = 0 
b = 4 

#generate random data grid 
x = [] 
for i in range(0, n): 
    x.append(random.uniform(a, b)) 
x.sort() 

#noise-free data points 
yclean = [] 
for i in range(0, n): 
    yclean.append(func(x[i])) 

#define mean, standard deviation, sample size for 0 noise and 1 errors 
mu0 = 0 
sigma0 = 0.4 
mu1 = 0.5 
sigma1 = 0.02 

#generate noise 
noise = st.norm.rvs(mu0, sigma0, size = n) 
y = yclean + noise 
yerr = st.norm.rvs(mu1, sigma1, size = n) 

#now x and y is your data 
#define analytic x and y 
xan = np.linspace(a, b, n) 
yan = [] 
for i in range(0, n): 
    yan.append(func(xan[i])) 

#now estimate fit parameters 
#initial guesses 
x0 = [1.0, 1.0] 
#popt are list of optimal coefficients, pcov is covariation matrix 
popt, pcov = curve_fit(fit, x, y, x0, yerr) 

fity = [] 
for i in range(0, n): 
    fity.append(fit(xan[i], *popt)) 

print 'function used to generate is 10 * exp(-0.5 * x)' 
print 'fit function is', popt[0], '* exp(', popt[1], '* x)' 

#plotting data and analytical function 
plt.rc("figure", facecolor="w") 
plt.rc('text', usetex=True) 
plt.rc('font', family='serif',size = 16)  
plt.title("Data", fontsize=20) 
plt.errorbar(x, y, yerr, fmt='o') 
plt.plot(xan, yan, 'r') 
plt.plot(xan, fity, 'g') 
plt.show()