Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

plotting in python using binomial

Posted on 2011-09-02
5
Medium Priority
?
550 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
ID: 36477842
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 29

Expert Comment

by:pepr
ID: 36478460
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
ID: 36480030
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 29

Accepted Solution

by:
pepr earned 1000 total points
ID: 36480601
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
ID: 36487413
Many thanks
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
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…
Suggested Courses

886 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