src/models/hippo/hippo.py
def transition(measure, N, **measure_args):
    """ A, B transition matrices for different measures

    measure: the type of measure
      legt - Legendre (translated)
      legs - Legendre (scaled)
      glagt - generalized Laguerre (translated)
      lagt, tlagt - previous versions of (tilted) Laguerre with slightly different normalization
    """
    # Laguerre (translated)
    if measure == 'lagt':
        b = measure_args.get('beta', 1.0)
        A = np.eye(N) / 2 - np.tril(np.ones((N, N)))
        B = b * np.ones((N, 1))
    elif measure == 'tlagt':
        # beta = 1 corresponds to no tilt
        b = measure_args.get('beta', 1.0)
        A = (1.-b)/2 * np.eye(N) - np.tril(np.ones((N, N)))
        B = b * np.ones((N, 1))
    # Generalized Laguerre
    # alpha 0, beta small is most stable (limits to the 'lagt' measure)
    # alpha 0, beta 1 has transition matrix A = [lower triangular 1]
    elif measure == 'glagt':
        alpha = measure_args.get('alpha', 0.0)
        beta = measure_args.get('beta', 0.01)
        A = -np.eye(N) * (1 + beta) / 2 - np.tril(np.ones((N, N)), -1)
        B = ss.binom(alpha + np.arange(N), np.arange(N))[:, None]

        L = np.exp(.5 * (ss.gammaln(np.arange(N)+alpha+1) - ss.gammaln(np.arange(N)+1)))
        A = (1./L[:, None]) * A * L[None, :]
        B = (1./L[:, None]) * B * np.exp(-.5 * ss.gammaln(1-alpha)) * beta**((1-alpha)/2)
    # Legendre (translated)
    elif measure == 'legt':
        Q = np.arange(N, dtype=np.float64)
        R = (2*Q + 1) ** .5
        j, i = np.meshgrid(Q, Q)
        A = R[:, None] * np.where(i < j, (-1.)**(i-j), 1) * R[None, :]
        B = R[:, None]
        A = -A
    # LMU: equivalent to LegT up to normalization
    elif measure == 'lmu':
        Q = np.arange(N, dtype=np.float64)
        R = (2*Q + 1)[:, None] # / theta
        j, i = np.meshgrid(Q, Q)
        A = np.where(i < j, -1, (-1.)**(i-j+1)) * R
        B = (-1.)**Q[:, None] * R
    # Legendre (scaled)
    elif measure == 'legs':
        q = np.arange(N, dtype=np.float64)
        col, row = np.meshgrid(q, q)
        r = 2 * q + 1
        M = -(np.where(row >= col, r, 0) - np.diag(q))
        T = np.sqrt(np.diag(2 * q + 1))
        A = T @ M @ np.linalg.inv(T)
        B = np.diag(T)[:, None]
        B = B.copy() # Otherwise "UserWarning: given NumPY array is not writeable..." after torch.as_tensor(B)
    elif measure == 'fourier':
        freqs = np.arange(N//2)
        d = np.stack([freqs, np.zeros(N//2)], axis=-1).reshape(-1)[:-1]
        A = 2*np.pi*(np.diag(d, 1) - np.diag(d, -1))
        A = A - embed_c2r(np.ones((N//2, N//2)))
        B = embed_c2r(np.ones((N//2, 1)))[..., :1]
    elif measure == 'random':
        A = np.random.randn(N, N) / N
        B = np.random.randn(N, 1)
    elif measure == 'diagonal':
        A = -np.diag(np.exp(np.random.randn(N)))
        B = np.random.randn(N, 1)
    else:
        raise NotImplementedError

    return A, B
 A, B transition matrices for different measures

    measure: the type of measure
      legt - Legendre (translated)
      legs - Legendre (scaled)
      glagt - generalized Laguerre (translated)
      lagt, tlagt - previous versions of (tilted) Laguerre with slightly different normalization
    
A, B transition matrices for different measures
measure: the type of measure
legt - Legendre (translated)
legs - Legendre (scaled)
glagt - generalized Laguerre (translated)
lagt, tlagt - previous versions of (tilted) Laguerre with slightly different normalization
A B transition matrices for different measures measure : the type of measure legt - Legendre ( translated ) legs - Legendre ( scaled ) glagt - generalized Laguerre ( translated ) lagt tlagt - previous versions of ( tilted ) Laguerre with slightly different normalization
def transition(measure, N, **measure_args):
    if measure == 'lagt':
        b = measure_args.get('beta', 1.0)
        A = np.eye(N) / 2 - np.tril(np.ones((N, N)))
        B = b * np.ones((N, 1))
    elif measure == 'tlagt':
        b = measure_args.get('beta', 1.0)
        A = (1.-b)/2 * np.eye(N) - np.tril(np.ones((N, N)))
        B = b * np.ones((N, 1))
    elif measure == 'glagt':
        alpha = measure_args.get('alpha', 0.0)
        beta = measure_args.get('beta', 0.01)
        A = -np.eye(N) * (1 + beta) / 2 - np.tril(np.ones((N, N)), -1)
        B = ss.binom(alpha + np.arange(N), np.arange(N))[:, None]
        L = np.exp(.5 * (ss.gammaln(np.arange(N)+alpha+1) - ss.gammaln(np.arange(N)+1)))
        A = (1./L[:, None]) * A * L[None, :]
        B = (1./L[:, None]) * B * np.exp(-.5 * ss.gammaln(1-alpha)) * beta**((1-alpha)/2)
    elif measure == 'legt':
        Q = np.arange(N, dtype=np.float64)
        R = (2*Q + 1) ** .5
        j, i = np.meshgrid(Q, Q)
        A = R[:, None] * np.where(i < j, (-1.)**(i-j), 1) * R[None, :]
        B = R[:, None]
        A = -A
    elif measure == 'lmu':
        Q = np.arange(N, dtype=np.float64)
        R = (2*Q + 1)[:, None] 
        j, i = np.meshgrid(Q, Q)
        A = np.where(i < j, -1, (-1.)**(i-j+1)) * R
        B = (-1.)**Q[:, None] * R
    elif measure == 'legs':
        q = np.arange(N, dtype=np.float64)
        col, row = np.meshgrid(q, q)
        r = 2 * q + 1
        M = -(np.where(row >= col, r, 0) - np.diag(q))
        T = np.sqrt(np.diag(2 * q + 1))
        A = T @ M @ np.linalg.inv(T)
        B = np.diag(T)[:, None]
        B = B.copy() 
    elif measure == 'fourier':
        freqs = np.arange(N//2)
        d = np.stack([freqs, np.zeros(N//2)], axis=-1).reshape(-1)[:-1]
        A = 2*np.pi*(np.diag(d, 1) - np.diag(d, -1))
        A = A - embed_c2r(np.ones((N//2, N//2)))
        B = embed_c2r(np.ones((N//2, 1)))[..., :1]
    elif measure == 'random':
        A = np.random.randn(N, N) / N
        B = np.random.randn(N, 1)
    elif measure == 'diagonal':
        A = -np.diag(np.exp(np.random.randn(N)))
        B = np.random.randn(N, 1)
    else:
        raise NotImplementedError
    return A, B
def transition ( measure , N , ** measure_args ) : if measure == 'lagt' : b = measure_args . get ( 'beta' , 1.0 ) A = np . eye ( N ) / 2 - np . tril ( np . ones ( ( N , N ) ) ) B = b * np . ones ( ( N , 1 ) ) elif measure == 'tlagt' : b = measure_args . get ( 'beta' , 1.0 ) A = ( 1. - b ) / 2 * np . eye ( N ) - np . tril ( np . ones ( ( N , N ) ) ) B = b * np . ones ( ( N , 1 ) ) elif measure == 'glagt' : alpha = measure_args . get ( 'alpha' , 0.0 ) beta = measure_args . get ( 'beta' , 0.01 ) A = - np . eye ( N ) * ( 1 + beta ) / 2 - np . tril ( np . ones ( ( N , N ) ) , - 1 ) B = ss . binom ( alpha + np . arange ( N ) , np . arange ( N ) ) [ : , None ] L = np . exp ( .5 * ( ss . gammaln ( np . arange ( N ) + alpha + 1 ) - ss . gammaln ( np . arange ( N ) + 1 ) ) ) A = ( 1. / L [ : , None ] ) * A * L [ None , : ] B = ( 1. / L [ : , None ] ) * B * np . exp ( - .5 * ss . gammaln ( 1 - alpha ) ) * beta ** ( ( 1 - alpha ) / 2 ) elif measure == 'legt' : Q = np . arange ( N , dtype = np . float64 ) R = ( 2 * Q + 1 ) ** .5 j , i = np . meshgrid ( Q , Q ) A = R [ : , None ] * np . where ( i < j , ( - 1. ) ** ( i - j ) , 1 ) * R [ None , : ] B = R [ : , None ] A = - A elif measure == 'lmu' : Q = np . arange ( N , dtype = np . float64 ) R = ( 2 * Q + 1 ) [ : , None ] j , i = np . meshgrid ( Q , Q ) A = np . where ( i < j , - 1 , ( - 1. ) ** ( i - j + 1 ) ) * R B = ( - 1. ) ** Q [ : , None ] * R elif measure == 'legs' : q = np . arange ( N , dtype = np . float64 ) col , row = np . meshgrid ( q , q ) r = 2 * q + 1 M = - ( np . where ( row >= col , r , 0 ) - np . diag ( q ) ) T = np . sqrt ( np . diag ( 2 * q + 1 ) ) A = T @ M @ np . linalg . inv ( T ) B = np . diag ( T ) [ : , None ] B = B . copy ( ) elif measure == 'fourier' : freqs = np . arange ( N // 2 ) d = np . stack ( [ freqs , np . zeros ( N // 2 ) ] , axis = - 1 ) . reshape ( - 1 ) [ : - 1 ] A = 2 * np . pi * ( np . diag ( d , 1 ) - np . diag ( d , - 1 ) ) A = A - embed_c2r ( np . ones ( ( N // 2 , N // 2 ) ) ) B = embed_c2r ( np . ones ( ( N // 2 , 1 ) ) ) [ ... , : 1 ] elif measure == 'random' : A = np . random . randn ( N , N ) / N B = np . random . randn ( N , 1 ) elif measure == 'diagonal' : A = - np . diag ( np . exp ( np . random . randn ( N ) ) ) B = np . random . randn ( N , 1 ) else : raise NotImplementedError return A , B
A, B transition matrices for different measures
measure: the type of measure
legt - Legendre (translated)
legs - Legendre (scaled)
glagt - generalized Laguerre (translated)
lagt, tlagt - previous versions of (tilted) Laguerre with slightly different normalization
A B transition matrices for different measures measure : the type of measure legt - Legendre ( translated ) legs - Legendre ( scaled ) glagt - generalized Laguerre ( translated ) lagt tlagt - previous versions of ( tilted ) Laguerre with slightly different normalization
""" A, B transition matrices for different measures

    measure: the type of measure
      legt - Legendre (translated)
      legs - Legendre (scaled)
      glagt - generalized Laguerre (translated)
      lagt, tlagt - previous versions of (tilted) Laguerre with slightly different normalization
    """ # Laguerre (translated) # beta = 1 corresponds to no tilt # Generalized Laguerre # alpha 0, beta small is most stable (limits to the 'lagt' measure) # alpha 0, beta 1 has transition matrix A = [lower triangular 1] # Legendre (translated) # LMU: equivalent to LegT up to normalization # / theta # Legendre (scaled) # Otherwise "UserWarning: given NumPY array is not writeable..." after torch.as_tensor(B)
measure
N