def predictB(
        nIter, nTakes, nSim, seed,
        zetaMu, zetaSi, psiB, omegaB, psiW, omegaW,
        indID, obsID, altID, chosen,
        xRnd):
    
    np.random.seed(seed)
    
    ###
    #Prepare data
    ###
    
    nRnd = xRnd.shape[1]
    
    xList = [xRnd]
    (xList,
     nInd, nObs, nRow,
     chosenIdx, nonChosenIdx,
     rowsPerInd, rowsPerObs,
     _, map_avail_to_obs) = prepareData(xList, indID, obsID, chosen)
    xRnd = xList[0]
    
    sim_xRnd = np.tile(xRnd, (nSim, 1))
    sim_rowsPerObs = np.tile(rowsPerObs, (nSim,))
    sim_map_avail_to_obs = scipy.sparse.kron(scipy.sparse.eye(nSim), map_avail_to_obs)

    ###
    #Prediction
    ###
    
    pPred = np.zeros((nRow + nObs,))  
    vFix = 0 
    
    zetaCh = np.linalg.cholesky(zetaSi)
    
    for i in np.arange(nIter):
        zeta_tmp = zetaMu + zetaCh @ np.random.randn(nRnd,)
        chB_tmp = np.linalg.cholesky(invwishart.rvs(omegaB, psiB).reshape((nRnd, nRnd)))
        chW_tmp = np.linalg.cholesky(invwishart.rvs(omegaW, psiW).reshape((nRnd, nRnd)))
        
        pPred_iter = np.zeros((nRow + nObs,))
        
        for j in np.arange(nSim * nTakes):
            betaRndInd = zeta_tmp.reshape((1,nRnd)) + (chB_tmp @ np.random.randn(nRnd, nInd)).T
            betaRndInd_perRow = np.tile(np.repeat(betaRndInd, rowsPerInd, axis = 0), (nSim, 1))
                
            for k in np.arange(nTakes):
                betaRndObs = (chW_tmp @ np.random.randn(nRnd, nObs * nSim)).T
                betaRnd = betaRndInd_perRow + np.repeat(betaRndObs, sim_rowsPerObs, axis = 0)
                vRnd = np.sum(sim_xRnd * betaRnd, axis = 1)
                    
                pPred_take = pPredMxl(vFix, vRnd, sim_map_avail_to_obs, nSim, chosenIdx, nonChosenIdx)
                pPred_iter += pPred_take 
        
        pPred += pPred_iter / (nSim * nTakes**2)
        
    pPred /= nIter  
    return pPred
def predictionMsle(nIter, nTakes, nSim, seed, param, iHess, indID, obsID,
                   altID, chosen, xFix, xRnd):

    np.random.seed(seed)

    ###
    #Prepare data
    ###

    nFix = xFix.shape[1]
    nRnd = xRnd.shape[1]

    xList = [xFix, xRnd]
    (xList, nInd, nObs, nRow, chosenIdx, nonChosenIdx, rowsPerInd, rowsPerObs,
     _, map_avail_to_obs) = prepareData(xList, indID, obsID, chosen)
    xFix, xRnd = xList[0], xList[1]

    sim_xRnd = np.tile(xRnd, (nSim, 1))
    sim_rowsPerInd = np.tile(rowsPerInd, (nSim, ))
    sim_map_avail_to_obs = scipy.sparse.kron(scipy.sparse.eye(nSim),
                                             map_avail_to_obs)

    chIdx = np.triu_indices(nRnd)
    chIdx = chIdx[1], chIdx[0]

    ###
    #Prediction
    ###

    pPred = np.zeros((nRow + nObs, ))

    vFix = 0
    chIHess = np.linalg.cholesky(iHess)
    nParam = param.shape[0]

    for i in np.arange(nIter):
        paramSim = param + chIHess @ np.random.randn(nParam, )
        if nFix:
            paramFixSim = np.array(paramSim[:nFix])
            vFix = np.tile(xFix @ paramFixSim, (nSim, ))
        paramRnd_muSim = np.array(paramSim[nFix:(nFix + nRnd)])
        paramRnd_chSim = np.zeros((nRnd, nRnd))
        paramRnd_chSim[chIdx] = np.array(paramSim[(nFix + nRnd):])

        pPred_iter = np.zeros((nRow + nObs, ))

        for t in np.arange(nTakes):
            paramRndSim = paramRnd_muSim + (
                paramRnd_chSim @ np.random.randn(nRnd, nInd * nSim)).T
            paramRndSimPerRow = np.repeat(paramRndSim, sim_rowsPerInd, axis=0)
            vRnd = np.sum(sim_xRnd * paramRndSimPerRow, axis=1)

            pPred_take = pPredMxl(vFix, vRnd, sim_map_avail_to_obs, nSim,
                                  chosenIdx, nonChosenIdx)
            pPred_iter += pPred_take
        pPred += (pPred_iter / nTakes)
    pPred /= nIter
    return pPred
