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
Solved

plotting in python using binomial

Posted on 2011-09-02
5
519 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 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Want to delete all my personal data 13 146
How to implement server side pagination with Flask and DataTables 7 844
Perl Frameworks 1 98
mysql database, schema and table creation 13 57
Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
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…
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 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…

856 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