示例#1
0
    def __init__(self,
                 slop,
                 threshold,
                 init_theta=None,
                 score=None,
                 iter_method='newton'):
        """
        不管是probit还是logit,都是用一样的参数估计算法,
        基于牛顿迭代的极大似然算法和贝叶斯最大后验算法
        :param slop: ndarray(float), 多维向量,斜率,区分度
        :param threshold: ndarray(float), 单维向量,阈值,通俗度,难度
        :param init_theta: ndarray(int|float), 特质向量初值
        :param score:  ndarray(0|1), 得分向量
        """
        if not isinstance(slop, np.ndarray):
            raise ItemParamError('item param must be ndarray')
        if not isinstance(threshold, np.ndarray):
            raise ItemParamError('item param must be ndarray')

        if len(slop.shape) == 1:
            slop.shape = 1, slop.shape[0]

        if len(slop) != len(threshold):
            raise ItemParamError('item param must be same length')

        if score is not None:
            if not isinstance(score, np.ndarray):
                raise ScoreError('score must be ndarray')
            if len(score) != len(slop):
                raise ScoreError('score must be same length as item param')

        if init_theta is not None and not isinstance(init_theta, np.ndarray):
            raise ThetaError('init_theta must be ndarray')

        if iter_method not in ('newton', 'gradient_ascent'):
            raise IterMethodError(
                'iter_method must be newton or gradient_ascent')

        self._slop = slop
        self._score = score
        self._threshold = threshold
        self._init_theta = init_theta if init_theta is not None else np.zeros(
            len(self._slop[0]))
        # 默认bayes先验正态分布标准差
        # TODO 改为根据样本估计
        self._inv_psi = np.identity(len(self._slop[0]))
        self._iter_method = iter_method
示例#2
0
 def z(self, theta):
     if not isinstance(theta, np.ndarray):
         raise ThetaError('theta must be ndarray')
     return self._z(theta)
示例#3
0
 def prob(self, theta):
     # 回答为1的概率
     if not isinstance(theta, np.ndarray):
         raise ThetaError('theta must be ndarray')
     return self._prob(theta)
示例#4
0
 def info(self, theta):
     # 信息矩阵
     if not isinstance(theta, np.ndarray):
         raise ThetaError('theta must be ndarray')
     _info = super(BayesProbitModel, self).info(theta)
     return _info + self._inv_psi
示例#5
0
 def info(self, theta):
     # 信息矩阵
     if not isinstance(theta, np.ndarray):
         raise ThetaError('theta must be ndarray')
     h, prob_val, w = self._get_h_prob_val_w(theta)
     return np.dot(self._slop.transpose() * w, self._slop)
示例#6
0
 def info(self, theta):
     # 信息矩阵
     if not isinstance(theta, np.ndarray):
         raise ThetaError('theta must be ndarray')
     prob_val = self._prob(theta)
     return self._expect(prob_val)