예제 #1
0
    def test_close_at_6_sig_figs_exhaustive(self, M=6):
        for a in np.arange(-9.0, 9.0, 0.01):
            for offset in np.arange(5.000001e-6, 2 * 5.000001e-6, 1e-6):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                assert not isEqual

            for offset in np.arange(-5e-6, 5e-6 + EPS, 1e-6):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                if not isEqual:
                    print a
                    print a + offset
                assert isEqual
예제 #2
0
    def test_close_at_6_sig_figs_exhaustive(self, M=6):
        for a in np.arange(-9., 9., 0.01):
            for offset in np.arange(5.000001e-6, 2 * 5.000001e-6, 1e-6):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                assert not isEqual

            for offset in np.arange(-5e-6, 5e-6 + EPS, 1e-6):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                if not isEqual:
                    print a
                    print a + offset
                assert isEqual
예제 #3
0
    def test_close_at_3_sig_figs_exhaustive(self, M=3):
        for a in np.arange(-9., 9., 0.01):
            for offset in np.arange(0.005000001, 0.01, 0.001):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                assert not isEqual

            for offset in np.arange(-0.005000001, -0.01, -0.001):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                assert not isEqual

            for offset in np.arange(-0.005, 0.005 + EPS, 0.001):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                if not isEqual:
                    print a
                    print a + offset
                assert isEqual
예제 #4
0
    def test_close_at_3_sig_figs_exhaustive(self, M=3):
        for a in np.arange(-9.0, 9.0, 0.01):
            for offset in np.arange(0.005000001, 0.01, 0.001):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                assert not isEqual

            for offset in np.arange(-0.005000001, -0.01, -0.001):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                assert not isEqual

            for offset in np.arange(-0.005, 0.005 + EPS, 0.001):
                isEqual = closeAtMSigFigs(a, a + offset, M=M, tol=5)
                if not isEqual:
                    print a
                    print a + offset
                assert isEqual
예제 #5
0
    def verify_evidence(self, evBound=0.00001, prevBound=0, lapFrac=None):
        ''' Compare current and previous evidence (ELBO) values,
        verify that (within numerical tolerance) increases monotonically
    '''
        if np.isnan(evBound):
            raise ValueError("Evidence should never be NaN")
        if np.isinf(prevBound):
            return False
        isIncreasing = prevBound <= evBound
        M = self.algParams['convergeSigFig']
        isWithinTHR = closeAtMSigFigs(prevBound, evBound, M=M)
        mLPkey = 'doMemoizeLocalParams'
        if not isIncreasing and not isWithinTHR:
            serious = True
            if self.hasMove('birth') \
               and (len(self.BirthCompIDs) > 0 or len(self.ModifiedCompIDs) > 0):
                warnMsg = 'ev decreased during a birth'
                warnMsg += ' (so monotonic increase not guaranteed)\n'
                serious = False
            elif mLPkey in self.algParams and not self.algParams[mLPkey]:
                warnMsg = 'ev decreased when doMemoizeLocalParams=0'
                warnMsg += ' (so monotonic increase not guaranteed)\n'
                serious = False
            else:
                warnMsg = 'evidence decreased!\n'
            warnMsg += '    prev = % .15e\n' % (prevBound)
            warnMsg += '     cur = % .15e\n' % (evBound)
            if lapFrac is None:
                prefix = "WARNING: "
            else:
                prefix = "WARNING @ %.3f: " % (lapFrac)

            if serious or not self.algParams['doShowSeriousWarningsOnly']:
                Log.error(prefix + warnMsg)
        return isWithinTHR
예제 #6
0
  def verify_evidence(self, evBound=0.00001, prevBound=0, lapFrac=None):
    ''' Compare current and previous evidence (ELBO) values,
        verify that (within numerical tolerance) increases monotonically
    '''
    if np.isnan(evBound):
      raise ValueError("Evidence should never be NaN")
    if np.isinf(prevBound):
      return False
    isIncreasing = prevBound <= evBound
    M = self.algParams['convergeSigFig']
    isWithinTHR = closeAtMSigFigs(prevBound, evBound, M=M)
    mLPkey = 'doMemoizeLocalParams'
    if not isIncreasing and not isWithinTHR:
      serious = True
      if self.hasMove('birth') \
         and (len(self.BirthCompIDs) > 0 or len(self.ModifiedCompIDs) > 0):
        warnMsg = 'ev decreased during a birth'
        warnMsg += ' (so monotonic increase not guaranteed)\n'
        serious = False
      elif mLPkey in self.algParams and not self.algParams[mLPkey]:
        warnMsg = 'ev decreased when doMemoizeLocalParams=0'
        warnMsg += ' (so monotonic increase not guaranteed)\n'
        serious = False
      else:
        warnMsg = 'evidence decreased!\n'
      warnMsg += '    prev = % .15e\n' % (prevBound)
      warnMsg += '     cur = % .15e\n' % (evBound)
      if lapFrac is None:
        prefix = "WARNING: "
      else:
        prefix = "WARNING @ %.3f: " % (lapFrac)

      if serious or not self.algParams['doShowSeriousWarningsOnly']:
        Log.error(prefix + warnMsg)
    return isWithinTHR 
 def test_sovb_with_one_batch_equivalent_to_vb(self):
   if 'soVB' not in self.learnAlgs:
     raise SkipTest
   kwargs = self.get_kwargs()
   kwargs['nBatch'] = 1
   kwargs['rhoexp'] = 0
   __, __, vbInfo = run(self.Data, self.allocModelName,
                         self.obsModelName, 'VB', **kwargs)
   __, __, sovbInfo = run(self.Data, self.allocModelName,
                         self.obsModelName, 'soVB', **kwargs)
   vbEv = vbInfo['evTrace'][:-1]
   sovbEv = sovbInfo['evTrace']
   for ii in range(len(vbEv)):
     if hasattr(self, 'mustRetainLPAcrossLapsForGuarantees'):
       print vbEv[ii], sovbEv[ii]
       assert closeAtMSigFigs(vbEv[ii], sovbEv[ii], M=2)
     else:
       assert closeAtMSigFigs(vbEv[ii], sovbEv[ii], M=8)
예제 #8
0
 def test_sovb_with_one_batch_equivalent_to_vb(self):
     if 'soVB' not in self.learnAlgs:
         raise SkipTest
     kwargs = self.get_kwargs()
     kwargs['nBatch'] = 1
     kwargs['rhoexp'] = 0
     __, __, vbInfo = run(self.Data, self.allocModelName, self.obsModelName,
                          'VB', **kwargs)
     __, __, sovbInfo = run(self.Data, self.allocModelName,
                            self.obsModelName, 'soVB', **kwargs)
     vbEv = vbInfo['evTrace'][:-1]
     sovbEv = sovbInfo['evTrace']
     for ii in range(len(vbEv)):
         if hasattr(self, 'mustRetainLPAcrossLapsForGuarantees'):
             print vbEv[ii], sovbEv[ii]
             assert closeAtMSigFigs(vbEv[ii], sovbEv[ii], M=2)
         else:
             assert closeAtMSigFigs(vbEv[ii], sovbEv[ii], M=8)
예제 #9
0
    def test_vb_sovb_and_movb_all_estimate_evBound_in_same_ballpark(self):
        if 'moVB' not in self.learnAlgs:
            raise SkipTest
        kwargs = self.get_kwargs()
        kwargs['nBatch'] = 5
        __, __, vbInfo = run(self.Data, self.allocModelName, self.obsModelName,
                             'VB', **kwargs)
        __, __, movbInfo = run(self.Data, self.allocModelName,
                               self.obsModelName, 'moVB', **kwargs)
        __, __, sovbInfo = run(self.Data, self.allocModelName,
                               self.obsModelName, 'soVB', **kwargs)
        vbEv = vbInfo['evBound']
        movbEv = movbInfo['evBound']
        sovbEv = np.mean(sovbInfo['evTrace'][-10:])

        print vbEv
        print movbEv
        print sovbEv
        assert closeAtMSigFigs(vbEv, movbEv, M=2)
        assert closeAtMSigFigs(vbEv, sovbEv, M=2)
  def test_vb_sovb_and_movb_all_estimate_evBound_in_same_ballpark(self):
    if 'moVB' not in self.learnAlgs:
      raise SkipTest
    kwargs = self.get_kwargs()
    kwargs['nBatch'] = 5
    __, __, vbInfo = run(self.Data, self.allocModelName,
                          self.obsModelName, 'VB', **kwargs)
    __, __, movbInfo = run(self.Data, self.allocModelName,
                          self.obsModelName, 'moVB', **kwargs)
    __, __, sovbInfo = run(self.Data, self.allocModelName,
                          self.obsModelName, 'soVB', **kwargs)
    vbEv = vbInfo['evBound']
    movbEv = movbInfo['evBound']
    sovbEv = np.mean(sovbInfo['evTrace'][-10:])

    print vbEv
    print movbEv
    print sovbEv
    assert closeAtMSigFigs(vbEv, movbEv, M=2)
    assert closeAtMSigFigs(vbEv, sovbEv, M=2)
예제 #11
0
 def test_movb_with_one_batch_equivalent_to_vb(self):
     if 'moVB' not in self.learnAlgs:
         raise SkipTest
     kwargs = self.get_kwargs()
     kwargs['nBatch'] = 1
     __, __, vbInfo = run(self.Data, self.allocModelName, self.obsModelName,
                          'VB', **kwargs)
     __, __, movbInfo = run(self.Data, self.allocModelName,
                            self.obsModelName, 'moVB', **kwargs)
     vbEv = vbInfo['evTrace'][:-1]
     movbEv = movbInfo['evTrace']
     print vbEv
     print movbEv
     assert len(vbEv) == len(movbEv)
     for ii in range(len(vbEv)):
         assert closeAtMSigFigs(vbEv[ii], movbEv[ii], M=8)
 def test_movb_with_one_batch_equivalent_to_vb(self):
   if 'moVB' not in self.learnAlgs:
     raise SkipTest
   kwargs = self.get_kwargs()
   kwargs['nBatch'] = 1
   __, __, vbInfo = run(self.Data, self.allocModelName,
                         self.obsModelName, 'VB', **kwargs)
   __, __, movbInfo = run(self.Data, self.allocModelName,
                         self.obsModelName, 'moVB', **kwargs)
   vbEv = vbInfo['evTrace'][:-1]
   movbEv = movbInfo['evTrace']
   print vbEv
   print movbEv
   assert len(vbEv) == len(movbEv)
   for ii in range(len(vbEv)):
     assert closeAtMSigFigs(vbEv[ii], movbEv[ii], M=8)
  def test_vb_repeatable_when_continued(self):
    if 'VB' not in self.learnAlgs:
      raise SkipTest
    kwargs = self.get_kwargs_do_save_disk()
    kwargs['nLap'] = 10
    hmodel1, LP1, Info1 = run(self.Data, self.allocModelName,
                          self.obsModelName, 'VB', **kwargs)
    kwargs['nLap'] = 5
    kwargs['startLap'] = 5
    hmodel2, LP2, Info2 = continueRun(self.Data, self.allocModelName,
                          self.obsModelName, 'VB', **kwargs)
    if hasattr(self, 'mustRetainLPAcrossLapsForGuarantees'):
      print Info1['evTrace'][-1]
      print Info2['evTrace'][-1]
      assert closeAtMSigFigs(Info1['evTrace'][-1], Info2['evTrace'][-1], M=2)

    else:
      assert Info1['evTrace'][-1] == Info2['evTrace'][-1]
