Solved

plotting in python using binomial

Posted on 2011-09-02
5
520 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 250 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

This article will show the steps for installing Python on Ubuntu Operating System. I have created a virtual machine with Ubuntu Operating system 8.10 and this installing process also works with upgraded version of Ubuntu OS. For installing Py…
A set of related code is known to be a Module, it helps us to organize our code logically which is much easier for us to understand and use it. Module is an object with arbitrarily named attributes which can be used in binding and referencing. …
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

733 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