Showing posts with label Monte Carlo integration. Show all posts
Showing posts with label Monte Carlo integration. Show all posts

Saturday, August 12, 2023

HP Prime: Monte Carlo Integration

HP Prime:  Monte Carlo Integration



Integration by Random Points



There are many ways to calculate the area under the curve f(x) in the interval [a, b].     Two of the common are calculating the antiderivative and using the Simpson's Rule.  Another method is the Monte Carlo method.   Unlike Simpson's Rule, where the intervals are fixed, the Monte Carlo method picks random points in interval.  The integral is calculated using the simple formula:


∫ f(x) dx ≈ (b - a) ÷ (n - 1) * Σ( f(x_i), from i = 0 to n)


x_i is a random point in the interval [a, b]

n = number of points, n > 2


How are the random numbers are picked are determined by various methods: pseudo-random generators, using a uniform distribution, using a normal distribution, etc.  


The program MONTE for the HP Prime uses the RANDOM function.  The HP Prime allows to pick a random (real) number in the interval [a , b] by the syntax RANDOM(a,b). 


Note:  I use the Function app function AREA to calculate the integral using any of the system variables F0 through F9.  In trade, the program uses global variables instead of local variables.  



HP Prime Program:  MONTE


Code: 


EXPORT MONTE()

BEGIN

// 2023-06-03 EWS

STARTAPP("Function");

// use input box for global variables

INPUT({A,B,E},"Monte Carlo of F1(X)",

{"lower:","upper:","# places:"}); 

// set radians

HAngle:=0;

// input check

IF A≥B THEN

