2016-04-21 6 views
0

Ich versuche, mit Punkten zu horten, hoffend am Ende kann ich eine Wahrscheinlichkeitsdichtefunktionssimulation erhalten. Mein Code ist:spyder plotten mit Punkten für ein pdf

import random 
import math 
from numpy import * 
from matplotlib.pyplot import * 
import matplotlib.pyplot as pl 


clock_offset=3000 

y=0 

p=0.50 

for i in range (40): 

    x = random.random() 
    if x < p: 
     clock_offset+=1 

     for 'bo' in (clock_offset,y): 
      y+=1 
     pl.plot(clock_offset,y,'bo') 
     pl.axis([2980, 3040, 0, 40]) 
     y=0 
    else: 
     clock_offset-=1 

     for 'bo' in (clock_offset,y): 
      y+=1 
     pl.plot(clock_offset,y,'bo') 
     pl.axis([2980, 3040, 0, 40]) 
     y=0 

Das Problem ist, ich für Schleife ein nicht schreiben kann, die y + = 1 macht, wenn dieser Ort (clock_offset, y) bereits mit einem Punkt besetzt. Irgendwelche Lösungen?

+0

Was haben Sie eigentlich vor? Das für 'bo' in (clock_offset, y): y + = 1 'macht keinen Sinn. Was hat die Saite "Bo" damit zu tun? – Roberto

Antwort

0

Ich bin mir nicht sicher, was dieser Code von Ihnen tun soll. Aber werfen Sie einen Blick auf this answer of mine, die erklärt, wie man eine Zufallszahl auf einer Verteilung erhält. Ich habe dir einen C++ - Code in Python geschrieben.

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


def GausPDF(x, a=1., b=2., c=3.): 
    return a*math.exp( -((x-b)*(x-b)/(2*c*c) )) 

def random_on_PDF(PDF, top, bottom, maxPDF): 
    x = (top-bottom)*np.random.random()+bottom 
    y = maxPDF*random.random() 

    while(y>PDF(x)): 
     x = (top-bottom)*np.random.random()+bottom 
     y = maxPDF*random.random() 

    return x,y 


x, y, = list(), list() 
for i in range(0, 1000): 
    a,b = random_on_PDF(GausPDF, 10., -5., 1.) 
    x.append(a) 
    y.append(b) 

plt.scatter(x,y) 
plt.show() 

Mit diesem Code und THIS matplotlib Beispiel direkt, können Sie simulieren, wie zufällig Abstimmung beeinflusst/baut ein PDF.

Sind Sie danach?