示例#3
0
def mcmcChainPredW(chainID, seed, mcmc_iterSampleThin, mcmc_iterSampleThinPred,
                   mcmc_disp, nTakes, nSim, modelName, sim_xRnd, nRnd, nInd,
                   nObs, nRow, obsPerInd, sim_rowsPerObs, sim_map_avail_to_obs,
                   chosenIdx, nonChosenIdx):

    np.random.seed(seed + chainID)

    ###
    #Retrieve draws
    ###

    fileName = modelName + '_draws_chain' + str(chainID + 1) + '.hdf5'
    file = h5py.File(fileName, "r")

    paramRndB_store = np.array(file['paramRndB_store'][:, :nInd, :])
    OmegaW_store = np.array(file['OmegaW_store'])

    ###
    #Simulate
    ###

    pPred = np.zeros((nRow + nObs, ))
    vFix = 0

    sampleIdx = np.sort(
        np.random.choice(np.arange(mcmc_iterSampleThin),
                         size=mcmc_iterSampleThinPred,
                         replace=False))

    ii = -1
    for i in sampleIdx:
        ii += 1

        paramRndB_tmp = np.tile(
            np.repeat(paramRndB_store[i, :, :], obsPerInd, axis=0), (nSim, 1))
        chW_tmp = np.linalg.cholesky(OmegaW_store[i, :, :])

        pPred_iter = np.zeros((nRow + nObs, ))

        for t in np.arange(nTakes):
            paramRnd = paramRndB_tmp + (
                chW_tmp @ np.random.randn(nRnd, nObs * nSim)).T
            paramRndPerRow = np.repeat(paramRnd, sim_rowsPerObs, axis=0)
            vRnd = np.sum(sim_xRnd * paramRndPerRow, axis=1)

            pPred_take = pPredMxl(vFix, vRnd, sim_map_avail_to_obs, nSim,
                                  chosenIdx, nonChosenIdx)
            pPred_iter += pPred_take

        pPred += pPred_iter / nTakes

        if ((ii + 1) % mcmc_disp) == 0:
            print('Chain ' + str(chainID + 1) + '; iteration: ' + str(ii + 1) +
                  ' (predictive simulation)')
            sys.stdout.flush()

    pPred /= mcmc_iterSampleThinPred
    return pPred
示例#4
0
def mcmcChainPred(
        chainID, seed,
        mcmc_iterSampleThin, mcmc_disp, nTakes, nSim,
        modelName,
        xFix, nFix, 
        sim_xRnd, nRnd, 
        nInd, nObs, nRow,
        sim_rowsPerInd, sim_map_avail_to_obs, chosenIdx, nonChosenIdx):   
    
    np.random.seed(seed + chainID)
    
    ###
    #Retrieve draws
    ###
    
    fileName = modelName + '_draws_chain' + str(chainID + 1) + '.hdf5'
    file = h5py.File(fileName, "r")
    
    paramFix_store = None
    if nFix: paramFix_store = np.array(file['paramFix_store'])
    zeta_store = np.array(file['zeta_store'])
    Omega_store = np.array(file['Omega_store'])
    
    ###
    #Simulate
    ###

    pPred = np.zeros((nRow + nObs,))
    vFix = 0 
    
    for i in np.arange(mcmc_iterSampleThin):
        
        if nFix: 
            paramFix = paramFix_store[i,:]
            vFix = np.tile(xFix @ paramFix, (nSim,));
        
        zeta_tmp = zeta_store[i,:]
        ch_tmp = np.linalg.cholesky(Omega_store[i,:,:])
        
        pPred_iter = np.zeros((nRow + nObs,))
        
        for t in np.arange(nTakes):
            paramRnd = zeta_tmp + (ch_tmp @ np.random.randn(nRnd, nInd * nSim)).T
            paramRndPerRow = np.repeat(paramRnd, sim_rowsPerInd, axis = 0)
            vRnd = np.sum(sim_xRnd * paramRndPerRow, axis = 1)
            
            pPred_take = pPredMxl(vFix, vRnd, sim_map_avail_to_obs, nSim, chosenIdx, nonChosenIdx)
            pPred_iter += pPred_take
            
        pPred += (pPred_iter / nTakes)
        
        if ((i + 1) % mcmc_disp) == 0:
            print('Chain ' + str(chainID + 1) + '; iteration: ' + str(i + 1) + ' (predictive simulation)')
            sys.stdout.flush()
            
    pPred /= mcmc_iterSampleThin
    return pPred
示例#5
0
def predict(nIter, nTakes, nSim, seed, paramFixMu, paramFixCh, zetaMu, zetaSi,
            psi, omega, indID, obsID, altID, chosen, xFix, xRnd):

    np.random.seed(seed)

    ###
    #Prepare data
    ###

    nFix = xFix.shape[1]
    nRnd = xRnd.shape[1]

    xList = [xFix, xRnd]
    (xList, nInd, nObs, nRow, chosenIdx, nonChosenIdx, rowsPerInd, rowsPerObs,
     _, map_avail_to_obs) = prepareData(xList, indID, obsID, chosen)
    xFix, xRnd = xList[0], xList[1]

    sim_xRnd = np.tile(xRnd, (nSim, 1))
    sim_rowsPerInd = np.tile(rowsPerInd, (nSim, ))
    sim_map_avail_to_obs = scipy.sparse.kron(scipy.sparse.eye(nSim),
                                             map_avail_to_obs)

    chIdx = np.triu_indices(nRnd)
    chIdx = chIdx[1], chIdx[0]

    ###
    #Prediction
    ###

    pPred = np.zeros((nRow + nObs, ))
    vFix = 0

    zetaCh = np.linalg.cholesky(zetaSi)

    for i in np.arange(nIter):
        if nFix:
            paramFix = paramFixMu + paramFixCh @ np.random.randn(nFix, )
            vFix = np.tile(xFix @ paramFix, (nSim, ))
        zeta = zetaMu + zetaCh @ np.random.randn(nRnd, )
        ch = np.linalg.cholesky(
            invwishart.rvs(omega, psi).reshape((nRnd, nRnd)))

        pPred_iter = np.zeros((nRow + nObs, ))

        for t in np.arange(nTakes):
            paramRnd = zeta + (ch @ np.random.randn(nRnd, nInd * nSim)).T
            paramRndPerRow = np.repeat(paramRnd, sim_rowsPerInd, axis=0)
            vRnd = np.sum(sim_xRnd * paramRndPerRow, axis=1)

            pPred_take = pPredMxl(vFix, vRnd, sim_map_avail_to_obs, nSim,
                                  chosenIdx, nonChosenIdx)
            pPred_iter += pPred_take
        pPred += (pPred_iter / nTakes)
    pPred /= nIter
    return pPred
def predictW(
        nIter, nTakes, nSim, seed,
        paramRndMuB, paramRndSiB, psiW, omegaW,
        indID, obsID, altID, chosen,
        xRnd):
    
    np.random.seed(seed)
    
    ###
    #Prepare data
    ###
    
    nRnd = xRnd.shape[1]
    
    xList = [xRnd]
    (xList,
     nInd, nObs, nRow,
     chosenIdx, nonChosenIdx,
     rowsPerInd, rowsPerObs,
     _, map_avail_to_obs) = prepareData(xList, indID, obsID, chosen)
    xRnd = xList[0]
    
    sim_xRnd = np.tile(xRnd, (nSim, 1))
    sim_rowsPerObs = np.tile(rowsPerObs, (nSim,))
    sim_map_avail_to_obs = scipy.sparse.kron(scipy.sparse.eye(nSim), map_avail_to_obs)

    ###
    #Prediction
    ###
    
    pPred = np.zeros((nRow + nObs,))  
    vFix = 0
    
    paramRndChB = np.linalg.cholesky(paramRndSiB)
    
    for i in np.arange(nIter):
        paramRndB_tmp = np.tile(paramRndMuB + \
                                (paramRndChB @ np.random.randn(nInd, nRnd, 1)).reshape((nInd, nRnd,)), 
                                (nSim,1))
        chW_tmp = np.linalg.cholesky(invwishart.rvs(omegaW, psiW).reshape((nRnd, nRnd)))
        
        pPred_iter = np.zeros((nRow + nObs,))
        
        for t in np.arange(nTakes):
            paramRnd = paramRndB_tmp + (chW_tmp @ np.random.randn(nRnd, nObs * nSim)).T
            paramRndPerRow = np.repeat(paramRnd, sim_rowsPerObs, axis = 0)
            vRnd = np.sum(sim_xRnd * paramRndPerRow, axis = 1)
            
            pPred_take = pPredMxl(vFix, vRnd, sim_map_avail_to_obs, nSim, chosenIdx, nonChosenIdx)
            pPred_iter += pPred_take
            
        pPred += pPred_iter / nTakes
    pPred /= nIter  
    return pPred
def mcmcChainPredW(chainID, seed, mcmc_iterSampleThin, mcmc_iterSampleThinPred,
                   mcmc_disp, modelName, xFix, nFix, xRnd, nRnd, nInd, nObs,
                   nRow, rowsPerInd, map_avail_to_obs, chosenIdx,
                   nonChosenIdx):

    np.random.seed(seed + chainID)

    ###
    #Retrieve draws
    ###

    fileName = modelName + '_draws_chain' + str(chainID + 1) + '.hdf5'
    file = h5py.File(fileName, "r")

    paramFix_store = None
    paramRnd_store = None
    if nFix: paramFix_store = np.array(file['paramFix_store'])
    if nRnd: paramRnd_store = np.array(file['paramRnd_store'][:, :nInd, :])

    ###
    #Simulate
    ###

    pPred = np.zeros((nRow + nObs, ))
    vFix = 0
    vRnd = 0

    sampleIdx = np.sort(
        np.random.choice(np.arange(mcmc_iterSampleThin),
                         size=mcmc_iterSampleThinPred,
                         replace=False))

    ii = -1
    for i in sampleIdx:
        ii += 1

        if nFix:
            paramFix = np.array(paramFix_store[i, :])
            vFix = xFix @ paramFix
        if nRnd:
            paramRnd = paramRnd_store[i, :, :]
            paramRndPerRow = np.repeat(paramRnd, rowsPerInd, axis=0)
            vRnd = np.sum(xRnd * paramRndPerRow, axis=1)
        pPred += pPredMxl(vFix, vRnd, map_avail_to_obs, 1, chosenIdx,
                          nonChosenIdx)

        if ((ii + 1) % mcmc_disp) == 0:
            print('Chain ' + str(chainID + 1) + '; iteration: ' + str(ii + 1) +
                  ' (predictive simulation)')
            sys.stdout.flush()

    pPred /= mcmc_iterSampleThinPred
    return pPred
    altID_valW = np.array(altID_tot[idx])
    chosen_valW = np.array(chosen_tot[idx])
    xRnd_valW = np.array(xRnd_tot[idx, :])

    #Calculate choice probabilities for the training sample
    xList = [xRnd]

    (xList, _, _, _, chosenIdx, nonChosenIdx, rowsPerInd, _, _,
     map_avail_to_obs) = prepareData(xList, indID, obsID, chosen)
    xRnd_valDiff = xList[0]

    vFix = 0
    betaRndInd_val = np.array(betaRndInd[:N, :])
    betaRndInd_perRow_val = np.repeat(betaRndInd_val, rowsPerInd, axis=0)
    vRnd = np.sum(xRnd_valDiff * betaRndInd_perRow_val, axis=1)
    pPredT_true = pPredMxl(vFix, vRnd, map_avail_to_obs, 1, chosenIdx,
                           nonChosenIdx)

    #Simulate predictive choice distributions for the between validation sample
    xList = [xRnd_valB]

    (xList, _, _, _, chosenIdx, nonChosenIdx, rowsPerInd, _, _,
     map_avail_to_obs) = prepareData(xList, indID_valB, obsID_valB,
                                     chosen_valB)

    xRnd_valDiff = xList[0]
    xRnd_valDiff = np.tile(xRnd_valDiff, (D, 1))
    sim_map_avail_to_obs = scipy.sparse.kron(scipy.sparse.eye(D),
                                             map_avail_to_obs)
    sim_rowsPerInd = np.tile(rowsPerInd, D)
    rowsPerObs = np.squeeze(np.asarray(np.sum(map_avail_to_obs, axis=0)))
    sim_rowsPerObs = np.tile(rowsPerObs, D)
    vFix = 0

    for i in np.arange(simDraws_B):
        betaRndInd = betaRndMu_true + (
            betaRndChB_true @ np.random.randn(nRnd, N_val)).T
        betaRndInd_perRow = np.tile(np.repeat(betaRndInd, rowsPerInd, axis=0),
                                    (D_B, 1))

        for j in np.arange(nTakes_B):
            betaRndObs = (
                betaRndChW_true @ np.random.randn(nRnd, NT_val * D_W)).T
            betaRnd = betaRndInd_perRow + np.repeat(
                betaRndObs, sim_rowsPerObs, axis=0)
            vRnd = np.sum(xRnd_valDiff * betaRnd, axis=1)

            pPred_true_take = pPredMxl(vFix, vRnd, sim_map_avail_to_obs, D_W,
                                       chosenIdx, nonChosenIdx)
            pPred_true += pPred_true_take
    pPred_true /= (D_B * nTakes_B**2)
    pPredB_true = np.array(pPred_true)

    #Simulate predictive choice distributions for the within validation sample
    D_W = 1000
    nTakes_W = 10
    simDraws_W = D_W * nTakes_W

    xList = [xRnd_valW]

    (xList, _, _, _, chosenIdx, nonChosenIdx, rowsPerInd, _, _,
     map_avail_to_obs) = prepareData(xList, indID_valW, obsID_valW,
                                     chosen_valW)