Beispiel #1
0
 def compute_stationary_dist(self):
     
    """                               
     Generates a NumPy array containing the stationary distribution
    """ 
     
     self.mu = mc_compute_stationary(self.P)
 def equilibrium(self):
     self.set_move()
     Y = mc_compute_stationary(self.X)
     tit = str(self.n)+' people,  '+self.mode+' mode, '+'epsilon = '+str(self.epsi)
     plt.bar(range(self.n+1), Y, align='center')
     plt.xlim([-0.5, self.n+0.5])
     plt.ylim([0,1])
     plt.title(tit)        
     plt.show()
Beispiel #3
0
def test_compute_stationary():
    for i, pvalues in enumerate(pvalues_list):
        if pvalues['move'] == 'simultaneous':
            P = KMR_Markov_matrix_simultaneous(pvalues['N'], p, pvalues['epsilon'])
        else:
            P = KMR_Markov_matrix_sequential(pvalues['N'], p, pvalues['epsilon'])
        v = mc_compute_stationary(P)

        print '===='
        print 'Testing with prameter values set %d\n' % i
        print 'N =', pvalues['N'], ', epsilon =', pvalues['epsilon'], ', move =', pvalues['move'], '\n'
        if pvalues['N'] <= 5:
            print 'P =\n', P, '\n'
        print 'v =\n', v, '\n'
        print 'TOL =', TOL, '\n'

        yield MarkovMatrix(), P
        yield SumOne(), v
        yield Nonnegative(), v
        yield LeftEigenVec(), P, v
Beispiel #4
0
def test_markovchain_pmatrices():
    """
    Test the methods of MarkovChain, as well as mc_compute_stationary,
    with P matrix and known solutions
    """
    # Matrix with two recurrent classes [0, 1] and [3, 4, 5],
    # which have periods 2 and 3, respectively
    Q = np.zeros((6, 6))
    Q[0, 1], Q[1, 0] = 1, 1
    Q[2, [0, 3]] = 1/2
    Q[3, 4], Q[4, 5], Q[5, 3] = 1, 1, 1
    Q_stationary_dists = \
        np.array([[1/2, 1/2, 0, 0, 0, 0], [0, 0, 0, 1/3, 1/3, 1/3]])

    testset = [
        {'P': np.array([[0.4, 0.6], [0.2, 0.8]]),  # P matrix
         'stationary_dists': np.array([[0.25, 0.75]]),  # Known solution
         'comm_classes': [np.arange(2)],
         'rec_classes': [np.arange(2)],
         'is_irreducible': True,
         'period': 1,
         'is_aperiodic': True,
         'cyclic_classes': [np.arange(2)],
         },
        {'P': np.array([[0, 1], [1, 0]]),
         'stationary_dists': np.array([[0.5, 0.5]]),
         'comm_classes': [np.arange(2)],
         'rec_classes': [np.arange(2)],
         'is_irreducible': True,
         'period': 2,
         'is_aperiodic': False,
         'cyclic_classes': [np.array([0]), np.array([1])],
         },
        {'P': np.eye(2),
         'stationary_dists': np.array([[1, 0], [0, 1]]),
         'comm_classes': [np.array([0]), np.array([1])],
         'rec_classes': [np.array([0]), np.array([1])],
         'is_irreducible': False,
         'period': 1,
         'is_aperiodic': True,
         },
        {'P': Q,
         'stationary_dists': Q_stationary_dists,
         'comm_classes': [np.array([0, 1]), np.array([2]), np.array([3, 4, 5])],
         'rec_classes': [np.array([0, 1]), np.array([3, 4, 5])],
         'is_irreducible': False,
         'period': 6,
         'is_aperiodic': False,
         }
    ]

    # Loop Through TestSet #
    for test_dict in testset:
        mc = MarkovChain(test_dict['P'])
        computed = mc.stationary_distributions
        assert_allclose(computed, test_dict['stationary_dists'])

        assert(mc.num_communication_classes == len(test_dict['comm_classes']))
        assert(mc.is_irreducible == test_dict['is_irreducible'])
        assert(mc.num_recurrent_classes == len(test_dict['rec_classes']))
        list_of_array_equal(
            sorted(mc.communication_classes, key=lambda x: x[0]),
            sorted(test_dict['comm_classes'], key=lambda x: x[0])
        )
        list_of_array_equal(
            sorted(mc.recurrent_classes, key=lambda x: x[0]),
            sorted(test_dict['rec_classes'], key=lambda x: x[0])
        )
        assert(mc.period == test_dict['period'])
        assert(mc.is_aperiodic == test_dict['is_aperiodic'])
        try:
            list_of_array_equal(
                sorted(mc.cyclic_classes, key=lambda x: x[0]),
                sorted(test_dict['cyclic_classes'], key=lambda x: x[0])
            )
        except NotImplementedError:
            assert(mc.is_irreducible is False)

        # Test of mc_compute_stationary
        computed = mc_compute_stationary(test_dict['P'])
        assert_allclose(computed, test_dict['stationary_dists'])
