Solved

plotting in python using binomial

Posted on 2011-09-02
5
506 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 28

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 28

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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. …
Flask is a microframework for Python based on Werkzeug and Jinja 2. This requires you to have a good understanding of Python 2.7. Lets install Flask! To install Flask you can use a python repository for libraries tool called pip. Download this f…
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 …

919 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

21 Experts available now in Live!

Get 1:1 Help Now