<jupyter_start><jupyter_text>IMPORT &SETUP<jupyter_code>import math
import numpy as np
import pandas as pd
from sklearn.model_selection import GridSearchCV, cross_val_score, cross_val_predict, StratifiedKFold
from sklearn.linear_model import LogisticRegressionCV
from sklearn.naive_bayes import GaussianNB
from sklearn import preprocessing, metrics, svm, ensemble
from sklearn.metrics import accuracy_score, classification_report
import tabpy_client
import tabpy
import tabpy_server

#Google sheets -------------------------------------------------------
import gspread
from oauth2client.service_account import ServiceAccountCredentials
# pip install statsmodels<jupyter_output><empty_output><jupyter_text>Set up Google Sheets<jupyter_code>scope = ["https://spreadsheets.google.com/feeds",'https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("personal-finance-aug30-5f28b13e4879.json", scope)
sheetsclient = gspread.authorize(creds)

workbook = sheetsclient.open("personal_finance_test")  # Open the workbook<jupyter_output><empty_output><jupyter_text>Read Data from Google Sheets<jupyter_code>sheet = sheetsclient.open("personal_finance_test").worksheet('portfolio_returns')  # Open the spreadhseet master

#Loop to create dictionary for mapping investments
df = pd.DataFrame(sheet.get_all_records())  # Get a list of all records
df = df.set_index("Date")
# ARIMA example
from statsmodels.tsa.arima_model import ARIMA
# contrived dataset
# data = [x + random() for x in range(1, 100)]
# fit model
model = ARIMA(df['FXAIX'], order=(3, 1, 1))
model_fit = model.fit(disp=False)
print(model_fit.summary())
# multi-step out-of-sample forecast
forecast = model_fit.forecast(steps=365)[0]
[*forecast]
# Use LabelEncoder to convert textual classifications to numeric. 
# We will use the same encoder later to convert them back.
encoder = preprocessing.LabelEncoder()
df['Class'] = encoder.fit_transform(df['Class'])
def ARIMA(fund):
    from statsmodels.tsa.arima_model import ARIMA
    model = ARIMA(df[fund], order=(3, 1, 1))
    model_fit = model.fit(disp=False)
#     print(model_fit.summary())
    forecast = model_fit.forecast(steps=30)[0]
    return [*forecast]
# Connect to TabPy server using the client library
connection = tabpy_client.Client('http://localhost:9004/')
connection.deploy('ARIMADemo',ARIMA,'Returns ARIMA forecast for next 30 days',override=True)
def add(x,y):
    import numpy as np
    return np.add(x, y).tolist()

client.deploy('add', add, 'Adds two numbers x and y',override = True)
from tabpy.tabpy_tools.client import Client
# import tabpy-tools
# import tabpy
client = Client('http://localhost:9004/')
lst = [1,2,3]
# float(lst)

list(map(lambda x:x*x,lst))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13017543859649122, 'fraction_numerical': 0.014736842105263158, 'mean_word_length': 5.724056603773585, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 4, 'lorem ipsum': 0, 'www.': 3, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '136101', 'license': 'MIT', 'n_tokens_mistral': 947, 'n_tokens_neox': 906, 'n_words': 218, 'path': 'model_script.ipynb', 'repo_name': 'rydernguyen/finance_401k', 'size': '2850'}