A simple library to calculate price elasticity, cross elasticity
Project description
Retail Stats
This repository contains code to calculate various values used in retail for products whose sales and prices are provided.
Metrics currently available:
- Price Elasticity (In Progress)
- Cross Elasticity (Complete)
These simple, almost naive, implementations are taken from the Wikipedia definitions of the metrics. All performance benefits are from the work of Numpy. The purpose of this repository is to provide some convenience functions.
Installation
Install from PyPi.
pip install retail-stats
If installing outside of a [virtualenv]()
then use --user
to avoid permission
issues:
pip install --user retail-stats
Dependencies
numpy~=1.19
Calculations
Cross Elasticity
From Wikipedia,
measures the responsiveness of the quantity demanded for a good to a change in the price of another good, ceteris paribus.
This can be seen written using the formula:
Percentage Change in Quantity Sold of Product B
-------------------------------------------------
Percentage Change in Price Charged for Product A
The implementation is a direct copy of the formula.
from retail_stats.elasticity import calculate_cross_elasticity
Calculate Cross Elasticity for a single pair of products
from math import isclose
from retail_stats import elasticity
original_quantity = 200
new_quantity = 400
original_price = 1000
new_price = 1050
# (200 / 300) / (50 / 1025)
expected_ced = 13.66666666666666
ced = elasticity.calculate_cross_elasticity(original_quantity,
new_quantity,
original_price,
new_price)
assert isclose(expected_ced, ced)
Calculate All Cross Elasticities
from math import isclose
import numpy as np
from retail_stats.elasticity import get_all_cross_elasticities
skus = np.array(list("ABCD"))
# [original, new]
qty_a = [200, 0]
qty_b = [200, 400]
prc_a = [1000, 1050]
prc_b = [1000, 1000]
qty_c = [1000, 1050]
qty_d = [1000, 1100]
prc_c = [100, 80]
prc_d = [80, 80]
original_quantities = [qty_a[0], qty_b[0], qty_c[0], qty_d[0]]
new_quantities = [qty_a[1], qty_b[1], qty_c[1], qty_d[1]]
original_prices = [prc_a[0], prc_b[0], prc_c[0], prc_d[0]]
new_prices = [prc_a[1], prc_b[1], prc_c[1], prc_d[1]]
"""
Cross Elasticities between pairs A,B and C,D
| A | B | C | D
A | | | |
B | | | |
C | | | |
D | | | |
"""
ceds = get_all_cross_elasticities(original_quantities=original_quantities,
new_quantities=new_quantities,
original_prices=original_prices,
new_prices=new_prices)
assert ceds.shape == (len(skus), len(skus))
assert isclose(ceds[np.argwhere(skus == "A"), np.argwhere(skus == "A")], -41)
assert isclose(ceds[np.argwhere(skus == "B"), np.argwhere(skus == "A")], 13.66666666666666)
assert isclose(ceds[np.argwhere(skus == "D"), np.argwhere(skus == "C")], -0.4285714286)
assert isclose(ceds[np.argwhere(skus == "C"), np.argwhere(skus == "A")], 1)
assert isclose(ceds[np.argwhere(skus == "A"), np.argwhere(skus == "C")], 9)
Performance
Core elasticity function
Number of Products | Time in Seconds |
---|---|
1,000 | 0.065512 |
10,000 | 0.200022 |
100,000 | 1.727269 |
1,000,000 | 26.730988 |
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for retail_stats-0.0.2.post1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f1e6c3e9a42b6afb386ab15b854b441053d50c7a5302170839370ec5a342252c |
|
MD5 | 31b7d0ba544afb091db3eae179140d52 |
|
BLAKE2b-256 | 4bd886dc38f1d77f39f268d1660e9ce3077ba6cd6b53cd50b5077f031567dce5 |