Spending patterns

Understand where people spend their money.

Overview

The spending pattern product uses artificial intelligence to understand an end user’s spending habit including what he spends on and how frequently he spends on them.

How it works

As with other Okra products, the spending pattern product starts with receiving permission from an end-user to access bank account transactions. These transactions are sent to the Okra Natural Language Understanding (NLU) where spending pattern/habit-related features are extracted.

The NLU splits spending habit features into two general categories:

Pattern:

Pattern features are data points and special keywords that indicate what a user spends on and how frequently he spends on it. These data points include Named Entities, Spending Keywords, and Transaction chunks. The features are sent to the Spending Pattern Engine that classifies the transaction into one (or more) of 19 different categories.

Channel:

Channel features are data points that indicate the method of spending. These are usually keywords that show whether a particular debit transaction was made through Transfer, PoS, Web purchase, ATM, or USSD. The features are also sent to the Spending Pattern Engine for further classification.

Spending patterns features:

The spending pattern features extracted from the NLU include:

  • Named Entities: Named Entities are special keywords and sub-texts that have been identified in the transaction description. These include transaction beneficiary, utility keywords, the reason for transaction, nuban, and spending habit keywords.
    Spending keywords are processed texts that indicate the item/object spent on. These include tithe, gifts, vacations, etc.
  • Transaction chunks: Chunks are transactions that are part of a larger transaction chain. The NLU identifies these chains and tracks them. Part payments for vacation and installment buying can be tracked like this.
  • Channel keywords: As described earlier, channel-related keywords indicate the method through which the transaction was made.

Model training

These features are sent to the spending pattern engine where the transaction is classified into one of the following categories:

Debt: These are debit transactions that are made to service debts such as loans or mortgages

Food: This groups transactions that were spent on food and food-related items. Restaurants, groceries, and take-outs belong to this category.

Entertainment: These are transactions that were spent on entertainment activities such as clubbing, beach, and cinema

Gambling: The Gambling category accounts for transactions spent on gambling activities such as lottery, betWay, etc.

Gifts: Gifts account for gifts such as Christmas gifts, birthday gifts, anniversary gifts, and valentine’s day gifts.

Donations: Donations are transactions that are of charitable nature. These include tithes, Sunday offerings, church pledges, and donations to charities.

Savings: Savings tracks keywords relating to investments and savings such as Piggyvest, Ajo, and trust funds.

Housing: This includes rent, mortgages, home decor, plumbing, gardening, etc.

Medical: Groups medical-related expenses such as hospital bills, medical diagnosis, and pharmacies.

Travel: Travel account for vacations, flight, and hotel reservations

Utilities: These are utility-based transactions such as internet subscriptions, cable TV, and power bills.

Others are:

Insurance: Groups insurance-related expenses and transactions

Transportation: Handles transportation expenses such as BRT subscription, Uber, and Bolt

Personal care: Transactions relating to self-care such as gym membership, spa treatment, and shopping

Taxes: Tax-related expenses

Pet: Pet-related expenses such as pet food and veterinary services

Subscriptions: Includes Netflix, IrokoTV, Spotify, Apple Music, Etc.

Family: Handles transactions relating to family expenses. These include school fees, toys, diapers, nursery, and allowance.

Evaluating spending pattern models

The spending pattern model combines two distinct machine learning models to classify transactions into a pattern and channel groups. A confidence score is used to measure the accuracy of these classifications. The confidence score is the number of correctly predicted transactions expressed as a percentage.

A simplified confusion matrix can be derived from the 19 different groups from the pattern model and the 4 different types of channels as shown below:

TP: True positive: Number of correctly predicted spending pattern transactions. This is the sum of all transactions correctly predicted as belonging to any of the 19 categories.

FN: False Negative: Total number of actual Spending Pattern transactions misclassified.

FP: False Positive: Total number of non-spending pattern transactions misclassified into any of the spending pattern categories.

TN: True Negative: Number of correctly predicted non-spending pattern transactions.

How to add it to your app

You get a breakdown of expenses per category and real-time insights based on expenses after monthly income.
The Spending Patterns product fetches the user’s banking data and produces categorized insights on their spending patterns via the user's consent to access their bank account in an application. Implementing the spending pattern feature on your project is simple.

To integrate,

  1. Visit here and signup.
  2. After verification, log in.
  3. Get your secret keys
  4. Test on API Reference

Visit the API Reference, and use this endpoint 👉🏻 https://api.okra.ng/v2/products/spending-patterns/process

  • Create a customer on your dash, and autogenerate a customer.
  • Grab the customer's Id.

