Last active
August 22, 2024 14:59
-
-
Save bemxio/583e15fb72e00b7314b3bed691bf66d0 to your computer and use it in GitHub Desktop.
A Python script for simulating a currency based on a couple of parameters. Requires `numpy` and `pandas` to be installed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
""" | |
This is free and unencumbered software released into the public domain. | |
Anyone is free to copy, modify, publish, use, compile, sell, or | |
distribute this software, either in source code form or as a compiled | |
binary, for any purpose, commercial or non-commercial, and by any | |
means. | |
In jurisdictions that recognize copyright laws, the author or authors | |
of this software dedicate any and all copyright interest in the | |
software to the public domain. We make this dedication for the benefit | |
of the public at large and to the detriment of our heirs and | |
successors. We intend this dedication to be an overt act of | |
relinquishment in perpetuity of all present and future rights to this | |
software under copyright law. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | |
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | |
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
For more information, please refer to <https://unlicense.org> | |
""" | |
import numpy as np | |
import pandas as pd | |
# parameters | |
percentage_formatter = lambda n: f"{n * 100:.2f}%" if n else "" | |
initial_price = 1.00 | |
initial_supply = 3600 | |
sensitivity = 0.1 | |
damping_factor = 1.0 | |
buffer_factor = 0.1 | |
buffer_size = 0.05 | |
hours = 24 * 365 | |
# arrays | |
np.random.seed(42) | |
prices = np.zeros(hours) | |
supplies = np.zeros(hours) | |
demands = np.random.normal(initial_supply, initial_supply * 0.5, hours) | |
daily_inflation = np.zeros(hours // 24) | |
monthly_inflation = np.zeros(hours // 24) | |
# initial values | |
prices[0] = initial_price | |
supplies[0] = initial_supply | |
# main simulation loop | |
for t in range(1, hours): | |
# ensure demand is positive | |
demands[t] = max(0, demands[t]) | |
# update the supply and price | |
ratio = demands[t] / supplies[t - 1] | |
prices[t] = prices[t - 1] * (1 + sensitivity * (ratio - 1)) | |
supplies[t] = max(0, supplies[t - 1] - demands[t]) | |
# calculate the inflation percentage | |
if t % 24 == 0: | |
current_cost = 1 / prices[t] | |
previous_cost = 1 / prices[t - 24] | |
daily_inflation[t // 24] = (current_cost - previous_cost) / previous_cost | |
if t % (24 * 30) == 0: | |
current_cost = 1 / prices[t] | |
previous_cost = 1 / prices[t - (24 * 30)] | |
monthly_inflation[t // 24] = (current_cost - previous_cost) / previous_cost | |
# refill the supply | |
supplies[t] += initial_supply * prices[t] * damping_factor | |
if prices[t] > initial_price + buffer_factor: | |
supplies[t] += initial_supply * buffer_size | |
elif prices[t] < initial_price - buffer_factor: | |
supplies[t] -= initial_supply * buffer_size | |
# create a DataFrame | |
pd.set_option("display.float_format", "{:.4f}".format) | |
df = pd.DataFrame({ | |
"Hour": np.arange(hours), | |
"Price (USD)": prices, | |
"Cost of $1": 1 / prices, | |
"Supply": supplies, | |
"Demand": demands | |
}) | |
df2 = pd.DataFrame({ | |
"Day": np.arange(hours // 24), | |
"Daily Inflation (%)": daily_inflation, | |
"Monthly Inflation (%)": monthly_inflation | |
}) | |
df.to_html( | |
"rundown.html", | |
justify="center", | |
index=False | |
) | |
df2.to_html( | |
"inflation.html", | |
formatters={ | |
"Daily Inflation (%)": percentage_formatter, | |
"Monthly Inflation (%)": percentage_formatter | |
}, | |
justify="center", | |
index=False | |
) | |
print(df, df2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment