kdb Products
Overview
KDB.AI
kdb+
kdb Insights
kdb Insights Enterprise
Capabilities
Anomaly Detection
kdb+ Time Series Database
Liquidity Management
PyKX Python Interoperability
The Data Timehouse
Vector Database Explained
Services & Support
Financial Services
Quant Research
Trading Analytics
Industry & IoT
Automotive
Energy & Utilities
Healthcare & Life Sciences
Manufacturing
Telco
Learn
Overview
Featured Courses
KX Academy
KX University Partnerships
Connect
KX Community
Community Events
Developer Blog
Build
Download
Documentation
Support
About Us
Partner with Us
KX Partner Network
Find a Partner
Partner Signup
Join Us
Connect with Us
by Declan Fallon
To celebrate Pi Day (3/14) we will construct a dashboard which will show how the mathematical constant for the ratio of the circumference of a circle to its diameter can be estimated. We will use the Monte Carlo method[1].
If a circle of radius R is inscribed inside a square with side length 2R, then the area of the circle will be π R2 and the area of the square will be (2R)2. Therefore, the ratio of the area of the circle to the area of the square will be π/4.
So for n points inside the square, approximately n*( π/4) should fall inside the circle.
For the sequence of random co-ordinates, the point will fall inside the line if x2 + y2 < R2, where x and y are coordinates of the point, and R is the radius of the circle.
The calculation for π is (4*m)/n, where m is number of points inside the circle to total number of points n.
We will start with a sequence of random x and y co-ordinate data, with a column for x2 + y2, and R2 that we will initially display in a Data Grid. We want to use 10,000 values so we set the Max Rows to 10,000. We will come back to the data grid later.
Next, we are going to add a Canvas Chart to plot the data. For the random data points we will use a Bubble chart and then set the x and y column to our axis settings.
Then we define both X-axis and Y-Axis as a linear relationship, with two decimal places. Our chart looks like this:
Next, we can add a Circle to our Canvas Chart. To do we will use a Line Chart, which we have configured as two additional layers. I have disabled the Legend as the circle is self-explanatory.
We can show which values are contained inside the circle with a Highlight Rule. As we have a data column which has a value of 1 when the random point is inside the circle, we can set our highlight rule to trigger on the aforementioned column, while applying a color highlight on to the random point itself. This looks like:
Next, we can go back to our Data Grid and configure it to show the total number of dots inside and outside the circle; this can be done using the grouping feature. First, Enable Grouping.
Then we create a grouping relationship for the sum of the columns which measure the number of random dots inside, and outside the circle. Because we have assigned a value of one or zero depending if the point is inside or outside the circle for the “In” column, a straight sum value will give us the total number of points out of the 10,000 plotted which fall inside and outside of the circle. If you haven’t added a column for the “In” you can do so from the Columns menu – this column assigns a value of 1 when the value of R2 is less than one.
With the grouping enabled, we get a sum total of points inside the circle of 7,856. This leaves 2,144 points outside the circle.
From the introduction we have a calculation for π of (4*m)/n, where m is number of points inside the circle to total number of points n.
From the Dashboard, we have a value of m of 7,856, and an n value of 10,000. Our calculation is now
π = (4 * 7,856) / 10,000 = 3.14
[1] http://www.eveandersson.com/pi/monte-carlo-circle