Beispiel #5
0
                        default=default_TOL,
                        metavar='tol',
                        help=u'tol = tolerance')
    parser.add_argument("--move",
                        dest='move',
                        action='store',
                        default=default_move,
                        help=u'\'sequential\' (default) or \'simulataneous\'')
    args = parser.parse_args()

    if args.move == 'simultaneous':
        P = KMR_Markov_matrix_simultaneous(N=args.N,
                                           p=args.p,
                                           epsilon=args.epsilon)
    else:
        P = KMR_Markov_matrix_sequential(N=args.N,
                                         p=args.p,
                                         epsilon=args.epsilon)
    v = mc_compute_stationary(P)
    TOL = args.tol

    print 'N =', args.N, ', epsilon =', args.epsilon, '\n'

    if args.N <= 5:
        print 'P =\n', P, '\n'
    print 'v =\n', v, '\n'
    print 'TOL =', TOL, '\n'

    suite = unittest.TestLoader().loadTestsFromTestCase(TestComputeStationary)
    unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite)
    parser.add_argument(
        "--p", dest='p', action='store', default=default_p,
        metavar='p', help=u'p = level of p-dominance of action 1'
        )
    parser.add_argument(
        "--tolerance", type=float, dest='tol', action='store', default=default_TOL,
        metavar='tol', help=u'tol = tolerance'
        )
    parser.add_argument(
        "--move", dest='move', action='store', default=default_move,
        help=u'\'sequential\' (default) or \'simulataneous\''
        )
    args = parser.parse_args()

    if args.move == 'simultaneous':
        P = KMR_Markov_matrix_simultaneous(N=args.N, p=args.p, epsilon=args.epsilon)
    else:
        P = KMR_Markov_matrix_sequential(N=args.N, p=args.p, epsilon=args.epsilon)
    v = mc_compute_stationary(P)
    TOL = args.tol

    print 'N =', args.N, ', epsilon =', args.epsilon, '\n'

    if args.N <= 5:
        print 'P =\n', P, '\n'
    print 'v =\n', v, '\n'
    print 'TOL =', TOL, '\n'

    suite = unittest.TestLoader().loadTestsFromTestCase(TestComputeStationary)
    unittest.TextTestRunner(verbosity=2, stream=sys.stderr).run(suite)
Beispiel #7
0
        exp = np.dot(pay0, pr)
        if exp[0] > exp[1]:
            P[i][i-1] = num*(1-epsilon*0.5)
        elif exp[0] < exp[1]:
            P[i][i-1] = num*epsilon*0.5
        else:
            P[i][i-1] = num*0.5
    for i in range(n):
        num = fractions.Fraction(i, n)
        pr = np.array([1-num, num])
        exp = np.dot(pay0, pr)
        if exp[0] > exp[1]:
            P[i][i+1] = (1-num)*epsilon*0.5
        elif exp[0] < exp[1]:
            P[i][i+1] = (1-num)*(1-epsilon*0.5)
        else:
            P[i][i+1] = (1-num)*0.5
    return P

fig = plt.figure()
for val in epsilon:
    P = make_matrix(pay0, n, epsilon[val])
    stationary_dist = mc_compute_stationary(P)
    ax = plt.subplot(2,2,val)
    fig.add_subplot(ax)
    ax.hist(stationary_dist)
    plt.title('epsilon='+str(epsilon[val]))
    #plt.legend(str(epsilon[val]), loc = 1, prop={'size' : 8})
#plt.savefig('X_t_hist.png')
plt.show()
Beispiel #8
0
def test_markovchain_pmatrices():
    """
    Test the methods of MarkovChain, as well as mc_compute_stationary,
    with P matrix and known solutions
    """
    # Matrix with two recurrent classes [0, 1] and [3, 4, 5],
    # which have periods 2 and 3, respectively
    Q = np.zeros((6, 6))
    Q[0, 1], Q[1, 0] = 1, 1
    Q[2, [0, 3]] = 1 / 2
    Q[3, 4], Q[4, 5], Q[5, 3] = 1, 1, 1
    Q_stationary_dists = \
        np.array([[1/2, 1/2, 0, 0, 0, 0], [0, 0, 0, 1/3, 1/3, 1/3]])

    testset = [
        {
            'P': np.array([[0.4, 0.6], [0.2, 0.8]]),  # P matrix
            'stationary_dists': np.array([[0.25, 0.75]]),  # Known solution
            'comm_classes': [np.arange(2)],
            'rec_classes': [np.arange(2)],
            'is_irreducible': True,
            'period': 1,
            'is_aperiodic': True,
            'cyclic_classes': [np.arange(2)],
        },
        {
            'P': np.array([[0, 1], [1, 0]]),
            'stationary_dists': np.array([[0.5, 0.5]]),
            'comm_classes': [np.arange(2)],
            'rec_classes': [np.arange(2)],
            'is_irreducible': True,
            'period': 2,
            'is_aperiodic': False,
            'cyclic_classes': [np.array([0]), np.array([1])],
        },
        {
            'P': np.eye(2),
            'stationary_dists': np.array([[1, 0], [0, 1]]),
            'comm_classes': [np.array([0]), np.array([1])],
            'rec_classes': [np.array([0]), np.array([1])],
            'is_irreducible': False,
            'period': 1,
            'is_aperiodic': True,
        },
        {
            'P': Q,
            'stationary_dists': Q_stationary_dists,
            'comm_classes':
            [np.array([0, 1]),
             np.array([2]),
             np.array([3, 4, 5])],
            'rec_classes': [np.array([0, 1]),
                            np.array([3, 4, 5])],
            'is_irreducible': False,
            'period': 6,
            'is_aperiodic': False,
        }
    ]

    # Loop Through TestSet #
    for test_dict in testset:
        mc = MarkovChain(test_dict['P'])
        computed = mc.stationary_distributions
        assert_allclose(computed, test_dict['stationary_dists'])

        assert (mc.num_communication_classes == len(test_dict['comm_classes']))
        assert (mc.is_irreducible == test_dict['is_irreducible'])
        assert (mc.num_recurrent_classes == len(test_dict['rec_classes']))
        list_of_array_equal(
            sorted(mc.communication_classes, key=lambda x: x[0]),
            sorted(test_dict['comm_classes'], key=lambda x: x[0]))
        list_of_array_equal(
            sorted(mc.recurrent_classes, key=lambda x: x[0]),
            sorted(test_dict['rec_classes'], key=lambda x: x[0]))
        assert (mc.period == test_dict['period'])
        assert (mc.is_aperiodic == test_dict['is_aperiodic'])
        try:
            list_of_array_equal(
                sorted(mc.cyclic_classes, key=lambda x: x[0]),
                sorted(test_dict['cyclic_classes'], key=lambda x: x[0]))
        except NotImplementedError:
            assert (mc.is_irreducible is False)

        # Test of mc_compute_stationary
        computed = mc_compute_stationary(test_dict['P'])
        assert_allclose(computed, test_dict['stationary_dists'])