Andi5986 commited on
Commit
123db68
1 Parent(s): 0880134

Upload 2 files

Browse files
Files changed (2) hide show
  1. oracle.py +83 -0
  2. requirements.txt +52 -0
oracle.py ADDED
@@ -0,0 +1,83 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import requests
3
+ import pandas as pd
4
+ from sklearn.linear_model import LinearRegression
5
+ import random
6
+ import matplotlib.pyplot as plt
7
+ import numpy as np
8
+
9
+ st.title('Oracle Function Simulation')
10
+
11
+ # Oracle function
12
+ def oracle(task_complexity, ether_price, active_users, solved_tasks, unsolved_tasks, user_kpis, service_level_agreements):
13
+ weights = [random.random() for _ in range(7)]
14
+ return (
15
+ weights[0] * task_complexity
16
+ + weights[1] * ether_price
17
+ + weights[2] * active_users
18
+ + weights[3] * solved_tasks
19
+ + weights[4] * unsolved_tasks
20
+ + weights[5] * user_kpis
21
+ + weights[6] * service_level_agreements
22
+ )
23
+
24
+ # Get historical data for Ether
25
+ url = "https://api.coingecko.com/api/v3/coins/ethereum/market_chart"
26
+ params = {"vs_currency": "usd", "days": "1095"} # 1095 days is approximately 3 years
27
+ response = requests.get(url, params=params)
28
+ data = response.json()
29
+
30
+ # Convert the price data to a Pandas DataFrame
31
+ df = pd.DataFrame(data['prices'], columns=['time', 'price'])
32
+ df['time'] = pd.to_datetime(df['time'], unit='ms')
33
+
34
+ # Generate mock data for the oracle function and simulate the last 3 years
35
+ oracle_outputs = []
36
+ variables = {'task_complexity': [], 'ether_price': [], 'active_users': [], 'solved_tasks': [], 'unsolved_tasks': [], 'user_kpis': [], 'service_level_agreements': []}
37
+ for _ in range(len(df)):
38
+ task_complexity = random.randint(1, 10)
39
+ active_users = random.randint(1, 10000)
40
+ solved_tasks = random.randint(1, 1000)
41
+ unsolved_tasks = random.randint(1, 1000)
42
+ user_kpis = random.uniform(0.1, 1)
43
+ service_level_agreements = random.uniform(0.1, 1)
44
+ ether_price = df.iloc[_]['price']
45
+ oracle_outputs.append(oracle(task_complexity, ether_price, active_users, solved_tasks, unsolved_tasks, user_kpis, service_level_agreements))
46
+ variables['task_complexity'].append(task_complexity)
47
+ variables['ether_price'].append(ether_price)
48
+ variables['active_users'].append(active_users)
49
+ variables['solved_tasks'].append(solved_tasks)
50
+ variables['unsolved_tasks'].append(unsolved_tasks)
51
+ variables['user_kpis'].append(user_kpis)
52
+ variables['service_level_agreements'].append(service_level_agreements)
53
+
54
+ # Train a linear regression model to adjust the oracle output based on Ether price
55
+ model = LinearRegression()
56
+ model.fit(df['price'].values.reshape(-1, 1), oracle_outputs)
57
+
58
+ # Resample the price data to monthly data and calculate average price for each month
59
+ df['oracle_output'] = oracle_outputs
60
+ df.set_index('time', inplace=True)
61
+ monthly_df = df.resample('M').mean()
62
+
63
+ # Predict the oracle output for each average monthly price
64
+ monthly_df['predicted_oracle_output'] = model.predict(monthly_df['price'].values.reshape(-1, 1))
65
+
66
+ # Display a line chart of the predicted oracle output and Ether price over time
67
+ st.subheader('Predicted Oracle Output and Ether Price Over Time')
68
+ st.line_chart(monthly_df[['predicted_oracle_output', 'price']])
69
+
70
+ # Display a scatter plot with linear relation between Predicted Oracle output and Ether price
71
+ st.subheader('Predicted Oracle output vs Ether price')
72
+ plt.figure(figsize=(8,6))
73
+ plt.scatter(monthly_df['predicted_oracle_output'], monthly_df['price'])
74
+ m, b = np.polyfit(monthly_df['predicted_oracle_output'], monthly_df['price'], 1)
75
+ plt.plot(monthly_df['predicted_oracle_output'], m*monthly_df['predicted_oracle_output'] + b, color='red')
76
+ plt.xlabel('Predicted Oracle Output')
77
+ plt.ylabel('Ether Price')
78
+ st.pyplot(plt)
79
+
80
+ # Display tables showing average values of the variables over time
81
+ st.subheader('Average Values of the Variables Over Time')
82
+ for var in variables:
83
+ st.write(f"{var}: {sum(variables[var])/len(variables[var])}")
requirements.txt ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ altair==5.0.1
2
+ attrs==23.1.0
3
+ blinker==1.6.2
4
+ cachetools==5.3.1
5
+ certifi==2023.5.7
6
+ charset-normalizer==3.1.0
7
+ click==8.1.3
8
+ contourpy==1.0.7
9
+ cycler==0.11.0
10
+ decorator==5.1.1
11
+ fonttools==4.39.4
12
+ gitdb==4.0.10
13
+ GitPython==3.1.31
14
+ idna==3.4
15
+ importlib-metadata==6.6.0
16
+ Jinja2==3.1.2
17
+ jsonschema==4.17.3
18
+ kiwisolver==1.4.4
19
+ markdown-it-py==2.2.0
20
+ MarkupSafe==2.1.3
21
+ matplotlib==3.7.1
22
+ mdurl==0.1.2
23
+ numpy==1.24.3
24
+ packaging==23.1
25
+ pandas==2.0.2
26
+ Pillow==9.5.0
27
+ protobuf==4.23.2
28
+ pyarrow==12.0.0
29
+ pydeck==0.8.1b0
30
+ Pygments==2.15.1
31
+ Pympler==1.0.1
32
+ pyparsing==3.0.9
33
+ pyrsistent==0.19.3
34
+ python-dateutil==2.8.2
35
+ pytz==2023.3
36
+ pytz-deprecation-shim==0.1.0.post0
37
+ requests==2.31.0
38
+ rich==13.4.1
39
+ six==1.16.0
40
+ sklearn==0.0.post5
41
+ smmap==5.0.0
42
+ streamlit==1.23.1
43
+ tenacity==8.2.2
44
+ toml==0.10.2
45
+ toolz==0.12.0
46
+ tornado==6.3.2
47
+ typing_extensions==4.6.3
48
+ tzdata==2023.3
49
+ tzlocal==4.3
50
+ urllib3==2.0.3
51
+ validators==0.20.0
52
+ zipp==3.15.0