Solved

plotting in python using binomial

Posted on 2011-09-02
5
500 Views
Last Modified: 2012-05-12
Hi

I am trying to learn python and graphing. Could anybody help me with the following?

I want to plot a graph of probabilities using the Binomial which I will write as B(N,n,p); where N is the total number of events, n is the number of successes and p is the probability of a success.

What I want to do is plot the probability of the n events occurring for varying N but fixed n and p.

For example, if n = 20 and p = 0.1, I would like to work out the probability of P(n=20, p=0.1) for N = 20, ... , 100.

Could someone give me the Python code which would allow me to do this?

Thanks in advance

Issac
0
Comment
Question by:IssacJones
  • 3
  • 2
5 Comments
 

Author Comment

by:IssacJones
Comment Utility
Maybe I've made the question too complex?

If it helps, can anybody tell me how to do the following?

(1) Using x in the range 0 to 20, plot y=x^2 and plot the graph.
(2) Is there a library function which will allow me to calculation combiations i.e. nCr values?

Thanks in advance

Issac
0
 
LVL 28

Expert Comment

by:pepr
Comment Utility
Are you searching the tool formaking graphs or you already use some?

For the combinatorics, the standard module itertools implements some generators that return combinations and permutations. See http://docs.python.org/library/itertools.html
0
 

Author Comment

by:IssacJones
Comment Utility
Hi pepr

To plot the graph I was going to use matplotlib (?)

Essentially all I'm asking is given a function, be it y = x^2, y = sin x + x^3, or whatever and an array of x values e.g. x = -2, -1, 0, 5, 10, how do I plot the graph.

I come from a C++ background, so I assumed that you could have an array of x values e.g. x[5] (for the above) from which I could then calculate the corresponding y values y[5], and then pass these into some kind of plotting routine e.g. matplotlip must have something I suspect.

Any ideas?
0
 
LVL 28

Accepted Solution

by:
pepr earned 250 total points
Comment Utility
Well, it depends.  I do not know matplotlib.  It is likely that it offers mathematical functions including somethin like numpy (that is not part of the standard Python).  Python has the math standard module -- see http://docs.python.org/library/math.html and some mathematical functions built into the interpreter.  However, it may be much more efficient to use the matplotlib's equivalents.  It could also be the case that you prepare x list and the function and the matplotlib computes the results on its own.  I do not know.  Just generally, the python list type can be used as a heterogeneous dynamic array, zero-based indexing.  You can calculate a separate list of y values or a list of tuples with the point coordinates, etc:

a.py
def fn(x):
    return x**2

xlst = [-2, -1, 0, 5, 10]
ylst = []
points = []

for x in xlst:
    ylst.append(fn(x))
    points.append( (x, fn(x)) )
    
print xlst
print ylst
print points

Open in new window


It prints:

c:\tmp\_Python\IssacJones\Q_27288526>python a.py
[-2, -1, 0, 5, 10]
[4, 1, 0, 25, 100]
[(-2, 4), (-1, 1), (0, 0), (5, 25), (10, 100)]

Open in new window


0
 

Author Closing Comment

by:IssacJones
Comment Utility
Many thanks
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Plenty of writing has gone on the web trying to compare Python with other competitive programming languages and vice versa. However, not much has been put into a wholistic perspective. This article should help you decide whether to adopt Python as a…
The really strange introduction Once upon a time there were individuals who intentionally put the grass seeds to the soil with anticipation of solving their nutrition problems. Or they maybe only played with seeds and noticed what happened... Som…
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now