Payload explanation

  • customer_id[Required] - customer ID to be used to compute spending pattern
  • interval[Optional] - determines the interval you want the spending pattern to be calculated. Default is monthly
  • channel_filter[Optional] - if you want to focus on a particular channel of spending.
    - Values: Transfer, USSD, Cash, Web_pos
  • start_date: if you want spending pattern calculation to start at a particular date
  • end_date: If you want the spending pattern to end at a particular date
curl -X POST https://api.okra.ng/v2/products/spending-patterns/process
-H 'Content-Type: application/json' 
-H 'Authorization: Bearer <AccessToken>'
-d '{
    "customer_id": "xxxxxxxxx",
    }'
{
    "data": {
                "confidence": 84.63,
        "breakdown": {
            "channel": {
                "Cash": {
                    "count": 40,
                    "max": 100800.0,
                    "min": 1000.0,
                    "percentage": 1.71,
                    "sum": 220116.12
                },
                "Transfer": {
                    "count": 118,
                    "max": 2516000.0,
                    "min": 0.02,
                    "percentage": 96.84,
                    "sum": 12485350.6
                },
                "web_pos": {
                    "count": 25,
                    "max": 40512.0,
                    "min": 1216.87,
                    "percentage": 1.04,
                    "sum": 133983.64
                }
            },
            "pattern": {
                "food": {
                    "count": 10,
                    "max": 40512.0,
                    "min": 2800.0,
                    "percentage": 0.97,
                    "sum": 125026.17
                },
                "housing": {
                    "count": 1,
                    "max": 1200000.0,
                    "min": 1200000.0,
                    "percentage": 9.31,
                    "sum": 1200000.0
                },
                "medical": {
                    "count": 1,
                    "max": 2100.0,
                    "min": 2100.0,
                    "percentage": 0.02,
                    "sum": 2100.0
                },
                .
                                .   
                                .
                "transportation": {
                    "count": 8,
                    "max": 11700.0,
                    "min": 1216.87,
                    "percentage": 0.2,
                    "sum": 25965.04
                }
            }
        },
        "summary": {
            "channel": {
                "max": 2516000.0,
                "min": 0.02,
                "sum": 12892506.71
            },
            "debit_count": 188,
            "last_year_total": 1386542.42,
            "last_year_total_minus_tax": 1386541.92,
            "pattern": {
                "max": 2516000.0,
                "min": 0.02,
                "sum": 12892506.71
            }
        },
            "streams": [
            {
                "average_amount_spent": 312916.8,
                "average_day_interval": 21.58,
                "channel": "Transfer",
                "count": 34,
                "pattern": null,
                "period": "bi-weekly"
            },
            {
                "average_amount_spent": 37518.75,
                "average_day_interval": 19,
                "channel": "Transfer",
                "count": 4,
                "pattern": "savings",
                "period": "weekly"
            }
        ],
    },
    "status": "success"
}

Response explanation:

The response object is grouped into two broad categories: channel and pattern.

Each channel and pattern has the following key-value pairs:

Count: This is the total of transactions that were classified into the channel or individual category

Max: This is the maximum sum of money that was spent on the channel or category

Min: This is the minimum lump sum of money that was spent on the channel or category

Percentage: This is the percentage of total expenses that this channel or category accounts for

Sum: This is the total sum of money that was spent on/through this category or channel

The pattern and channel response object come with a summary key which is an overall summary of the pattern categories and channels.

An interesting subsection of the spending pattern response is the streams object. This is a list of active expenses detected by the spending pattern engine. Each stream contains:

Average amount spent:

This is the average total amount spent on that stream of expense

Average day interval:

This is the average number of days between consecutive transactions in the stream

Channel: The major channel through which most transactions in the stream are made

Count: Number of transactions identified as part of the stream

Pattern: The identified pattern for the majority of the transactions

Period: How frequently this stream occurs.

Granular spending pattern

If you check the request object above, you would notice that there is no way to control the granularity of the spending patterns. The response object only gives the overall pattern groupings and channels of all the end-user transactions available to us.

To control how granular the spending pattern response will be, and to specify whether a subset of the user transactions should be used to calculate spending patterns, you can pass additional parameters to the request object such as start_date, end_date, interval, and channel_filter

