Skip to content

Instantly share code, notes, and snippets.

@Tomen
Created July 8, 2023 13:58
Show Gist options
  • Save Tomen/96cdd991065cc75260948683c9fa0e13 to your computer and use it in GitHub Desktop.
Save Tomen/96cdd991065cc75260948683c9fa0e13 to your computer and use it in GitHub Desktop.
Dynamic Coretime Pricing
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"provenance": []
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "WpIbCa7V-Vim"
},
"outputs": [],
"source": [
"pip install matplotlib numpy\n"
]
},
{
"cell_type": "code",
"source": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n"
],
"metadata": {
"id": "iOcvNusr-Wwr"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# Constants\n",
"MIN_PRICE = 100 # the minimum price for a unit\n",
"MAX_PRICE_INCREASE_FACTOR = 2 # the factor to which the price may increase maximally\n",
"\n",
"BULK_TARGET = 30 # if cores_sold is BULK_TARGET, the price should not change. cores_sold below BULK_TARGET shall lead to a price decrease. cores_sold above BULK_TARGET shall lead to a price increase\n",
"BULK_LIMIT = 45 # if cores_sold reaches BULK_LIMIT, the price increases to MAX_PRICE_INCREASE_FACTOR\n",
"\n",
"# Scaling parameters\n",
"SCALE_DOWN = 2\n",
"SCALE_UP = 2\n",
"\n",
"old_price = 1000\n",
"\n",
"\n",
"def plot_with_function(f, title):\n",
" # Generate a list of units sold from 0 to BULK_LIMIT\n",
" cores_sold = np.linspace(0, BULK_LIMIT, BULK_LIMIT+1)\n",
"\n",
" # Calculate prices\n",
" prices = np.array([f(old_price, x) for x in cores_sold])\n",
"\n",
" # Plot the results\n",
" plt.figure(figsize=(10, 6))\n",
" plt.plot(cores_sold, prices)\n",
" plt.axvline(BULK_TARGET, color='r', linestyle='--')\n",
" plt.title(title)\n",
" plt.xlabel('Units Sold')\n",
" plt.ylabel('Price')\n",
" plt.grid(True)\n",
" plt.show()\n",
"\n",
" return cores_sold, prices\n"
],
"metadata": {
"id": "6IV6g3a3G5kp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# linear\n",
"def adjust_price_based_on_demand_linear (old_price, cores_sold):\n",
" if cores_sold < BULK_TARGET:\n",
" return old_price / 2 + old_price / 2 * (cores_sold / BULK_TARGET)\n",
" else:\n",
" return old_price + old_price / 2 * ((cores_sold - BULK_TARGET) / (BULK_LIMIT - BULK_TARGET))\n",
"\n",
"\n",
"cores_sold, prices = plot_with_function(adjust_price_based_on_demand_linear, \"Linear function\")"
],
"metadata": {
"id": "szrS0TAALks9"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"cores_sold, prices"
],
"metadata": {
"id": "cNHnbBJCpAOm"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"BULK_TARGET = 30\n",
"BULK_LIMIT = 45\n",
"MIN_PRICE = 1\n",
"MAX_PRICE_INCREASE_FACTOR = 2\n",
"SCALE_DOWN = 2\n",
"SCALE_UP = 2\n",
"old_price = 1000\n",
"\n",
"# power function\n",
"def adjust_price_based_on_demand_power(old_price, cores_sold):\n",
" if cores_sold <= BULK_TARGET:\n",
" return (old_price - MIN_PRICE) * (1 - (abs(cores_sold - BULK_TARGET)**SCALE_DOWN / BULK_TARGET**SCALE_DOWN)) + MIN_PRICE\n",
" else:\n",
" return ((MAX_PRICE_INCREASE_FACTOR - 1) * old_price * ((cores_sold - BULK_TARGET)**SCALE_UP / (BULK_LIMIT - BULK_TARGET)**SCALE_UP)) + old_price\n",
"\n",
"\n",
"\n",
"plot_with_function(adjust_price_based_on_demand_power, \"Power function with elongated platou\")"
],
"metadata": {
"id": "W5MnstzmKGvQ"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"cores_sold, prices"
],
"metadata": {
"id": "BPcuGdhcqNLR"
},
"execution_count": null,
"outputs": []
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment