Esempio n. 1
0
 def __init__(self, df, categoricals=None):
     if categoricals is None:
         categoricals = []
     for col in set(df.columns) - set(categoricals):
         setattr(self, col, tt(df[col].values).double())
     for col in categoricals:
         setattr(self, col, tt(df[col].values).long())
Esempio n. 2
0
def format_data(df, categoricals=None):
    data = dict()
    if categoricals is None:
        categoricals = []
    for col in set(df.columns) - set(categoricals):
        data[col] = tt(df[col].values).double()
    for col in categoricals:
        data[col] = tt(df[col].values).long()
    return data
Esempio n. 3
0
def labeller(s):
    if s == tt(0):
        return ('phi', 'psi')
    elif s == tt(1):
        return ('phi', 'chi')
    elif s == tt(2):
        return ()
    elif s == tt(3):
        return ('psi', )
Esempio n. 4
0
def test_constraints():
    predictor = torch.randn(5)
    for cp in (
            tt([1, 2, 3, 4, 0]),
            tt([1, 2, 4, 3, 5]),
            tt([1, 2, 3, 4, 4]),
    ):
        with pytest.raises(ValueError):
            OrderedLogistic(predictor, cp)
Esempio n. 5
0
 def create_masks(self, n):
     
     masks = set()
     while len(masks) < n:
         m = np.random.randint(0, high=2, size=self.state_size)
         if m.any():
             masks.add(tuple(m))
     return [tt(m).float() for m in masks]
Esempio n. 6
0
def test_tensor_ops():
    pi = 3.141592654
    X = Uniform(0, 1).expand([5, 5]).rv
    a = tt([[1, 2, 3, 4, 5]])
    b = a.T
    X = abs(pi*(-X + a - 3*b))
    x = X.dist.sample()
    assert x.shape == (5, 5)
    assert (x >= 0).all().item()
Esempio n. 7
0
def transition(s, a):
    if s == tt(0):
        if a == ('a', ):
            return s
        else:
            return d0.sample()
    else:
        if a == ('a', ):
            return s
        else:
            return d0.sample()
Esempio n. 8
0
def labeller(s):
    if s == tt(0):
        return ()
    elif s == tt(1):
        return ('phi', )
    elif s == tt(2):
        return ('phi', 'chi')
    elif s == tt(3):
        return ()
    elif s == tt(4):
        return ()
    elif s == tt(5):
        return ('psi', )
    elif s == tt(6):
        return ('psi', )
    elif s == tt(7):
        return ()
    elif s == tt(8):
        return ('chi', 'psi')
    elif s == tt(9):
        return ('chi', )
Esempio n. 9
0
def transition(s, a):
    if s == tt(0):
        if a[0] == 'a':
            return d0.sample()
        elif a == ('b', 'x'):
            return d1.sample()
        else:
            return tt(1)
    elif s == tt(1):
        if a == ('a', 'x'):
            return d0.sample()
        elif a == ('b', 'x'):
            return d1.sample()
        elif a == ('a', 'y'):
            return d2.sample()
        else:
            return d4.sample()
    elif s == tt(2):
        if a[1] == 'y':
            return d2.sample()
        else:
            return d3.sample()
    elif s == tt(3):
        if a[0] == 'b':
            return d4.sample()
        else:
            return tt(3)
Esempio n. 10
0
def get_targets(rows):
    """
        This is where you can build your target.
        For the tutorial we're only concerned about whether we should buy or sell.
        So we'll create a rule for this, we'll need to set this up as regression ( on the open set (-1,1) ). Our rule is as follows:
        If the stock closes below the open, we should have sold (t-1 = sell at close, t = buy at close).
        If the stock closes above the open, we should have bought (t-1 = buy at close, t = sell at close).
        While under the constraint of maximizing our profit

        We can obviously make this more complex, even with the data we have, but for this tutorial, 


        If you want to create your own targets, this is where you should do it. Below is the accessible data structure passed to this function.

            rows = {
                    "timestamp": [
                        1557149400000,
                    ],
                    "open": [
                        126.38999938964844,
                    ],
                    "high": [
                        128.55999755859375,
                    ],
                    "low": [
                        126.11000061035156,
                    ],
                    "close": [
                        128.14999389648438,
                    ],
                    "volume": [
                        24239800,
                    ]
                }
    """

    

    # targets: sell = -1, buy = +1
    # set to sell at beginning of the trading day
    # we assume that unless the it's going down, buy.
    # later we'll add some business logic to determine the actual action of purchasing
    # return [ tt([0.]) ] + [ tt([ 0 if (rows['close'][i-2] > rows['open'][i-2]) and (rows['close'][i] >  rows['open'][i]) else (1 if random.random() > .7 else 2 )]) for i in range(2,len(rows['open'])) ]

    return [ tt( [ [ [ rows['high'][i] ] ] ] )  for i in range(1,len(rows['open'])) ]
Esempio n. 11
0
if torch.cuda.is_available():
    print("Using GPU!")
    device = torch.device("cuda")
    torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
    print("Using CPU!")
    device = torch.device("cpu")

### MARKOV GAME 3 ###

# Number of players
num_players = 5

# State space
state_space = [tt(i) for i in range(14)]

# Action spaces
action_spaces = [('a', 'b'), ('x', 'y'), ('p', 'q', 'r'), ('i', 'j'),
                 ('u', 'v')]

# Dists
d0 = Categorical(
    tt([0.1, 0.0, 0.0, 0.0, 0.0, 0.1, 0.2, 0.0, 0.0, 0.5, 0.0, 0.0, 0.1, 0.0]))
d1 = Categorical(
    tt([0.0, 0.0, 0.3, 0.0, 0.3, 0.0, 0.0, 0.0, 0.3, 0.0, 0.0, 0.0, 0.1, 0.0]))
d2 = Categorical(
    tt([0.0, 0.3, 0.0, 0.0, 0.0, 0.2, 0.1, 0.0, 0.0, 0.0, 0.1, 0.1, 0.1, 0.1]))
d3 = Categorical(
    tt([0.1, 0.1, 0.0, 0.2, 0.1, 0.1, 0.0, 0.1, 0.0, 0.1, 0.1, 0.0, 0.1, 0.0]))
d4 = Categorical(
Esempio n. 12
0
def transition(s, a):
    if s == tt(0):
        if a[0] == 'a' and a[1] == 'y':
            return d0.sample()
        elif a == ('b', 'y', 'p'):
            return d1.sample()
        elif a == ('b', 'y', 'q'):
            return d3.sample()
        else:
            return d6.sample()
    elif s == tt(1):
        if a[2] == 'r':
            return d4.sample()
        elif a == ('a', 'x', 'q'):
            return d2.sample()
        else:
            return tt(4)
    elif s == tt(2):
        if a == ('b', 'y', 'p'):
            return d5.sample()
        elif a == ('b', 'x', 'q'):
            return d1.sample()
        elif a == ('a', 'y', 'r'):
            return d2.sample()
        elif a == ('a', 'x', 'q'):
            return d6.sample()
        else:
            return d4.sample()
    elif s == tt(3):
        if a[1] == 'p':
            return d0.sample()
        elif a[1] == 'q':
            return d2.sample()
        else:
            return tt(1)
    elif s == tt(4):
        if a == ('b', 'y', 'p'):
            return d5.sample()
        elif a == ('b', 'x', 'q'):
            return d1.sample()
        elif a == ('a', 'y', 'r'):
            return d2.sample()
        else:
            return tt(4)
    elif s == tt(5):
        if a[0] == 'a':
            return d3.sample()
        else:
            return d6.sample()
    elif s == tt(6):
        if a == ('a', 'x', 'p'):
            return d4.sample()
        else:
            return tt(6)
    elif s == tt(7):
        if a[0] == 'x':
            return d1.sample()
        else:
            return d0.sample()
    elif s == tt(8):
        if a[1] == 'x' and a[2] == 'r':
            return d5.sample()
        elif a[1] == 'y' and a[2] == 'q':
            return d4.sample()
        else:
            return d3.sample()
    elif s == tt(9):
        if a[1] == 'y':
            return d1.sample()
        elif a == ('b', 'x', 'p'):
            return d2.sample()
        else:
            return tt(2)
Esempio n. 13
0
def trainModel(epoch):
    trainBar = tqdm(trainLoader)
    runningResults = {
        'batchSize': 0,
        'DLoss': 0,
        'GLoss': 0,
        'DScore': 0,
        'GScore': 0
    }

    netG.train()
    netD.train()

    for data, target in trainBar:
        batchSize = data.size(0)
        runningResults['batchSize'] += batchSize

        ################################################################################################################
        # (1) Update D network: maximize D(x)-1-D(G(z))
        ################################################################################################################
        fakeHRs = []
        fakeLRs = []
        fakeScrs = []
        realScrs = []
        DLoss = 0
        LRImgs = []
        HRImgs = []

        # Zero-out gradients, i.e., start afresh
        netD.zero_grad()

        netG.init_hidden(device)

        for LRImg, HRImg in zip(data, target):
            HRImg = HRImg.to(device)
            LRImg = LRImg.to(device)

            fakeHR, fakeLR = netG(LRImg)

            realOut = netD(HRImg).mean()
            fake_out = netD(fakeHR).mean()

            fakeHRs.append(fakeHR)
            fakeLRs.append(fakeLR)
            LRImgs.append(LRImg)
            HRImgs.append(HRImg)
            fakeScrs.append(fake_out)
            realScrs.append(realOut)

            DLoss += 1 - realOut + fake_out

        DLoss /= len(data)

        # Calculate gradients
        DLoss.backward(retain_graph=True)

        # Update weights
        optimizerD.step()

        ################################################################################################################
        # (2) Update G network: minimize 1-D(G(z)) + Perception Loss + Image Loss + TV Loss
        ################################################################################################################
        GLoss = 0

        # Zero-out gradients, i.e., start afresh
        netG.zero_grad()

        idx = 0
        for fakeHR, fakeLR, fake_scr, HRImg, LRImg in zip(
                fakeHRs, fakeLRs, fakeScrs, target, data):
            fakeHR = fakeHR.to(device)
            fakeLR = fakeLR.to(device)
            fake_scr = fake_scr.to(device)
            HRImg = HRImg.to(device)
            LRImg = LRImg.to(device)
            GLoss += generatorCriterion(fake_scr, fakeHR, HRImg, fakeLR, LRImg,
                                        idx)
            idx += 1

        GLoss /= len(data)

        # Calculate gradients
        GLoss.backward()

        # Update weights
        optimizerG.step()

        realOut = torch.Tensor(realScrs).mean()
        fake_out = torch.Tensor(fakeScrs).mean()
        runningResults['GLoss'] += GLoss.data.item() * batchSize
        runningResults['DLoss'] += DLoss.data.item() * batchSize
        runningResults['DScore'] += realOut.data.item() * batchSize
        runningResults['GScore'] += fake_out.data.item() * batchSize

        dloss = runningResults['DLoss'] / runningResults['batchSize']
        gloss = runningResults['GLoss'] / runningResults['batchSize']
        dscore = runningResults['DScore'] / runningResults['batchSize']
        gscore = runningResults['GScore'] / runningResults['batchSize']
        trainBar.set_description(
            desc=
            '[Epoch: %d/%d] D Loss: %.4f G Loss: %.4f D(x): %.4f D(G(z)): %.4f'
            % (epoch, NUM_EPOCHS, dloss, gloss, dscore, gscore))

        current_step = trainBar.n

        if current_step % visdom_iter == 0:
            lrimg = LRImgs[len(LRImgs) // 2].detach().cpu()
            hrimg = HRImgs[len(HRImgs) // 2].detach().cpu()
            srimg = fakeHRs[len(fakeHRs) // 2].detach().cpu()
            vis.images(torch.clamp(UP(lrimg), 0, 1),
                       opts=dict(title='LR'),
                       win=lr_win)
            vis.images(torch.clamp(srimg, 0, 1),
                       opts=dict(title='SR'),
                       win=sr_win)
            vis.images(torch.clamp(hrimg, 0, 1),
                       opts=dict(title='HR'),
                       win=hr_win)
            vis.line(X=tt([current_step]),
                     Y=tt([dloss]).cpu(),
                     win=dloss_win,
                     update='append',
                     opts=dloss_opts)
            vis.line(X=tt([current_step]),
                     Y=tt([gloss]).cpu(),
                     win=gloss_win,
                     update='append',
                     opts=gloss_opts)
            vis.line(X=tt([current_step]),
                     Y=tt([dscore]).cpu(),
                     win=dscore_win,
                     update='append',
                     opts=dscore_opts)
            vis.line(X=tt([current_step]),
                     Y=tt([gscore]).cpu(),
                     win=gscore_win,
                     update='append',
                     opts=gscore_opts)

        if current_step != 0 and current_step % save_iter == 0:
            saveModelParams(epoch, runningResults, iter=current_step)

        gc.collect()

    netG.eval()

    return runningResults
Esempio n. 14
0
            update_average(netG_test, netG, EMA)

    Giter += 1

    G_growth = output_F_mean_after - output_F_mean

    if Giter % verbose_freq == 0:
        if LAMBDA > 0:
            log_str = '[{:d}/{:d}][{:d}] | WD {:.9f} | real_mean {:.9f} | fake_mean {:.9f} | G_growth {:.9f} | ' \
                      'L2LossD_real {:.9f} | L2LossD_fake {:.9f} | ' \
                      'L2LossD {:.9f} | RegLossD {:.9f} | TotalLoss {:.9f}'.format(
                epoch, epochs, Giter,
                WD.item(), output_R_mean.item(), output_F_mean.item(), G_growth.item(),
                L2LossD_real.item(), L2LossD_fake.item(),
                L2LossD.item(), RegLossD.item(), TotalLoss.item())
            vis.line(X=tt([Giter]),
                     Y=tt([L2LossD_real.item()]).cpu(),
                     win=wL2LossD_real,
                     update='append',
                     opts=oL2LossD_real)
            vis.line(X=tt([Giter]),
                     Y=tt([L2LossD_fake.item()]).cpu(),
                     win=wL2LossD_fake,
                     update='append',
                     opts=oL2LossD_fake)
            vis.line(X=tt([Giter]),
                     Y=tt([L2LossD.item()]).cpu(),
                     win=wL2LossD,
                     update='append',
                     opts=oL2LossD)
            vis.line(X=tt([Giter]),
Esempio n. 15
0
if torch.cuda.is_available():
    print("Using GPU!")
    device = torch.device("cuda")
    torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
    print("Using CPU!")
    device = torch.device("cpu")

### MARKOV GAME 1 ###

# Number of players
num_players = 2

# State space
state_space = [tt(i) for i in range(5)]

# Action spaces
action_spaces = [('a', 'b'), ('x', 'y')]

# Dists
d0 = Categorical(tt([0.0, 0.3, 0.1, 0.6]))
d1 = Categorical(tt([0.9, 0.1, 0.0, 0.0]))
d2 = Categorical(tt([0.3, 0.1, 0.1, 0.5]))
d3 = Categorical(tt([0.0, 0.0, 1.0, 0.0]))
d4 = Categorical(tt([0.2, 0.2, 0.4, 0.2]))


# Initial state dist
def initial(states):
    return d3.sample()
Esempio n. 16
0
    torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
    print("Using CPU!")
    device = torch.device("cpu")

### MARKOV GAME 0 ###
# Note that this game (and its variants) is for debugging purposes and so is very simple

### MARKOV GAME 0.0 ###
# Single player, single spec

# Number of players
num_players = 1

# State space
state_space = [tt(i) for i in range(2)]

# Action spaces
action_spaces = [('a', 'b')]

# Dists
d0 = Categorical(tt([0.5, 0.5]))
d1 = Categorical(tt([0.7, 0.3]))


# Initial state dist
def initial(states):
    return d1.sample()


# Transition function
Esempio n. 17
0
def labeller(state):
    if state == tt(0):
        return ('phi', )
    else:
        return ('psi', )


### MARKOV GAME 0.1 ###
# Two players, single spec

# # Number of players
# num_players = 2

# # State space
# state_space = [tt(i) for i in range(2)]

# # Action spaces
# action_spaces = [('a','b'),('c','d')]

# # Dists
# d0 = Categorical(tt([ 0.5, 0.5 ]))
# d1 = Categorical(tt([ 0.7, 0.3 ]))

# # Initial state dist
# def initial(states):
#     return d1.sample()

# # Transition function
# def transition(s, a):
#     if s == tt(0):
#         if a == ('a','c'):
#             return s
#         else:
#             return d0.sample()
#     else:
#         if a == ('a','d'):
#             return s
#         else:
#             return d0.sample()

# # Labelling function
# def labeller(state):
#     if state == tt(0):
#         return ('phi',)
#     else:
#         return ('psi',)

### MARKOV GAME 0.2 ###
# Single player, two specs

# # Number of players
# num_players = 1

# # State space
# state_space = [tt(i) for i in range(2)]

# # Action spaces
# action_spaces = [('a','b')]

# # Dists
# d0 = Categorical(tt([ 0.5, 0.5 ]))
# d1 = Categorical(tt([ 0.7, 0.3 ]))

# # Initial state dist
# def initial(states):
#     return d1.sample()

# # Transition function
# def transition(s, a):
#     if s == tt(0):
#         if a == ('a',):
#             return s
#         else:
#             return d0.sample()
#     else:
#         if a == ('a',):
#             return s
#         else:
#             return d0.sample()

# # Labelling function
# def labeller(state):
#     if state == tt(0):
#         return ('phi',)
#     else:
#         return ('psi',)
Esempio n. 18
0
    if isinstance(inp,tuple):(inp,h) = inp
    return inp.cpu().data.numpy()


DEFAULT_SYMBOL = 'MSFT'
my_share = share.Share('MSFT')

config = json.load(open('config.json','r'))
order, nets = cfg2nets(config)
nets['DayTrader'].load_state_dict(torch.load('DayTrader.pt'))
while True:
    rows = my_share.get_historical(share.PERIOD_TYPE_DAY,
                                        1,
                                        share.FREQUENCY_TYPE_MINUTE,
                                        1)
    inp = tt([ [ [ rows['open'][-1],rows['close'][-1],rows['volume'][-1],rows['low'][-1],rows['high'][-1] ] ] ])
    y = predict(nets,order,inp)[0]
    action = decide( y )
    print(action, '@',rows['high'][-1],y)
    time.sleep(60)

# while True:



# parser = argparse.ArgumentParser()
# parser.add_argument('-u','--username',help='your Robinhood username',required=True)
# parser.add_argument('-p','--password',help='your Robinhood password',required=True)
# parser.add_argument('-t','--ticker',help='the ticker symbol you wish to monitor',default=DEFAULT_SYMBOL)
# args = parser.parse_args()
# #Setup
Esempio n. 19
0
if torch.cuda.is_available():
    print("Using GPU!")
    device = torch.device("cuda")
    torch.set_default_tensor_type('torch.cuda.FloatTensor')
else:
    print("Using CPU!")
    device = torch.device("cpu")

### MARKOV GAME 2 ###

# Number of players
num_players = 3

# State space
state_space = [tt(i) for i in range(10)]

# Action spaces
action_spaces = [('a', 'b'), ('x', 'y'), ('p', 'q', 'r')]

# Dists
d0 = Categorical(tt([0.1, 0.0, 0.1, 0.0, 0.3, 0.3, 0.0, 0.0, 0.0, 0.2]))
d1 = Categorical(tt([0.0, 0.2, 0.2, 0.4, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0]))
d2 = Categorical(tt([0.3, 0.0, 0.0, 0.0, 0.0, 0.0, 0.6, 0.0, 0.1, 0.0]))
d3 = Categorical(tt([0.0, 0.0, 0.4, 0.1, 0.1, 0.2, 0.0, 0.1, 0.0, 0.1]))
d4 = Categorical(tt([0.1, 0.1, 0.1, 0.1, 0.1, 0.0, 0.0, 0.0, 0.0, 0.5]))
d5 = Categorical(tt([0.2, 0.2, 0.0, 0.0, 0.3, 0.1, 0.2, 0.0, 0.2, 0.0]))
d6 = Categorical(tt([0.0, 0.0, 0.7, 0.0, 0.0, 0.0, 0.0, 0.2, 0.0, 0.0]))


# Initial state dist
Esempio n. 20
0
    def create_transitions(self):

        possible_actions = [[]]
        for action_size in self.action_sizes:
            new_possible_actions = utils.flatten([[p_a + [a] for p_a in possible_actions] for a in range(action_size)])
            possible_actions = new_possible_actions
        transitions = dict()
        matrices = [torch.rand(self.state_size, self.state_size) * torch.where(torch.rand(self.state_size, self.state_size) > self.sparsity, tt(1), tt(0)) for r in range(len(self.action_sizes))] + [torch.eye(self.state_size)]
        for p_a in possible_actions:
            transitions[tuple(p_a)] = random.choice(matrices)
        return transitions
Esempio n. 21
0
def get_inputs(rows):
    # you could also use a pandas DataFrame
    return [ tt( [ [ [ rows['open'][i],rows['close'][i],rows['volume'][i],rows['low'][i],rows['high'][i] ] ] ]) for i in range(len(rows['open'])-1 ) ]