curl -X POST https://api.okra.ng/v2/products/spending-patterns/process
-H 'Content-Type: application/json'
-H 'Authorization: Bearer <AccessToken>'
-d '{
"customer_id": "xxxxxxxxx",
"start_date": "YYYY-MM-DD",
"end_date": "YYYY-MM-DD",
"interval": "monthly",
"channel_filter": "Transfer",
}'
{
    "data": {
        "breakdown": {
            "channel": {
                "2021": {
                    "December": {
                        "Cash": {
                            "count": 3,
                            "date": "12/31/2021",
                            "max": 28400.0,
                            "min": 1000.0,
                            "pct_change": 0.0,
                            "sum": 34400.0
                        },
                        "Transfer": {
                            "count": 32,
                            "date": "12/31/2021",
                            "max": 500.0,
                            "min": 15.0,
                            "pct_change": 0.334,
                            "sum": 5707.5
                        },
                        "ussd": {
                            "count": 1,
                            "date": "12/31/2021",
                            "max": 3500.0,
                            "min": 3500.0,
                            "pct_change": 0.0,
                            "sum": 3500.0
                        },
                        "web_pos": {
                            "count": 1,
                            "date": "12/31/2021",
                            "max": 1900.0,
                            "min": 1900.0,
                            "pct_change": 0.0,
                            "sum": 1900.0
                        }
                    },
                    "November": {
                        "Cash": {
                            "count": 0,
                            "date": "11/30/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": -1.0,
                            "sum": 0.0
                        },
                        "Transfer": {
                            "count": 15,
                            "date": "11/30/2021",
                            "max": 2300.0,
                            "min": 15.0,
                            "pct_change": 22.187,
                            "sum": 4278.0
                        },
                        "ussd": {
                            "count": 0,
                            "date": "11/30/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": 0.0,
                            "sum": 0.0
                        },
                        "web_pos": {
                            "count": 0,
                            "date": "11/30/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": -1.0,
                            "sum": 0.0
                        }
                    }  
                                }
            },
            "pattern": {
                "2021": {
                    "December": {
                        "debt": {
                            "count": 0,
                            "date": "12/31/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": 0.0,
                            "sum": 0.0
                        },
                        "gambling": {
                            "count": 0,
                            "date": "12/31/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": 0.0,
                            "sum": 0.0
                        },
                        "miscellaneous": {
                            "count": 1,
                            "date": "12/31/2021",
                            "max": 52.5,
                            "min": 52.5,
                            "pct_change": -0.564,
                            "sum": 52.5
                        },
                        "taxes": {
                            "count": 0,
                            "date": "12/31/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": 0.0,
                            "sum": 0.0
                        },
                        "utilities": {
                            "count": 24,
                            "date": "12/31/2021",
                            "max": 500.0,
                            "min": 20.0,
                            "pct_change": 2.176,
                            "sum": 5400.0
                        }
                    },
                    "November": {
                        "debt": {
                            "count": 0,
                            "date": "11/30/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": 0.0,
                            "sum": 0.0
                        },
                        "gambling": {
                            "count": 0,
                            "date": "11/30/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": 0.0,
                            "sum": 0.0
                        },
                        "miscellaneous": {
                            "count": 2,
                            "date": "11/30/2021",
                            "max": 68.0,
                            "min": 52.5,
                            "pct_change": -0.974,
                            "sum": 120.5
                        },
                        "taxes": {
                            "count": 0,
                            "date": "11/30/2021",
                            "max": 0.0,
                            "min": 0.0,
                            "pct_change": 0.0,
                            "sum": 0.0
                        },
                        "utilities": {
                            "count": 4,
                            "date": "11/30/2021",
                            "max": 600.0,
                            "min": 100.0,
                            "pct_change": 16.0,
                            "sum": 1700.0
                        }
                    }
                }
            }
        },
        "confidence": 87.2,
        "streams": [
            {
                "average_amount_spent": 19333.33,
                "average_frequency": 0.0,
                "channel": "Cash",
                "count": 3,
                "pattern": "miscellaneous",
                "period": "daily"
            },
            {
                "average_amount_spent": 11577.67,
                "average_frequency": 4.36,
                "channel": "web_pos",
                "count": 34,
                "pattern": null,
                "period": "weekly"
            },
        ],
        "summary": {
            "channel": {
                "max": 51000.0,
                "min": 4.0,
                "sum": 1916746.74
            },
            "debit_count": 718,
            "last_year_total": 288159.39,
            "last_year_total_minus_tax": 288046.86,
            "pattern": {
                "max": 51000.0,
                "min": 4.0,
                "sum": 1916746.74
            }
        }
    },
    "status": "success"
}

Request explanation

Start Date: The date you want the spending pattern calculation to start from

End Date: The date you want the spending pattern calculation to end

Interval: Interval controls how detailed you want the spending pattern response to be. You can specify monthly, quarterly, or yearly intervals. The spending pattern response object will become more granular breaking the pattern and channels further into the specified interval.

Channel Filter: Channel filter indicates which spending channel to focus on. For instance, you may want to know spending habits just on the USSD channel.

📘

Not a developer?

You can turn on Spending Patterns by simply toggling a switch within the Add-on tab or the inner Transactions page of any of your customers.

Each pattern and channel has been broken down into year and month. As with the normal spending pattern response, each channel and pattern has count, max, min, and sum. However, we also have date which is the last processed date for the pattern or channel, and pct_change which measures the month-on-month (interval-on-interval) percentage change of the pattern or channel.


Did this page help you?