(batch, n_assets_range=(5, 10), lookback_range=(2, 20), horizon_range=(3, 15), asset_ixs=None,
                    random_state=None)
return X_batch, y_batch, timestamps_batch, asset_names_batch
Create batch of samples.

    Randomly (from uniform distribution) selects assets, lookback and horizon. If `assets` are specified then assets
    kept constant.

    Parameters
    ----------
    batch : list
        List of tuples representing `(X_sample, y_sample, timestamp_sample, asset_names)`. Note that the sample
        dimension is not present and all the other dimensions are full (as determined by the dataset).

    n_assets_range : tuple
        Minimum and maximum (only left included) number of assets that are randomly subselected. Ignored if `asset_ixs`
        specified.

    lookback_range : tuple
        Minimum and maximum (only left included) of the lookback that is randomly selected.

    horizon_range : tuple
        Minimum and maximum (only left included) of the horizon that is randomly selected.

    asset_ixs : None or list
        If None, then `n_assets` sampled randomly. If ``list`` then it represents the indices of desired assets - no
        randomness and `n_assets_range` is not used.

    random_state : int or None
        Random state.

    Returns
    -------
    X_batch : torch.Tensor
        Features batch of shape `(batch_size, n_input_channels, sampled_lookback, n_sampled_assets)`.

    y_batch : torch.Tensor
        Targets batch of shape `(batch_size, n_input_channels, sampled_horizon, n_sampled_assets)`.

    timestamps_batch : list
        List of timestamps (per sample).

    asset_names_batch : list
        List of asset names in the batch (same for each sample).
Create batch of samples.
Create batch of samples .
def collate_uniform(batch, n_assets_range=(5, 10), lookback_range=(2, 20), horizon_range=(3, 15), asset_ixs=None,
                    random_state=None):
    """Create batch of samples.

    Randomly (from uniform distribution) selects assets, lookback and horizon. If `assets` are specified then assets
    kept constant.

    Parameters
    ----------
    batch : list
        List of tuples representing `(X_sample, y_sample, timestamp_sample, asset_names)`. Note that the sample
        dimension is not present and all the other dimensions are full (as determined by the dataset).

    n_assets_range : tuple
        Minimum and maximum (only left included) number of assets that are randomly subselected. Ignored if `asset_ixs`
        specified.

    lookback_range : tuple
        Minimum and maximum (only left included) of the lookback that is randomly selected.

    horizon_range : tuple
        Minimum and maximum (only left included) of the horizon that is randomly selected.

    asset_ixs : None or list
        If None, then `n_assets` sampled randomly. If ``list`` then it represents the indices of desired assets - no
        randomness and `n_assets_range` is not used.

    random_state : int or None
        Random state.

    Returns
    -------
    X_batch : torch.Tensor
        Features batch of shape `(batch_size, n_input_channels, sampled_lookback, n_sampled_assets)`.

    y_batch : torch.Tensor
        Targets batch of shape `(batch_size, n_input_channels, sampled_horizon, n_sampled_assets)`.

    timestamps_batch : list
        List of timestamps (per sample).

    asset_names_batch : list
        List of asset names in the batch (same for each sample).
    """
    # checks
    if asset_ixs is None and not n_assets_range[1] > n_assets_range[0] >= 1:
        raise ValueError('Incorrect number of assets range.')

    if not lookback_range[1] > lookback_range[0] >= 2:
        raise ValueError('Incorrect lookback range.')

    if not horizon_range[1] > horizon_range[0] >= 1:
        raise ValueError('Incorrect horizon range.')

    if random_state is not None:
        torch.manual_seed(random_state)

    lookback_max, n_assets_max = batch[0][0].shape[1:]
    horizon_max = batch[0][1].shape[1]

    # sample assets
    if asset_ixs is None:
        n_assets = torch.randint(low=n_assets_range[0], high=min(n_assets_max + 1, n_assets_range[1]), size=(1,))[0]
        asset_ixs = torch.multinomial(torch.ones(n_assets_max), n_assets.item(), replacement=False)
    else:
        pass

    # sample lookback
    lookback = torch.randint(low=lookback_range[0], high=min(lookback_max + 1, lookback_range[1]), size=(1,))[0]

    # sample horizon
    horizon = torch.randint(low=horizon_range[0], high=min(horizon_max + 1, horizon_range[1]), size=(1,))[0]

    X_batch = torch.stack([b[0][:, -lookback:, asset_ixs] for b in batch], dim=0)
    y_batch = torch.stack([b[1][:, :horizon, asset_ixs] for b in batch], dim=0)
    timestamps_batch = [b[2] for b in batch]
    asset_names_batch = [batch[0][3][ix] for ix in asset_ixs]

    return X_batch, y_batch, timestamps_batch, asset_names_batch
def collate_uniform ( batch , n_assets_range = ( 5 , 10 ) , lookback_range = ( 2 , 20 ) , horizon_range = ( 3 , 15 ) , asset_ixs = None , random_state = None ) : # checks if asset_ixs is None and not n_assets_range [ 1 ] > n_assets_range [ 0 ] >= 1 : raise ValueError ( 'Incorrect number of assets range.' ) if not lookback_range [ 1 ] > lookback_range [ 0 ] >= 2 : raise ValueError ( 'Incorrect lookback range.' ) if not horizon_range [ 1 ] > horizon_range [ 0 ] >= 1 : raise ValueError ( 'Incorrect horizon range.' ) if random_state is not None : torch . manual_seed ( random_state ) lookback_max , n_assets_max = batch [ 0 ] [ 0 ] . shape [ 1 : ] horizon_max = batch [ 0 ] [ 1 ] . shape [ 1 ] # sample assets if asset_ixs is None : n_assets = torch . randint ( low = n_assets_range [ 0 ] , high = min ( n_assets_max + 1 , n_assets_range [ 1 ] ) , size = ( 1 , ) ) [ 0 ] asset_ixs = torch . multinomial ( torch . ones ( n_assets_max ) , n_assets . item ( ) , replacement = False ) else : pass # sample lookback lookback = torch . randint ( low = lookback_range [ 0 ] , high = min ( lookback_max + 1 , lookback_range [ 1 ] ) , size = ( 1 , ) ) [ 0 ] # sample horizon horizon = torch . randint ( low = horizon_range [ 0 ] , high = min ( horizon_max + 1 , horizon_range [ 1 ] ) , size = ( 1 , ) ) [ 0 ] X_batch = torch . stack ( [ b [ 0 ] [ : , - lookback : , asset_ixs ] for b in batch ] , dim = 0 ) y_batch = torch . stack ( [ b [ 1 ] [ : , : horizon , asset_ixs ] for b in batch ] , dim = 0 ) timestamps_batch = [ b [ 2 ] for b in batch ] asset_names_batch = [ batch [ 0 ] [ 3 ] [ ix ] for ix in asset_ixs ] return X_batch , y_batch , timestamps_batch , asset_names_batch