Using distance calculation with the spherical law of cosines to detect fraud in ecommerce

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:

  1. 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)
  2. 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:
    Spherical law of cosines in a decision engine
  3. 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.
  4. 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.
Decisimo decision engine

Put these checks into a decision flow

Create rules for credit, fraud or collections, then run them on live decisions.

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.

Frequently asked questions

What distance cutoff should I use for an ecommerce address and IP location mismatch?

Set the cutoff from your own labelled transaction outcomes, and separate results by geolocation source or region when their accuracy differs. IP locations can be imprecise, so a large distance is a reason to request another check, not proof of fraud on its own. Choose the threshold by balancing missed fraud against legitimate customers sent for review.

When should I use Haversine instead of the spherical law of cosines?

Use Haversine when the calculation needs reliable results across very short distances or when small numerical errors could change a decision. The spherical law of cosines is a simpler option for general great circle distance checks, but both formulas treat Earth as a sphere. Neither gives the road distance, and neither makes an uncertain IP location precise.

What should I validate before using coordinates in a distance rule?

Check that both coordinates are present, numeric, and within valid latitude and longitude ranges, then convert degrees to radians before applying the formula. Treat missing or invalid coordinates as a distinct outcome rather than as zero or a nearby location. Also constrain the value passed to arccos to its valid range, since rounding can push it slightly outside.

How should I test a distance rule before releasing it?

Test known coordinate pairs, points on opposite sides of the cutoff, identical points, and missing or out of range values. Compare calculated distances with an independent reference, then replay labelled transactions to see how often the rule would flag legitimate customers and suspected fraud. Keep the test cases with the decision table or rule set so later policy changes can be checked against them.

Decisimo decision engine

Put these checks into a decision flow

Create rules for credit, fraud or collections, then run them on live decisions.