|
import streamlit as st |
|
import requests |
|
import pandas as pd |
|
from sklearn.linear_model import LinearRegression |
|
import random |
|
import matplotlib.pyplot as plt |
|
import numpy as np |
|
|
|
st.title('Oracle Function Simulation') |
|
|
|
|
|
def oracle(task_complexity, ether_price, active_users, solved_tasks, unsolved_tasks, user_kpis, service_level_agreements): |
|
weights = [random.random() for _ in range(7)] |
|
return ( |
|
weights[0] * task_complexity |
|
+ weights[1] * ether_price |
|
+ weights[2] * active_users |
|
+ weights[3] * solved_tasks |
|
+ weights[4] * unsolved_tasks |
|
+ weights[5] * user_kpis |
|
+ weights[6] * service_level_agreements |
|
) |
|
|
|
|
|
url = "https://api.coingecko.com/api/v3/coins/ethereum/market_chart" |
|
params = {"vs_currency": "usd", "days": "1095"} |
|
response = requests.get(url, params=params) |
|
data = response.json() |
|
|
|
|
|
df = pd.DataFrame(data['prices'], columns=['time', 'price']) |
|
df['time'] = pd.to_datetime(df['time'], unit='ms') |
|
|
|
|
|
oracle_outputs = [] |
|
variables = {'task_complexity': [], 'ether_price': [], 'active_users': [], 'solved_tasks': [], 'unsolved_tasks': [], 'user_kpis': [], 'service_level_agreements': []} |
|
for _ in range(len(df)): |
|
task_complexity = random.randint(1, 10) |
|
active_users = random.randint(1, 10000) |
|
solved_tasks = random.randint(1, 1000) |
|
unsolved_tasks = random.randint(1, 1000) |
|
user_kpis = random.uniform(0.1, 1) |
|
service_level_agreements = random.uniform(0.1, 1) |
|
ether_price = df.iloc[_]['price'] |
|
oracle_outputs.append(oracle(task_complexity, ether_price, active_users, solved_tasks, unsolved_tasks, user_kpis, service_level_agreements)) |
|
variables['task_complexity'].append(task_complexity) |
|
variables['ether_price'].append(ether_price) |
|
variables['active_users'].append(active_users) |
|
variables['solved_tasks'].append(solved_tasks) |
|
variables['unsolved_tasks'].append(unsolved_tasks) |
|
variables['user_kpis'].append(user_kpis) |
|
variables['service_level_agreements'].append(service_level_agreements) |
|
|
|
|
|
model = LinearRegression() |
|
model.fit(df['price'].values.reshape(-1, 1), oracle_outputs) |
|
|
|
|
|
df['oracle_output'] = oracle_outputs |
|
df.set_index('time', inplace=True) |
|
monthly_df = df.resample('M').mean() |
|
|
|
|
|
monthly_df['predicted_oracle_output'] = model.predict(monthly_df['price'].values.reshape(-1, 1)) |
|
|
|
|
|
st.subheader('Predicted Oracle Output and Ether Price Over Time') |
|
st.line_chart(monthly_df[['predicted_oracle_output', 'price']]) |
|
|
|
|
|
st.subheader('Predicted Oracle output vs Ether price') |
|
plt.figure(figsize=(8,6)) |
|
plt.scatter(monthly_df['predicted_oracle_output'], monthly_df['price']) |
|
m, b = np.polyfit(monthly_df['predicted_oracle_output'], monthly_df['price'], 1) |
|
plt.plot(monthly_df['predicted_oracle_output'], m*monthly_df['predicted_oracle_output'] + b, color='red') |
|
plt.xlabel('Predicted Oracle Output') |
|
plt.ylabel('Ether Price') |
|
st.pyplot(plt) |
|
|
|
|
|
st.subheader('Average Values of the Variables Over Time') |
|
for var in variables: |
|
st.write(f"{var}: {sum(variables[var])/len(variables[var])}") |
|
|
|
|