# Leveraging Distance Calculation with the Spherical Law of Cosines to Detect Fraud in Ecommerce

The Spherical Law of Cosines is a powerful tool for making informed decisions based on geo coordinates. In this article, we will show you how to easily implement it in a decision engine. With just a few steps, you can use the Spherical Law of Cosines for customer verification, geo-fencing, and fraud detection in ecommerce. Don't miss out on this essential tool for making better decisions. Follow our guide to get started now.

## Introduction to the Spherical Law of Cosines

The Spherical Law of Cosines is a mathematical formula that allows you to determine the distance between two points on the surface of a sphere based on the latitude and longitude of those points. It is a useful tool for calculating distances in a decision engine when working with geo coordinates.

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

Note that the "Spherical Law of Cosines" formula is less accurate than the Haversine formula for calculating distances on a sphere, especially for larger distances. However, it is often used because it is simpler to implement and does not require the use of the **atan2** function. If you need a more accurate distance calculation, you should 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
```

Where distance is the distance between the two points, φ1 and φ2 are the latitudes of the points, λ1 and λ2 are the longitudes of the points, and R is the radius of the sphere - in our case the Earth. You can use any value for the radius of the Earth, but a common value 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, you will need to ensure that the engine has the ability to perform functions such as arccos, sin, and cos. One example of a decision engine that can perform these functions is Decisimo. If you are using a different decision engine, you should check to see if it has the necessary mathematical capabilities before proceeding with 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:

- Transform the geo coordinates into radians. This is necessary because the Spherical Law of Cosines formula expects the coordinates to be in radians, not degrees. You can use the following formulas to convert degrees to radians:
- radians = degrees * (π / 180)
- Define a function to perform the calculation. This function should take in the necessary variables as input and return the distance between the two points as output. Here is an example of what the function might look like:
- In the function, specify the variables that will be replaced by values from the data object during the execution of the decision flow.
Our example works with data object that has fields "
*$.point_a.latitude*", "*$.point_a.longitude*", "*$.point_b.latitude*", and "*$.point_b.longitude*". - Use the function in your decision flow as needed to calculate the distance between two points. For example, you might use it to determine if the distance between two points is greater than a certain threshold or to select the point that is closest to a third point.

## Using Distance Calculation for Customer Verification

Calculating the distance between two points can be useful for verifying the position of a customer in a variety of contexts.

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

## Using Distance Calculation for Fraud Detection in Ecommerce

In ecommerce, calculating the distance between a customer's address and a location based on technical proxy data such as IP address or other potential geolocation proxy data can be useful for identifying fraudulent activity.

For example, if a customer's IP address indicates that they are located in one country, but the distance between their reported address and the location indicated by their IP address is much greater than expected, it could be a sign that the customer's true location is different from what they have reported. Using this information in conjunction with other fraud detection techniques can help to protect businesses from fraudulent transactions.