<jupyter_start><jupyter_text>Artificial Intelligence in Finance  Superintelligence<jupyter_code>import gym
import numpy as np
import pandas as pd
np.random.seed(100)
env = gym.make('CartPole-v0')
env.seed(100)
action_size = env.action_space.n
action_size
[env.action_space.sample() for _ in range(10)]
state_size = env.observation_space.shape[0]
state_size
state = env.reset()
state  # [cart position, cart velocity, pole angle, pole angular velocity]
state, reward, done, _ = env.step(env.action_space.sample())
state, reward, done, _
%%time
data = pd.DataFrame()
state = env.reset()
length = []
for run in range(25000):
    done = False
    prev_state = env.reset()
    treward = 1
    results = []
    while not done:
        action = env.action_space.sample()
        state, reward, done, _ = env.step(action)
        results.append({'s1': prev_state[0], 's2': prev_state[1],
                        's3': prev_state[2], 's4': prev_state[3],
                        'a': action, 'r': reward})
        treward += reward if not done else 0
        prev_state = state
    if treward >= 110:
        data = data.append(pd.DataFrame(results))
        length.append(treward)
np.array(length).mean()
data.info()
data.tail()
from pylab import plt
plt.style.use('seaborn')
%matplotlib inline
import tensorflow as tf
tf.random.set_seed(100)
from keras.layers import Dense
from keras.models import Sequential
model = Sequential()
model.add(Dense(64, activation='relu',
                input_dim=env.observation_space.shape[0]))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy',
              optimizer='adam',
              metrics=['acc'])
%%time
model.fit(data[['s1', 's2', 's3', 's4']], data['a'],
          epochs=25, verbose=False, validation_split=0.2)
res = pd.DataFrame(model.history.history)
res.tail(3)
res.plot(figsize=(10, 6), style='--');
def epoch():
    done = False
    state = env.reset()
    treward = 1
    while not done:
        action = np.where(model.predict(np.atleast_2d(state))[0][0] > 0.5, 1, 0)
        state, reward, done, _ = env.step(action)
        treward += reward if not done else 0
    return treward
epoch()
res = np.array([epoch() for _ in range(100)])
res
res.mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14544650751547303, 'fraction_numerical': 0.026083112290008842, 'mean_word_length': 3.4635108481262327, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '641415', 'license': 'MIT', 'n_tokens_mistral': 799, 'n_tokens_neox': 765, 'n_words': 185, 'path': '02_superintelligence.ipynb', 'repo_name': 'pepelawycliffe/AI_in_Finance', 'size': '2262'}