Ejemplo n.º 1
0
    def cal_delta(self, theta):
        """Calculate delta (mean utility) from contraction mapping"""
        _blp, theta, delta, v, D, x2, nmkt, nsimind, nbrand = self.set_aliases(
        )

        theta_v = theta[:, 0]
        theta_D = theta[:, 1:]

        niter = 0

        exp_mu = np.exp(
            _blp.cal_mu(theta_v, theta_D, v, D, x2, nmkt, nsimind, nbrand))

        while True:
            diff = self.ln_s_jt.copy()

            exp_xb = np.exp(delta.reshape(-1, 1)) * exp_mu

            diff -= np.log(_blp.cal_mktshr(exp_xb, nmkt, nsimind, nbrand))

            if np.isnan(diff).sum():
                print('nan in diffs')
                break

            delta += diff

            if (abs(diff).max() < self.etol) and (abs(diff).mean() < 1e-3):
                break

            niter += 1

        print('contraction mapping finished in {} iterations'.format(niter))
Ejemplo n.º 2
0
    def cal_delta(self, theta):
        """Calculate delta (mean utility) from contraction mapping"""
        _blp, theta, delta, v, D, x2, nmkt, nsimind, nbrand = self.set_aliases()

        theta_v = theta[:, 0]
        theta_D = theta[:, 1:]

        niter = 0

        exp_mu = np.exp(_blp.cal_mu(theta_v, theta_D,
                                 v, D, x2,
                                 nmkt, nsimind, nbrand))

        while True:
            diff = self.ln_s_jt.copy()

            exp_xb = np.exp(delta.reshape(-1, 1)) * exp_mu

            diff -= np.log(_blp.cal_mktshr(exp_xb, nmkt, nsimind, nbrand))

            if np.isnan(diff).sum():
                print('nan in diffs')
                break

            delta += diff

            if (abs(diff).max() < self.etol) and (abs(diff).mean() < 1e-3):
                break

            niter += 1

        print('contraction mapping finished in {} iterations'.format(niter))
Ejemplo n.º 3
0
    def cal_jacobian(self, theta):
        """calculate the Jacobian"""

        _blp, theta, delta, v, D, x2, nmkt, nsimind, nbrand = self.set_aliases(
        )

        mu = _blp.cal_mu(theta[:, 0], theta[:, 1:], v, D, x2, nmkt, nsimind,
                         nbrand)

        exp_xb = np.exp(delta.reshape(-1, 1) + mu)

        ind_choice_prob = _blp.cal_ind_choice_prob(exp_xb, nmkt, nsimind,
                                                   nbrand)

        nk = self.x2.shape[1]
        nD = theta.shape[1] - 1
        f1 = np.zeros((delta.shape[0], nk * (nD + 1)))

        # cdid relates each observation to the market it is in
        cdid = np.arange(nmkt).repeat(nbrand)

        cdindex = np.arange(nbrand, nbrand * (nmkt + 1), nbrand) - 1

        # compute (partial share) / (partial sigma)
        for k in xrange(nk):
            xv = x2[:, k].reshape(-1, 1).dot(np.ones((1, nsimind)))
            xv *= v[cdid, nsimind * k:nsimind * (k + 1)]

            temp = (xv * ind_choice_prob).cumsum(axis=0)
            sum1 = temp[cdindex, :]

            sum1[1:, :] = sum1[1:, :] - sum1[0:-1, :]

            f1[:, k] = (ind_choice_prob * (xv - sum1[cdid, :])).mean(axis=1)

        for d in xrange(nD):
            tmpD = D[cdid, nsimind * d:nsimind * (d + 1)]

            temp1 = np.zeros((cdid.shape[0], nk))

            for k in xrange(nk):
                xd = x2[:, k].reshape(-1, 1).dot(np.ones((1, nsimind))) * tmpD

                temp = (xd * ind_choice_prob).cumsum(axis=0)
                sum1 = temp[cdindex, :]

                sum1[1:, :] = sum1[1:, :] - sum1[0:-1, :]

                temp1[:, k] = (ind_choice_prob *
                               (xd - sum1[cdid, :])).mean(axis=1)

            f1[:, nk * (d + 1):nk * (d + 2)] = temp1

        rel = np.nonzero(theta.T.ravel())[0]

        f = np.zeros((cdid.shape[0], rel.shape[0]))

        n = 0

        for i in xrange(cdindex.shape[0]):
            temp = ind_choice_prob[n:cdindex[i] + 1, :]
            H1 = temp.dot(temp.T)
            H = (np.diag(temp.sum(axis=1)) - H1) / self.nsimind

            f[n:cdindex[i] + 1, :] = -solve(H, f1[n:cdindex[i] + 1, rel])

            n = cdindex[i] + 1

        return (f)
Ejemplo n.º 4
0
    def cal_jacobian(self, theta):
        """calculate the Jacobian"""

        _blp, theta, delta, v, D, x2, nmkt, nsimind, nbrand = self.set_aliases()

        mu = _blp.cal_mu(theta[:, 0], theta[:, 1:], v, D, x2, nmkt, nsimind, nbrand)

        exp_xb = np.exp(delta.reshape(-1, 1) + mu)

        ind_choice_prob = _blp.cal_ind_choice_prob(exp_xb, nmkt, nsimind, nbrand)

        nk = self.x2.shape[1]
        nD = theta.shape[1] - 1
        f1 = np.zeros((delta.shape[0], nk * (nD + 1)))

        # cdid relates each observation to the market it is in
        cdid = np.arange(nmkt).repeat(nbrand)

        cdindex = np.arange(nbrand, nbrand * (nmkt + 1), nbrand) - 1

        # compute (partial share) / (partial sigma)
        for k in xrange(nk):
            xv = x2[:, k].reshape(-1, 1).dot(np.ones((1, nsimind)))
            xv *= v[cdid, nsimind * k:nsimind * (k + 1)]

            temp = (xv * ind_choice_prob).cumsum(axis=0)
            sum1 = temp[cdindex, :]

            sum1[1:, :] = sum1[1:, :] - sum1[0:-1, :]

            f1[:, k] = (ind_choice_prob * (xv - sum1[cdid, :])).mean(axis=1)

        for d in xrange(nD):
            tmpD = D[cdid, nsimind * d:nsimind * (d + 1)]

            temp1 = np.zeros((cdid.shape[0], nk))

            for k in xrange(nk):
                xd = x2[:, k].reshape(-1, 1).dot(np.ones((1, nsimind))) * tmpD

                temp = (xd * ind_choice_prob).cumsum(axis=0)
                sum1 = temp[cdindex, :]

                sum1[1:, :] = sum1[1:, :] - sum1[0:-1, :]

                temp1[:, k] = (ind_choice_prob * (xd-sum1[cdid, :])).mean(axis=1)

            f1[:, nk * (d + 1):nk * (d + 2)] = temp1

        rel = np.nonzero(theta.T.ravel())[0]

        f = np.zeros((cdid.shape[0], rel.shape[0]))

        n = 0

        for i in xrange(cdindex.shape[0]):
            temp = ind_choice_prob[n:cdindex[i] + 1, :]
            H1 = temp.dot(temp.T)
            H = (np.diag(temp.sum(axis=1)) - H1) / self.nsimind

            f[n:cdindex[i] + 1, :] = - solve(H, f1[n:cdindex[i] + 1, rel])

            n = cdindex[i] + 1

        return(f)
Ejemplo n.º 5
0
    def cal_jacobian(self, theta):
        """calculate the Jacobian"""

        _blp, theta, delta, v, D, x2, nmkt, nsimind, nbrand = self.set_aliases()

        # delta = log(scipy.io.loadmat(r'/home/joon/documents/coursework-phd/2008-fall/Hendricks -  Industrial Organization/Problem Set 4/Data&Code/mvalold.mat')['mvalold'].reshape(-1, ))

        mu = _blp.cal_mu(theta[:, 0], theta[:, 1:], v, D, x2, nmkt, nsimind, nbrand)

        exp_xb = exp(delta.reshape(-1, 1) + mu)

        ind_choice_prob = _blp.cal_ind_choice_prob(exp_xb, nmkt, nsimind, nbrand)

        nk = self.x2.shape[1]
        nD = theta.shape[1] - 1
        f1 = zeros((delta.shape[0], nk * (nD + 1)))

        # cdid relates each observation to the market it is in
        cdid = arange(nmkt).repeat(nbrand)

        cdindex = arange(nbrand, nbrand * (nmkt + 1), nbrand) - 1

        # computing (partial share)/(partial sigma)
        for k in xrange(nk):
            xv = x2[:, k].reshape(-1, 1).dot(ones((1, nsimind)))
            xv *= v[cdid, nsimind * k : nsimind * (k + 1)]

            temp = (xv * ind_choice_prob).cumsum(axis=0)
            sum1 = temp[cdindex, :]

            sum1[1:, :] = sum1[1:, :] - sum1[0:-1, :]

            f1[:, k] = (ind_choice_prob * (xv - sum1[cdid, :])).mean(axis=1)

        for d in xrange(nD):
            tmpD = D[cdid, nsimind * d : nsimind * (d + 1)]

            temp1 = zeros((cdid.shape[0], nk))

            for k in xrange(nk):
                xd = x2[:, k].reshape(-1, 1).dot(ones((1, nsimind))) * tmpD

                temp = (xd * ind_choice_prob).cumsum(axis=0)
                sum1 = temp[cdindex, :]

                sum1[1:, :] = sum1[1:, :] - sum1[0:-1, :]

                temp1[:, k] = (ind_choice_prob * (xd - sum1[cdid, :])).mean(axis=1)

            f1[:, nk * (d + 1) : nk * (d + 2)] = temp1

        rel = nonzero(theta.T.ravel())[0]

        f = zeros((cdid.shape[0], rel.shape[0]))

        n = 0

        for i in xrange(cdindex.shape[0]):
            temp = ind_choice_prob[n : cdindex[i] + 1, :]
            H1 = temp.dot(temp.T)
            H = (diag(temp.sum(axis=1)) - H1) / self.nsimind

            f[n : cdindex[i] + 1, :] = -solve(H, f1[n : cdindex[i] + 1, rel])

            n = cdindex[i] + 1

        return f