distance.py 文件源码

python
阅读 35 收藏 0 点赞 0 评论 0

项目:fitr 作者: abrahamnunes 项目源码 文件源码
def likelihood_distance(loglik_func, data, params, diff_metric='sq', dist_metric='cosine', verbose=False):
    """
    Estimates the likelihood of the data from the i'th subject using the parameter estimates of the j'th subject, for all i and j, then computes the distance between subjects' likelihood difference vectors

    Parameters
    ----------
    loglik_func : function
        The log-likelihood function to be used
    data : dict
        Data formatted for input into the log-likelihood function
    params : ndarray(shape=(nsubjects, nparams))
        Array of parameter estimates
    diff_metric : {'sq', 'diff', 'abs'}
        Which type of difference measure to compute, 'diff' is simple subtractive difference, whereas 'sq' and 'abs' are the squared and absolute differences, respectively
    dist_metric : str (default='cosine')
        The pairwise distance metric to use. Any option that can be passed into ``sklearn.metrics.pairwise_distances`` can work.
    verbose : bool
        Whether to print out progress

    Returns
    -------
    ndarray(shape=(nsubjects, nsubjects))
    """
    nsubjects = np.shape(params)[0]
    D = np.zeros([nsubjects, nsubjects])
    for i in range(nsubjects):
        S = data[i]['S']
        A = data[i]['A']
        R = data[i]['R']

        if verbose is True:
            print('Likelihood Differences: Subject ' + str(i))

        # Compute loglikelihood for subject i with own data
        LL0 = loglik_func(params=params[i, :],
                          states=S,
                          actions=A,
                          rewards=R)

        for j in range(nsubjects):
            if i !=j:
                LL1 = loglik_func(params=params[j, :],
                                  states=S,
                                  actions=A,
                                  rewards=R)

                if diff_metric == 'diff':
                    D[i, j] = LL1 - LL0
                elif diff_metric == 'sq':
                    D[i, j] = (LL1 - LL0)**2
                elif diff_metric == 'abs':
                    D[i, j] = np.abs(LL1 - LL0)

    return pairwise_distances(D, metric=dist_metric)
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号