Example #1
0
    def testFit(self):
        """ Test submodel fitting on instationary data
        """
        np.random.seed(42)

        # original model coefficients
        b01 = np.array([[0.0, 0], [0, 0]])
        b02 = np.array([[0.5, 0.3], [0.3, 0.5]])
        b03 = np.array([[0.1, 0.1], [0.1, 0.1]])
        t, m, l = 10, 2, 100

        noisefunc = lambda: np.random.normal(size=(1, m))**3 / 1e3

        var = VAR(1)
        var.coef = b01
        sources1 = var.simulate([l, t], noisefunc)
        var.coef = b02
        sources2 = var.simulate([l, t], noisefunc)
        var.coef = b03
        sources3 = var.simulate([l, t * 2], noisefunc)

        sources = np.vstack([sources1, sources2, sources3])
        cl = [1] * t + [2] * t + [1, 2] * t

        var = VAR(1)
        r_trial = varica.cspvarica(sources,
                                   var,
                                   cl,
                                   reducedim=None,
                                   varfit='trial')
        r_class = varica.cspvarica(sources,
                                   var,
                                   cl,
                                   reducedim=None,
                                   varfit='class')
        r_ensemble = varica.cspvarica(sources,
                                      var,
                                      cl,
                                      reducedim=None,
                                      varfit='ensemble')

        vars = [
            np.var(r.var_residuals) for r in [r_trial, r_class, r_ensemble]
        ]

        # class one consists of trials generated with b01 and b03
        # class two consists of trials generated with b02 and b03
        #
        # ensemble fitting cannot resolve any model -> highest residual variance
        # class fitting cannot only resolve (b01+b03) vs (b02+b03) -> medium residual variance
        # trial fitting can resolve all three models -> lowest residual variance
        print(vars)

        self.assertLess(vars[0], vars[1])
        self.assertLess(vars[1], vars[2])
Example #2
0
    def testFit(self):
        """ Test submodel fitting on instationary data
        """
        np.random.seed(42)

        # original model coefficients
        b01 = np.array([[0.0, 0], [0, 0]])
        b02 = np.array([[0.5, 0.3], [0.3, 0.5]])
        b03 = np.array([[0.1, 0.1], [0.1, 0.1]])
        t, m, l = 10, 2, 100

        noisefunc = lambda: np.random.normal(size=(1, m)) ** 3 / 1e3

        var = VAR(1)
        var.coef = b01
        sources1 = var.simulate([l, t], noisefunc)
        var.coef = b02
        sources2 = var.simulate([l, t], noisefunc)
        var.coef = b03
        sources3 = var.simulate([l, t * 2], noisefunc)

        sources = np.vstack([sources1, sources2, sources3])
        cl = [1] * t + [2] * t + [1, 2] * t

        var = VAR(1)
        r_trial = varica.cspvarica(sources, var, cl, reducedim=None, varfit='trial')
        r_class = varica.cspvarica(sources, var, cl, reducedim=None, varfit='class')
        r_ensemble = varica.cspvarica(sources, var, cl, reducedim=None, varfit='ensemble')

        vars = [np.var(r.var_residuals) for r in [r_trial, r_class, r_ensemble]]

        # class one consists of trials generated with b01 and b03
        # class two consists of trials generated with b02 and b03
        #
        # ensemble fitting cannot resolve any model -> highest residual variance
        # class fitting cannot only resolve (b01+b03) vs (b02+b03) -> medium residual variance
        # trial fitting can resolve all three models -> lowest residual variance
        print(vars)

        self.assertLess(vars[0], vars[1])
        self.assertLess(vars[1], vars[2])
Example #3
0
#
# Here we cut out segments from 3s to 4s after each trigger. This is right in
# the middle of the motor imagery period.
data = cut_segments(raweeg, triggers, 3 * fs, 4 * fs)

m = 4  # number of sources to estimate
h = 66  # number of lags for whiteness test

i = 0
for p in [22, 33]:
    i += 1
    print("Model order:", p)

    print("    Performing CSPVARICA")
    var = scot.backend["var"](p)
    result = cspvarica(data, var, classes, m)

    if result.a.is_stable():
        s = ""
    else:
        s = "*NOT* "
    print("    VAR model is {}stable.".format(s))

    # discard the first p residuals
    # r = result.var_residuals[p:, :, :]

    print("    Testing VAR residuals for whiteness up to lag", h)
    pr = splot.plot_whiteness(result.a, h, repeats=100, axis=plt.subplot(2, 1, i))

    if pr < 0.05:
        plt.gca().set_title("model order {}: residuals significantly " "non-white with p={:f}".format(p, pr))
Example #4
0
#
# Here we cut out segments from 3s to 4s after each trigger. This is right in
# the middle of the motor imagery period.
data = cut_segments(raweeg, triggers, 3 * fs, 4 * fs)

m = 4  # number of sources to estimate
h = 66  # number of lags for whiteness test

i = 0
for p in [22, 33]:
    i += 1
    print('Model order:', p)

    print('    Performing CSPVARICA')
    var = scot.backend['var'](p)
    result = cspvarica(data, var, classes, m)

    if result.a.is_stable():
        s = ''
    else:
        s = '*NOT* '
    print('    VAR model is {}stable.'.format(s))

    # discard the first p residuals
    # r = result.var_residuals[p:, :, :]

    print('    Testing VAR residuals for whiteness up to lag', h)
    pr = splot.plot_whiteness(result.a,
                              h,
                              repeats=100,
                              axis=plt.subplot(2, 1, i))