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

Published on: 2024-08-10 18:29:56

The Spherical Law of Cosines is a practical method for making decisions from geo coordinates. In this article, we show how to implement it in a decision engine. In a few steps, you can use the Spherical Law of Cosines 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 is a mathematical formula for calculating the distance between two points on the surface of a sphere from their latitude and longitude. It is useful for distance calculations in a decision engine when you work with geo coordinates.

Decisimo decision engine

Try our decision engine.

Spherical Law of Cosines vs. Haversine: Comparing Distance Formulas on a Sphere

The "Spherical Law of Cosines" formula is less accurate than the Haversine formula for calculating distances on a sphere, especially over larger distances. It is still often used because it is simpler to implement and does not require the atan2 function. If you need higher accuracy for distance calculation, 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

Here, distance is the distance between the two points, φ1 and φ2 are the latitudes, λ1 and λ2 are the longitudes, and R is the radius of the sphere, in this case the Earth. You can use any Earth radius value, but a common choice is 6371 km, or 6371000 meters.

Implementing the Spherical Law of Cosines in a Decision Engine

To implement the Spherical Law of Cosines in a decision engine, the platform must support functions such as arccos, sin, and cos. Decisimo is one example. If you use a different decision engine, check that it supports the required 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 the geo coordinates to radians. This is required because the Spherical Law of Cosines formula expects radians, not degrees. You can use the following formula to convert degrees to radians:
  • radians = degrees * (π / 180)
  • Define a function to run the calculation. The function should take the required variables as input and return the distance between the two points as output. Here is an example of what the function might look like:
    Spherical law of cosines in a decision engine
  • In the function, specify the variables that will be replaced by values from the data object during execution of the decision flow. Our example uses a data object with the fields "$.point_a.latitude", "$.point_a.longitude", "$.point_b.latitude", and "$.point_b.longitude".
  • Use the function in your decision flow where needed to calculate the distance between two points. For example, you might use it to check whether the distance between two points is greater than a threshold, or to select the point closest to a third point.

Using Distance Calculation for Customer Verification

Calculating the distance between two points can help verify a customer's position in several contexts.

For example, you could compare the geo-coordinates of a customer's mobile phone with the distance from a lending point of sale to confirm that the customer is physically present at that location. This can be used for geo-fencing, allowing financial services only within a defined area and rejecting loan applications from outside it.

Using Distance Calculation for Fraud Detection in Ecommerce

In ecommerce, calculating the distance between a customer's address and a location derived from technical proxy data such as an IP address, or other geolocation proxy data, can help identify fraudulent activity.

For example, if a customer's IP address shows they are in one country, but the distance between their reported address and the location indicated by that IP address is much greater than expected, this 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.

Decisimo decision engine

Try our decision engine.