Math Is Fun Forum
  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2010-04-24 13:14:22

calccrypto
Member
Registered: 2010-03-06
Posts: 96

how to integrate using python

is there a way to integrate without going through an entire list of numbers (Riemann sums and the like) or using symbolic integration while still getting an accurate answer?


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#2 2010-04-24 13:33:24

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi calccrypto;

Yes, there are many techniques for numerical integration both with functions and with lists of data points. Can you be a little more specific as to what you are trying to do. Incidentally there are good routines for numerical integration in the sympy package for python.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#3 2010-04-24 15:42:24

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

im trying to write Cumulative distribution functions, like normalcdf, χ2cdf, tcdf

hello bobbym

Last edited by calccrypto (2010-04-24 18:04:44)


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#4 2010-04-24 18:13:51

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi;

Have you taken a look at sympy which is for python. It may already have some of what you need. If not you will have to have to implement a numerical scheme such as simpsons, gaussian or romberg integration.

There are also polynomial approximations that deliver good accuracy and are simpler.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#5 2010-04-25 06:12:37

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

thanks!


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#6 2010-04-25 06:42:00

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

If you need any help with them just post back.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#7 2010-05-08 13:54:52

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

im having a little trouble. when doing normalcdf (using simpson's rule), the values are off slightly, but since the real values are small to begin with, the errors are pretty big. i need 2 values 0.219194 and 0.114866, but im getting 0.22649523318724421 and 0.13293551413661003


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#8 2010-05-08 17:31:29

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi cal;

What are integrating from? From what to what?


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#9 2010-05-10 10:30:24

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

im trying to do ∫ normalpdf(x) (which i believe is e^(-.5x^2)/sqrt(2pi)) dx for:

n = 100, z = 1.6 | 1.9
1 - ∑ k from (-n/z+1)/4 to (n/z+1)/4 of [normalcdf(-100,(4k+1)z/sqrt(n)) - normalcdf(-100,(4k-1)z/sqrt(n))] + ∑ k from (-n/z-3)/4 to (n/z-1)/4 of [normalcdf(-100,(4k+3)z/sqrt(n)) - normalcdf(-100,(4k+1)z/sqrt(n))]

this is from nist's statistical tests for randomness


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#10 2010-05-10 14:42:26

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi cal;

Bear with me for a little bit.

Is this what you want?


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#11 2010-05-10 22:21:16

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

no.  1.6/1.9 are the 2 values to use for z in that big mess i have in the code block


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#12 2010-05-10 22:24:33

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi calccrypto;

Since a CDF is an area under the SNC do you know what values you are trying to integrate between?


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#13 2010-05-11 08:13:49

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

from -100 to the z scores, like normalcdf(-100,1.6) (using -100 because -infinity is a little too much), except the formula is a sum of normalcdfs

i dont know if this helps but: http://www.scribd.com/doc/11305936/NIST-Statistical-Test-Suite-for-Random-and-PseudoRandom
page 63; 2.14.8 Example


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#14 2010-05-11 08:35:07

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi calccrypto;



As you can see from above that your cutoff of -100 for - infinty is introducing a small but noticeable error in the CDF

Below is a closed form for the CDF, If you put 1.6 in there you will get the value produced by the second integral 0.945200708300442.

But for the life of me, I cannot determine how he is getting those 2 numbers.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#15 2010-05-11 10:09:25

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

thanks bobbym!

and the values came from 1-(sum of a bunch of normalcdfs)

edit: oops. i think i referred to the wrong pages. the number on the actual page says 54, but the pdf page says 63. the formula is on the bottom of (pdf) page 62

Last edited by calccrypto (2010-05-11 10:21:37)


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#16 2010-05-11 13:45:18

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi calccrypto;

Where are we now? What do you need? Can you follow that formula because I can hardly see it?


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#17 2010-05-11 14:16:02

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

im just trying to get a more accurate answer, to at least the thousandths place since for p values, they are usually between .01 and .1 ...

you know what? forget this. i have another question: how do you use the Gauss–Kronrod quadrature formula to integrate? what's with those special nodes?


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#18 2010-05-11 15:29:56

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi calccrypto;

Originally, numerical integration using newton - cotes formulas stuck to the idea of sampling the function at equally spaced points. This includes simpsons rule and the
trapezoidal rule as well as Bodes rule... It was Gauss who discovered the way to sample only the best points of a function. To make the best estimate
he had to drop the limitations of equally spaced abscissa. To do that he made use of the so called Legendre polynomials. The nodes are the roots
of those polynomials.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#19 2010-05-11 15:47:40

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

oh deary me... ive gotten myself into more complicated stuff than ive attempted to learn. oops. i think i'll hold off asking more about this for a few more months, when i finally get into college and have a better background in calc

thanks so much for the help bobbym!


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#20 2010-05-11 15:56:55

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi calccrypto;

Your welcome, sorry I couldn't help you more. Sorry, to jargon you to death, I was just trying to give you a little background. If you feel you would like to wait until you can receive some formal training that is cool. You could also start training yourself. The internet has everything you need to get started and you can always bring your unsolved problems in here. Some one will help you. Do not be discouraged that book was very hard for me as well.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#21 2010-05-11 22:19:58

calccrypto
Member
Registered: 2010-03-06
Posts: 96

Re: how to integrate using python

i try to teach myself here and there and i have learned quite a bit since 2 years ago, but ive had limited experience with polynomials other than using polynomial division to write the AES algorithm in python (a big jump from having a lookup table). i still dont understand so much about polynomials that i would love to know, but are too advanced to learn by skipping around to something interesting that i see


Visit calccrypto.wikidot.com for detailed descriptions of algorithms and other crypto related stuff (not much yet, so help would be appreciated).

Offline

#22 2010-05-11 22:47:14

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi calccrypto;

I know the feeling.


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#23 2011-08-14 23:05:26

bobbym
Administrator
From: Bumpkinland
Registered: 2009-04-12
Posts: 85,199

Re: how to integrate using python

Hi carol33;

Welcome to the forum!


In mathematics, you don't understand things. You just get used to them.
Of course that result can be rigorously obtained, but who cares?
Combinatorics is Algebra and Algebra is Combinatorics.

Offline

#24 2011-09-02 12:32:50

ShivamS
Member
Registered: 2011-02-07
Posts: 3,523

Re: how to integrate using python

from scipy import integrate
def myfunc(x, a, b):
return (x**b) + a
 
args = (1.0, -2.0)
 
results = integrate.quad(myfunc, 0.5, 1.5, args)
print 'Integral = ', results[0], ' with error = ', results[1]

Offline

Board footer

Powered by FluxBB