MSGBOX("Lower limit must be

less than upper limit.");

KILL;

END;

// AREA must be stored to a global var.

I:=Function.AREA(F1(X),A,B);

// initialize variables

N:=1;

S:=0;

// repeat loop

REPEAT 

X:=RANDOM(A,B);

S:=S+F1(X);

N:=N+1;

IF N>1 THEN

J:=(B-A)*S/(N-1);

END;

UNTIL (N>1) AND (ABS(I-J)<ALOG(−E));

// results

PRINT();

PRINT("Results");

PRINT("Actual: "+STRING(I));

PRINT("Approx: "+STRING(J));

PRINT("# terms used: "+STRING(N)); 

// return home

STARTVIEW(−1);  

END;



Examples


Estimate the integral of F1(X) to three decimal places.  Your mileage may vary. 


∫ F1(X) dX from X = A to X = B


Example 1:

F1(X) = X^2 + 1

A = 0,  B  = 3


Exact Integral = 12


Trial 1:  12.0004750664,  64 points

Trial 2:  11.9996447323,  272 points

Trial 3:  12.0001030297,  9 points


Example 2: 

F1(X) = COS(X - 1) 

A = 1,  B = π/2 + 1


Exact Integral = 1


Trial 1:  1.00079319497, 411 points

Trial 2:  .999705900656, 187 points

Trial 3:  1.00071067474, 737 points



Example 3:

F1(X) = SIN X/X

A = 0, B = 4


Exact Integral = Si(4) ≈ 1.75820313895


Trial 1:  1.75789723089, 205 points

Trial 2:  1.75729880586, 133 points

Trial 3:  1.75832553216, 223 points



Notes


While the Monte Carlo method is easy to calculate, it is difficult to get an accurate answer.  The method requires a lot of calculation points, and how many really depends on what random numbers are picked.  It's really the luck of a draw.   This is good for a short approximation but I recommend the Simpson's Rule, Trapezoid Rule, or when possible and feasible, finding the antiderivative instead.  



Source


Cumer, Victor.  "The basics of Monte Carlo integration"  Towards Data Science.   Medium.  October 26, 2020.  Last Retrieved June 4, 2023.  https://towardsdatascience.com/the-basics-of-monte-carlo-integration-5fe16b40482d


Eddie


All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


Saturday, June 3, 2023

Carnival of Mathematics # 216

Carnival of Mathematics # 216



Welcome to a special edition of Eddie's Math and Calculator Blog.  I am once again happy to host the Carnival of Mathematics, which this is the 216th Edition.  The Carnival of Mathematics is facilitated by The Aperiodical.   

Check out the monthly series, where you can also submit articles for future carnival articles, at https://aperiodical.com/carnival-of-mathematics/.

May 2023's Carnival was hosted by Cassandra from Cassandra Lee Yieng's Blog:  https://leeyieng.wordpress.com/2023/05/01/carnival-of-mathematics-215-april-2023/

July 2023's Carnival will be hosted by Vaibhav at Double Root.  Double Root's blog:  https://doubleroot.in/

Gratitude to Ioanna Georgiou for the invitation to host once again.    




The Number 216


In a regular, non-leap year, the 216th day of the calendar is August 4.  On leap years, the 216th day is August 3. 


The digital root of 216 is 9.  (2 + 1 + 6 = 9).


The prime factorization of 216 is 2^3 * 3^3.   Note that 216 = 6^3.  


The divisors of 216 are:  1, 2, 3, 4, 6, 8, 9, 12, 18, 24, 27, 36, 54, 72, 108, and 216.  The sum of the proper divisors are 384, making 216 an abundant number.  


216 hours is 9 days exactly.


216 can be expressed as sums and differences of powers, including:


216 = 3^5 - 3^3

216 = 2 * 10^2 + 4^2

216 = 12^2 + 9^2 - 3^2

216 = 4^4 - (6^2 + 2^2)

216 = 8^3 - (14^2 + 10^2)



The Carnival


Thank you to everyone who submitted an article.  You can submit entries at https://aperiodical.com/carnival-of-mathematics/.



SineRider 


This is a really cool game about mathematics.  The goal is to set a curve so that the snow-people successfully pass through a designated number of gates.  At first, the curves are linear but they get more challenging as the game goes on.  The game also saves your progress. (Screen shots are from the game.)






View Hack Club's intro video on Youtube:  https://www.youtube.com/watch?v=35nDYoIwiA8


Play the game here:  https://sinerider.com/




The Game of Hex:  Submitted by Massimo Dacasto





The game of Hex has two players.  The goal is to connect a string of hexagons from one side to the opposite side.  Each player claims a hexagon with their own color, such as red and blue.  The game of Hex is featured in the 1980s game show Blockbusters.  


The web page Hall of Hexagons is a gallery of moves a player can make to guarantee themselves a connection over a set number of rows.  Check out the gallery here:  https://www.drking.org.uk/hexagons/hex/templates.html.




Peirce's Law:  Jon Awbrey of the Inquiry Into Inquiry blog


This article explains Pierce's Law and provides the proof of the law.  The proof is provided in two ways:  by reason and graphically.  Simply put, for propositions P and Q, the law states:


P must be true if there exists Q such that the statement "if P then Q" is true.  In symbols:


(( P ⇒ Q) ⇒ P) ⇒ P


Article:  https://inquiryintoinquiry.com/2008/10/06/peirces-law/


The law is an interesting tongue twister to say the least.




Praeclarum Theorema:  Jon Awbrey of the Inquiry Into Inquiry blog



Jon Awbrey provides an explanation and proof of the Praeclarum Theorema, also known as the Splendid Theorem, which states:


For statements a, b, c, and d:  


((a ⇒ b) ^ (d ⇒ c)) ⇒ ((a ^ d) ⇒ (b ^ c))


Article:  https://inquiryintoinquiry.com/2008/10/05/praeclarum-theorema/



A Spiral on the Brighton Seafront:  Sam Hartburn


The place is the Brighton Seafront in Brighton, UK.  On the sea front there is a spiral of poles, which is the ratio of radii of the large circle to small circle is about the Golden Ratio ( (1 + √5) ÷ 2 ).   


See this spiral here:  https://samhartburn.co.uk/sh/maths-tourism-a-spiral-on-brighton-seafront/



Green's Windmill & Science Centre


In another travel related article, Tom, an Education Officer and blog writer of mathematics and astronomy, reviews the Green's Windmill and Science Centre in Nottingham, UK.  By the picture posted on Tom's post, this is a place I would like to visit one day.   Tom praises the Windmill for giving vision to the mathematics behind windmills, including displaying an integral equation related to electricity and magnetism:  


∫ dσ V dU/dw + ∫ dx dy dx V δU = ∫ dδ U dV/dw + ∫ dx dy dz U δV


The blog entry also features the work of George Green, a British mathematician.   


Tom's Blog Entry:  https://tommaths.blogspot.com/2023/05/greens-windmill-science-centre-museum.html


Green's Mill and Science Centre's Web Site:  https://tommaths.blogspot.com/2023/05/greens-windmill-science-centre-museum.html



Golden Integration:  John D. Cook


Speaking of the Golden Ratio, John d. Cook demonstrates Monte Carlo integration but instead of using a purely random sequence, a pseudo-random sequence using the fractional values of n * φ, where φ = ( (1 + √5) ÷ 2 ).  The method is demonstrated using Python.


Article:  https://www.johndcook.com/blog/2023/04/29/golden-integration/


Monte Carlo integration has an advantage of ease of calculation, with a cost of having to sample a large amount of points to get an accurate answer.  




A Plan to Address the World Challenges With Math:  Kevin Harnett of Quanta Magazine


The article is an interview with mathematician Minhyong Kim, Director of the International Centre of Mathematical Sciences (ICMS) in Edinburgh, Scotland.  The goal is ICMS is to focus mathematics with humanitarian studies. 


In the article, Kim emphasizes that young mathematicians want to work in diverse fields outside the traditional subjects.  


Article:  https://www.quantamagazine.org/a-plan-to-address-the-worlds-challenges-with-math-20230511/


Web page for ICMS:  https://www.icms.org.uk/


The ICMS's web page for The Mathematics for Humanity program, which has recently launched:  https://www.icms.org.uk/funding-opportunities/mathematics-humanity



Hamilton's Quaternions, or, The Trouble with Triples:  Article by James Propp


Math with a romantic story between William Rowan Hamilton and Catherine Barlow.  Hamilton's infatuation for Barlow lasted, despite the fact that Barlow was married to another man.   Barlow's son needed a tutor in mathematics, and Hamilton was the man to call.  While Hamilton was tutoring her son, he and Barlow discussed Barlow's unhappy marriage.   


Propp then details who Hamilton and Barlow first met, along with the math of quaternions in detail.  


A quaternion is defined an extended complex number represented in the form


q1 * i + q2 * j + q3 * k + q4, with q1, q2, q3, and q4 are real numbers.


When multiplying quaternions the following rules are observed:


i^2 = j^2 = k^2 = -1

i * j = k,   j * i = -k

j * k = i,   k * j = -i

k * i = j,   i * k = -j


The article:  https://mathenchant.wordpress.com/2023/05/17/hamiltons-quaternions-or-the-trouble-with-triples/



Degree of ζ(3)


Here is a stack exchange talking about the integral representation of ζ(3)  (Riemann Zeta function of 3).  As of June 2, 2023, the post has the option question regarding a degree of a group of permutations.   


The open question:  https://math.stackexchange.com/questions/4703662/degree-of-zeta3-as-a-period




I would like to thank everyone who submitted articles for the Carnival of Mathematics.  Gratitude to the Aperiodical for having me as this month's host.   


Eddie 




All original content copyright, © 2011-2023.  Edward Shore.   Unauthorized use and/or unauthorized distribution for commercial purposes without express and written permission from the author is strictly prohibited.  This blog entry may be distributed for noncommercial purposes, provided that full credit is given to the author. 


RPN HP 12C: Fibonacci and Lucas Sequences

  RPN HP 12C: Fibonacci and Lucas Sequences Golden Ratio, Formulas, and Sequences Let φ be the Golden Ratio: φ = (1 + √5) ÷ 2...