Using distance calculation with the spherical law of cosines to detect fraud in ecommerce
Published on: 2024-08-10 18:29:56
This article shows how to use the Spherical Law of Cosines to calculate the great-circle distance between two latitude-and-longitude points. It then shows how to place that calculation in a decision engine for customer verification, geo-fencing, and ecommerce fraud detection. You will see the equation, the implementation steps, a comparison with Haversine, and the accuracy trade-offs for shorter and longer distances.
The Spherical Law of Cosines is a practical method for making decisions from geographic coordinates. In this article, you will place the formula in a decision engine and use the resulting distance in checks for customer verification, geo-fencing, and fraud detection in ecommerce. Follow the guide to get started.
Introduction to the Spherical Law of Cosines
The Spherical Law of Cosines takes the latitude and longitude of two points on a sphere and returns the distance along the sphere's surface. In a decision engine, that number can be used when a decision flow reads geographic coordinates.
Spherical Law of Cosines vs. Haversine: Comparing Distance Formulas on a Sphere
The "Spherical Law of Cosines" formula gives less accurate distances than the Haversine formula, especially when the two points are far apart. It is still often used because its equation is simpler to implement and does not require the atan2 function. If your distance calculation needs higher accuracy, use the Haversine formula.
The Spherical Law of Cosines Formula
The general formula for the Spherical Law of Cosines is:
distance = acos(sin(φ1) * sin(φ2) + cos(φ1) * cos(φ2) * cos(λ2 - λ1)) * R
distance is the distance between the two points. φ1 and φ2 are the points' latitudes, and λ1 and λ2 are their longitudes. R is the sphere's radius; here, it is the Earth's radius. You can use any Earth-radius value. A common choice is 6371 km, or 6371000 meters.
Implementing the Spherical Law of Cosines in a Decision Engine
To place the Spherical Law of Cosines in a decision engine, the engine must provide mathematical functions such as arccos, sin, and cos. Decisimo is one example. If you use another decision engine, check its list of mathematical functions before you start the implementation.
Step-by-Step Guide to Implementing the Spherical Law of Cosines
Here is a step-by-step guide to implementing the Spherical Law of Cosines in a decision engine:
- Convert each latitude and longitude from degrees to radians. The Spherical Law of Cosines formula expects radians, not degrees. Use this conversion:
- radians = degrees * (π / 180)
- Define a function that accepts the latitude and longitude of both points and returns the distance between them. Here is an example of what the function might look like:

- In the function, specify the latitude and longitude for point A and point B. During execution of the decision flow, those places can receive the corresponding values.
- Use the function in the relevant part of your decision flow to calculate the distance between the two points. For example, a rule can check whether the distance is greater than a threshold, or select the point closest to a third point.
Using Distance Calculation for Customer Verification
The distance between two points can provide a customer's position for checks in several contexts.
For example, compare the geo-coordinates from a customer's mobile phone with the location of a lending point of sale. The comparison can check whether the customer is physically present at that location. This is one use of geo-fencing: financial services are allowed only inside a defined area, and loan applications from outside that area are rejected.
Using Distance Calculation for Fraud Detection in Ecommerce
In ecommerce, calculate the distance between a customer's reported address and a location derived from technical proxy data such as an IP address, or from other geolocation proxy data. A large gap between the two locations can help identify a potentially fraudulent transaction.
For example, an IP address may place a customer in one country while the distance between the customer's reported address and the IP-based location is much greater than expected. That difference may indicate that the customer's true location differs from what they reported. Used alongside other fraud detection techniques, this can help protect businesses from fraudulent transactions.