예제 #14
0
    def test_vb_repeatable_when_continued(self):
        if 'VB' not in self.learnAlgs:
            raise SkipTest
        kwargs = self.get_kwargs_do_save_disk()
        kwargs['nLap'] = 10
        hmodel1, LP1, Info1 = run(self.Data, self.allocModelName,
                                  self.obsModelName, 'VB', **kwargs)
        kwargs['nLap'] = 5
        kwargs['startLap'] = 5
        hmodel2, LP2, Info2 = continueRun(self.Data, self.allocModelName,
                                          self.obsModelName, 'VB', **kwargs)
        if hasattr(self, 'mustRetainLPAcrossLapsForGuarantees'):
            print Info1['evTrace'][-1]
            print Info2['evTrace'][-1]
            assert closeAtMSigFigs(Info1['evTrace'][-1],
                                   Info2['evTrace'][-1],
                                   M=2)

        else:
            assert Info1['evTrace'][-1] == Info2['evTrace'][-1]
예제 #15
0
 def test_close_at_2_sig_figs_boundary(self, M=2):
     assert closeAtMSigFigs(9.96, 9.98, M=M, tol=5)
     # once one arg goes about 10, the new threshold is 0.5, not 0.05
     assert closeAtMSigFigs(9.96, 10.01, M=M, tol=5)
     assert closeAtMSigFigs(9.96, 10.46, M=M, tol=5)
     assert not closeAtMSigFigs(9.96, 10.4601, M=M, tol=5)
예제 #16
0
    def test_close_at_2_sig_figs_readable(self, M=2):
        assert closeAtMSigFigs(1.42, 1.42, M=M, tol=5)
        assert closeAtMSigFigs(1.42, 1.47, M=M, tol=5)
        assert not closeAtMSigFigs(1.42, 1.4701, M=M, tol=5)
        assert closeAtMSigFigs(1.42, 1.37, M=M, tol=5)
        assert not closeAtMSigFigs(1.42, 1.36999, M=M, tol=5)

        assert closeAtMSigFigs(1.42e5, 1.47e5, M=M, tol=5)
        assert not closeAtMSigFigs(1.42e5, 1.4701e5, M=M, tol=5)

        assert closeAtMSigFigs(1.42e5, 1.37e5, M=M, tol=5)
        assert not closeAtMSigFigs(1.42e5, 1.3699e5, M=M, tol=5)

        assert closeAtMSigFigs(-1.42e2, -1.47e2, M=M, tol=5)
        assert not closeAtMSigFigs(-1.42e2, -1.4701e2, M=M, tol=5)

        assert closeAtMSigFigs(-1.42e2, -1.37e2, M=M, tol=5)
        assert not closeAtMSigFigs(-1.42e2, -1.369999e2, M=M, tol=5)
예제 #17
0
    def test_close_at_2_sig_figs_readable(self, M=2):
        assert closeAtMSigFigs(1.42, 1.42, M=M, tol=5)
        assert closeAtMSigFigs(1.42, 1.47, M=M, tol=5)
        assert not closeAtMSigFigs(1.42, 1.4701, M=M, tol=5)
        assert closeAtMSigFigs(1.42, 1.37, M=M, tol=5)
        assert not closeAtMSigFigs(1.42, 1.36999, M=M, tol=5)

        assert closeAtMSigFigs(1.42e5, 1.47e5, M=M, tol=5)
        assert not closeAtMSigFigs(1.42e5, 1.4701e5, M=M, tol=5)

        assert closeAtMSigFigs(1.42e5, 1.37e5, M=M, tol=5)
        assert not closeAtMSigFigs(1.42e5, 1.3699e5, M=M, tol=5)

        assert closeAtMSigFigs(-1.42e2, -1.47e2, M=M, tol=5)
        assert not closeAtMSigFigs(-1.42e2, -1.4701e2, M=M, tol=5)

        assert closeAtMSigFigs(-1.42e2, -1.37e2, M=M, tol=5)
        assert not closeAtMSigFigs(-1.42e2, -1.369999e2, M=M, tol=5)
예제 #18
0
 def test_close_at_2_sig_figs_boundary(self, M=2):
     assert closeAtMSigFigs(9.96, 9.98, M=M, tol=5)
     # once one arg goes about 10, the new threshold is 0.5, not 0.05
     assert closeAtMSigFigs(9.96, 10.01, M=M, tol=5)
     assert closeAtMSigFigs(9.96, 10.46, M=M, tol=5)
     assert not closeAtMSigFigs(9.96, 10.4601, M=M, tol=5)