Example #1
0
 def hand_color_detect(self, frame):
     from copy import copy
     frame = copy(frame)
     # 0 blau, 1 grun, 2 rot
     # return frame[:, :, 2]
     res1 = scipy.less(frame[:, :, 1], frame[:, :, 2])
     res2 = scipy.less(frame[:, :, 0], frame[:, :, 1])
     self.hand_colored_frame = (255 * scipy.logical_and(res1, res2)).astype(np.uint8)
     self.hand_colored_frame = cv2.erode(self.hand_colored_frame, None, iterations=2)
     self.hand_colored_frame = cv2.dilate(self.hand_colored_frame, None, iterations=3)
     return self.hand_colored_frame
    def _check_bounds(self,x_new):
        # If self.bounds_error = 1, we raise an error if any x_new values
        # fall outside the range of x.  Otherwise, we return an array indicating
        # which values are outside the boundary region.  
        # !! Needs some work for multi-dimensional x !!
        below_bounds = less(x_new,self.x[0])
        above_bounds = greater(x_new,self.x[-1])
        #  Note: sometrue has been redefined to handle length 0 arrays
        # !! Could provide more information about which values are out of bounds
        # RHC -- Changed these ValueErrors to PyDSTool_BoundsErrors
        if self.bounds_error and any(sometrue(below_bounds)):
##            print "Input:", x_new
##            print "Bound:", self.x[0]
##            print "Difference input - bound:", x_new-self.x[0]
            raise PyDSTool_BoundsError, " A value in x_new is below the"\
                              " interpolation range."
        if self.bounds_error and any(sometrue(above_bounds)):
##            print "Input:", x_new
##            print "Bound:", self.x[-1]
##            print "Difference input - bound:", x_new-self.x[-1]
            raise PyDSTool_BoundsError, " A value in x_new is above the"\
                              " interpolation range."
        # !! Should we emit a warning if some values are out of bounds.
        # !! matlab does not.
        out_of_bounds = logical_or(below_bounds,above_bounds)
        return out_of_bounds
    def Function(self, t, param):
        Ktrans, ve = param
        duration   = self.duration
        delay      = self.delay
        cp0        = self.cp0


        # If step imput is assumed, the respons can be described as
        #
        #   y = (Ktrans * cp0 / kep) * (1-exp(-kep*t))         (t < duration)   ... (1)
        #   y = (Ktrans * cp0 / kep) * exp(-kep*(t-duration))  (t >= duration)  ... (2)

        #sys.stderr.write('t     : %s\n' % t )


        # Calculate shifted time
        t2 = scipy.greater_equal(t, delay) * (t - delay);

        # To describe C(t) in one equasion, we introduce t_dush, which is
        # defined by t_dash = t (t < duration) and t_dash = duration (t >= duration):
        t_dash = scipy.less(t2, duration)*t2 + scipy.greater_equal(t2, duration)*duration

        # Eq. (1) and (2) can be rewritten by:
        kep = Ktrans / ve
        y = (Ktrans*cp0/kep)*(scipy.exp(kep*t_dash) - 1) * scipy.exp(-kep*t2)

        return y
Example #4
0
 def _check_bounds(self, x_new):
     # If self.bounds_error = 1, we raise an error if any x_new values
     # fall outside the range of x.  Otherwise, we return an array indicating
     # which values are outside the boundary region.
     # !! Needs some work for multi-dimensional x !!
     below_bounds = less(x_new, self.x[0])
     above_bounds = greater(x_new, self.x[-1])
     #  Note: sometrue has been redefined to handle length 0 arrays
     # !! Could provide more information about which values are out of bounds
     # RHC -- Changed these ValueErrors to PyDSTool_BoundsErrors
     if self.bounds_error and any(sometrue(below_bounds)):
         ##            print "Input:", x_new
         ##            print "Bound:", self.x[0]
         ##            print "Difference input - bound:", x_new-self.x[0]
         raise PyDSTool_BoundsError, " A value in x_new is below the"\
                           " interpolation range."
     if self.bounds_error and any(sometrue(above_bounds)):
         ##            print "Input:", x_new
         ##            print "Bound:", self.x[-1]
         ##            print "Difference input - bound:", x_new-self.x[-1]
         raise PyDSTool_BoundsError, " A value in x_new is above the"\
                           " interpolation range."
     # !! Should we emit a warning if some values are out of bounds.
     # !! matlab does not.
     out_of_bounds = logical_or(below_bounds, above_bounds)
     return out_of_bounds
Example #5
0
def stopGrow(informationGain_, data_, feature_used_):
    ylabels = []
    for instance in data_:
        ylabels.append(instance[-1])
    informationGain_ = np.array(informationGain_)
    informationGain_ = informationGain_[~np.isnan(informationGain_)]
    if len(set(ylabels)) == 1 or len(data_) < m or all(
            sp.less(informationGain_, 0)) or all(feature_used_):
        return True
    return False
Example #6
0
def stoppingPoint(dataFrame):
    if len(dataFrame) < m:
        return True
    elif dataFrame is None:
        return True
    elif isPure(dataFrame):
        return True
    itm = determineCandidateSplits(dataFrame)
    gain = []
    #print(itm)
    for i in itm:
        gain.append(i[2])
    gain = np.array(gain)
    if all(sp.less(gain, 0)):
        return True
    return False
Example #7
0
def dN(x):
    ''' Cumulative Normal Density Function of a standard normal random variable
    using continued fractions expansion of the normal CDF.
    '''

    ones = sp.less(x, 0)  # determine xi <0
    negs = -1 * ones + ~ones  # create vector of -1 and 1 to reverse sign of F(|x|)
    x = abs(x)

    # Set the variables here for ease of reading later
    f = N(x)
    z = 1 / (1 + 0.2316419 * x)
    a1 = 0.319381530
    a2 = -0.356563782
    a3 = 1.781477937
    a4 = -1.821255978
    a5 = 1.330274429

    F = 1 - f * z * ((((a5 * z + a4) * z + a3) * z + a2) * z + a1)
    F = ones + negs * F  # if xi <0 F(xi) = 1 - F(-xi)

    return F
 def compute_by_noise_pow(self,signal,n_pow):
     s_spec = sp.fft(signal*self._window)
     s_amp = sp.absolute(s_spec)
     s_phase = sp.angle(s_spec)
     gamma = self._calc_aposteriori_snr(s_amp,n_pow)
     xi = self._calc_apriori_snr(gamma)
     self._prevGamma = gamma
     nu = gamma * xi / (1.0+xi)
     self._G = (self._gamma15*sp.sqrt(nu)/gamma)*sp.exp(-nu/2.0)* ((1.0+nu)*spc.i0(nu/2.0)+nu*spc.i1(nu/2.0))
     idx = sp.less(s_amp**2.0,n_pow)
     self._G[idx] = self._constant
     idx = sp.isnan(self._G) + sp.isinf(self._G)
     self._G[idx] = xi[idx] / ( xi[idx] + 1.0)
     idx = sp.isnan(self._G) + sp.isinf(self._G)
     self._G[idx] = self._constant
     self._G = sp.maximum(self._G,0.0)
     amp = self._G * s_amp
     amp = sp.maximum(amp,0.0)
     amp2 = self._ratio*amp + (1.0-self._ratio)*s_amp
     self._prevAmp = amp
     spec = amp2 * sp.exp(s_phase*1j)
     return sp.real(sp.ifft(spec))
 def compute_by_noise_pow(self,signal,n_pow):
     s_spec = sp.fft(signal *self._window)
     s_amp = sp.absolute(s_spec)
     s_phase = sp.angle(s_spec)
     gamma = self._calc_aposteriori_snr(s_amp,n_pow)
     #xi = self._calc_apriori_snr2(gamma,n_pow)
     xi = self._calc_apriori_snr(gamma)
     self._prevGamma = gamma
     u = 0.5 - self._mu/(4.0*sp.sqrt(gamma*xi))
     self._G = u + sp.sqrt(u**2.0 + self._tau/(gamma*2.0))
     idx = sp.less(s_amp**2.0,n_pow)
     self._G[idx] = self._constant
     idx = sp.isnan(self._G) + sp.isinf(self._G)
     self._G[idx] = xi[idx] / ( xi[idx] + 1.0)
     idx = sp.isnan(self._G) + sp.isinf(self._G)
     self._G[idx] = self._constant
     self._G = sp.maximum(self._G,0.0)
     amp = self._G * s_amp
     amp = sp.maximum(amp,0.0)
     amp2 = self._ratio*amp + (1.0-self._ratio)*s_amp
     self._prevAmp = amp
     spec = amp2 * sp.exp(s_phase*1j)
     return sp.real(sp.ifft(spec))
Example #10
0
def stoppingGrowing(data_, infomationGain_, feature_used_):
    def setIsPure(data_):
        labels = []
        # collect all class labels
        for instance in data_:
            labels.append(instance[-1])
        labels = set(labels)
        # check if set cardinality == 1
        if len(labels) == 1:
            return True
        return False

    # remove nan value
    infomationGain_ = np.array(infomationGain_)
    infomationGain_ = infomationGain_[~np.isnan(infomationGain_)]

    # (i) all of the training instances reaching the node belong to the same class
    # (ii) number of training instances reaching the node < m
    # (iii) no feature has positive information gain
    # (iv) there are no more remaining candidate splits at the node.
    if setIsPure(data_) or len(data_) < m or all(sp.less(
            infomationGain_, 0)) or all(feature_used_):
        return True
    return False
    def plot_rc(self,
                save=False,
                hand=True,
                usgs=True,
                xs=True,
                xsapprox=True,
                sprnt=True,
                ci=True,
                dist=5000,
                raw=False,
                kind='power',
                alpha=0.05,
                div=5):
        """Plot HAND and xs rating curves with confidence intervals
		'hand' - plot hand rating curve [T/F]
		'xs' - plot xs rating curves [T/F]
		'xsapprox' - plot xs rating curve approximation from n-value averages [T/F]
		'ci' - plot confidence intervals [T/F]
		'alpha' - alpha for confidence intervals [float(0.0,1.0)]
		'div' - number of intervals for confidence interval [R]"""

        with open('results/output_{0}.csv'.format(self.comid), 'w') as f:
            writer = csv.writer(f)
            writer.writerow(['COMID:', self.comid])
            writer.writerow(['LENGTH:', self.handlen])

            if xs:  # Plot all linearly-interpolated XS rating curves
                intervals = scipy.arange(dist, self.handlen + dist, dist)
                # print 'Intervals:',intervals

                cutoffub = [i / self.handlen * 100 for i in intervals]
                cutofflb = scipy.copy(cutoffub)
                cutofflb = scipy.insert(cutofflb, 0, 0)[:-1]
                cutoffs = zip(cutofflb, cutoffub)
                for l, u in cutoffs:
                    idx = scipy.where(
                        scipy.logical_and(
                            scipy.greater_equal(self.xs_profs, l),
                            scipy.less(self.xs_profs, u)))
                    if u > 100: u = 100.00

                    if len(self.xs_disch[idx]) == 0:
                        continue

                    fig, ax = plt.subplots(
                    )  # get figure and axes for plotting
                    fname = 'results/sprntcompare/rc_comid_{0}_sprnt_compare.png'.format(
                        self.comid, ('%.2f' % l), ('%.2f' % u))

                    for prof, disch, stage in zip(self.xs_profs[idx],
                                                  self.xs_disch[idx],
                                                  self.xs_stage[idx]):
                        # Get interpolation function
                        # print (('%.2f' % prof) + str(disch))
                        # print (('%.2f' % prof) + str(stage))

                        f = self.interp(x=disch, y=stage, kind=kind)

                        if raw == True:  # Plot raw data (ie. only HEC-RAS points)
                            # interp over discharge
                            writer.writerow(['PROFILE:', prof])
                            writer.writerow(['DISCHARGE:'])
                            writer.writerow(disch)
                            writer.writerow(['STAGE:'])
                            writer.writerow(f(disch))
                            ax.plot(disch, f(disch), c='grey', linewidth=2)

                            # interp over stage (switched axes) for testing
                            # f = self.interp(x=stage,y=disch,kind=kind)
                            # ax.plot(f(stage),stage,c='purple',linewidth=1)

                        if raw == False:  # Plot interpolated data (ie. 'div' many interpolated points)
                            interval = disch[-1] / div
                            qvals = scipy.arange(0, (disch[-1] + interval),
                                                 interval)  # [1:]
                            writer.writerow(['PROFILE:', prof])
                            writer.writerow(['DISCHARGE:'])
                            writer.writerow(qvals)
                            writer.writerow(['STAGE:'])
                            writer.writerow(f(qvals))
                            ax.plot(qvals, f(qvals), c='grey', linewidth=2)

                    # print '\n------------------------\n'

            if ci:  # Plot confidence interval bounds
                self.get_ci(alpha=alpha,
                            div=div)  # get upper- and lower-bound variables
                # upper bounds
                f = self.interp(x=self.ci_vals, y=self.ubounds, kind=kind)
                writer.writerow(['UPPER CI:'])
                writer.writerow(['DISCHARGE:'])
                writer.writerow(self.ci_vals)
                writer.writerow(['STAGE:'])
                writer.writerow(f(self.ci_vals))
                ax.plot(self.ci_vals,
                        f(self.ci_vals),
                        label='{0}%CI'.format(int((1 - alpha) * 100)),
                        c='orange',
                        linewidth=5)
                # lower bounds
                f = self.interp(x=self.ci_vals, y=self.lbounds, kind=kind)
                writer.writerow(['LOWER CI:'])
                writer.writerow(['DISCHARGE:'])
                writer.writerow(self.ci_vals)
                writer.writerow(['STAGE:'])
                writer.writerow(f(self.ci_vals))
                ax.plot(self.ci_vals, f(self.ci_vals), c='orange', linewidth=5)

            if xsapprox:
                # Add approximate rating curve from average n-values
                qvals, hvals = self.get_xs_q(upto=83)
                f = self.interp(x=qvals, y=hvals, kind=kind)
                writer.writerow(['XSAPPROX:'])
                writer.writerow(['DISCHARGE:'])
                writer.writerow(qvals)
                writer.writerow(['STAGE:'])
                writer.writerow(f(qvals))
                ax.plot(qvals,
                        f(qvals),
                        label='Resistance Function',
                        c='red',
                        linewidth=5)

            if usgs:  # Plot interpolated USGS rating curve
                # Get data
                try:
                    self.get_usgsrc()  # Fetch usgs stage and disch values
                    # Plot curves
                    for q, h in zip(self.usgsq, self.usgsh):
                        if kind == 'cubic':
                            print 'USGS interpolation plotted as power-law fit'
                            f = self.interp(x=q, y=h, kind='power')
                        else:
                            f = self.interp(x=q, y=h, kind=kind)
                        writer.writerow(['USGS:'])
                        writer.writerow(['DISCHARGE:'])
                        writer.writerow(q)
                        writer.writerow(['STAGE:'])
                        writer.writerow(f(q))
                        ax.plot(q, f(q), label='usgs', c='g', linewidth=5)
                except IndexError:
                    print 'No USGS rating curve for comid {0}'.format(
                        self.comid)

            if hand:  # Plot interpolated HAND rating curve
                # Plot curves
                f = self.interp(x=self.handq, y=self.handh, kind=kind)
                writer.writerow(['HAND:'])
                writer.writerow(['DISCHARGE:'])
                writer.writerow(list(self.handq))
                writer.writerow(['STAGE:'])
                writer.writerow(list(f(self.handq)))
                ax.plot(self.handq,
                        f(self.handq),
                        label='hand',
                        c='b',
                        linewidth=5)

            if sprnt:
                f = self.interp(x=self.sprntq, y=self.sprnth, kind=kind)
                writer.writerow(['SPRNT:'])
                writer.writerow(['DISCHARGE:'])
                writer.writerow(list(self.sprntq))
                writer.writerow(['STAGE:'])
                writer.writerow(list(f(self.sprntq)))
                ax.plot(self.sprntq,
                        f(self.sprntq),
                        label='sprnt',
                        c='y',
                        linewidth=5)

            # Add one label for all cross-section curves
            ax.plot([], [], label='HEC-RAS', c='grey', linewidth=2)
            # Plot graph
            fig.set_size_inches(20, 16, forward=True)
            plt.gca().set_xlim(left=0, right=self.max_disch)
            plt.gca().set_ylim(bottom=0, top=self.max_stage)
            ax.set_xticks(ax.get_xticks()[::2])
            ax.set_yticks(ax.get_yticks()[::2])
            title = 'COMID {0}, ({1},{2})'.format(self.comid, ('%.2f' % l),
                                                  ('%.2f' % u))
            ax.set_title(title, y=1.04, fontsize=56)
            plt.xlabel('Q (cfs)', fontsize=56)
            plt.ylabel('H (ft)', fontsize=56)
            ax.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
            plt.rc('font', size=56)
            plt.legend(loc='upper left', fontsize=40)
            plt.tick_params(axis='both', labelsize=56)
            plt.grid()

            if save:
                fig.savefig(fname)
                plt.clf()

            if not save:
                mng = plt.get_current_fig_manager()
                mng.resize(*mng.window.maxsize())
                plt.show()
                plt.clf()
            writer.writerow('')
    def plot_rc(self,
                save=False,
                xs=True,
                xsapprox=True,
                kind='power',
                dist=5000,
                raw=False,
                alpha=0.05,
                div=5,
                box=False):
        """Plot HAND and xs rating curves with confidence intervals
		'hand' - plot hand rating curve [T/F]
		'xs' - plot xs rating curves [T/F]
		'xsapprox' - plot xs rating curve approximation from n-value averages [T/F]
		'ci' - plot confidence intervals [T/F]
		'alpha' - alpha for confidence intervals [float(0.0,1.0)]
		'div' - number of intervals for confidence interval [R]"""

        if xs:  # Plot all linearly-interpolated XS rating curves
            intervals = scipy.arange(dist, self.handlen + dist, dist)
            # print 'Intervals:',intervals

            cutoffub = [i / self.handlen * 100 for i in intervals]
            cutofflb = scipy.copy(cutoffub)
            cutofflb = scipy.insert(cutofflb, 0, 0)[:-1]
            cutoffs = zip(cutofflb, cutoffub)
            for l, u in cutoffs:
                idx = scipy.where(
                    scipy.logical_and(scipy.greater_equal(self.xs_profs, l),
                                      scipy.less(self.xs_profs, u)))[0]
                if u > 100: u = 100.00

                fig, ax = plt.subplots()  # get figure and axes for plotting
                fname = 'results/by5000/{0}/rc__comid_{0}_from_{1}_to_{2}.png'.format(
                    self.comid, ('%.2f' % l), ('%.2f' % u))

                for prof, disch, stage in zip(self.xs_profs[idx],
                                              self.xs_disch[idx],
                                              self.xs_stage[idx]):
                    # Get interpolation function
                    # print (('%.2f' % prof) + str(disch))
                    # print (('%.2f' % prof) + str(stage))

                    f = self.interp(x=disch, y=stage, kind=kind)

                    if raw == True:  # Plot raw data (ie. only HEC-RAS points)
                        # interp over discharge
                        ax.plot(disch, f(disch), c='grey', linewidth=2)

                        # interp over stage (switched axes) for testing
                        # f = self.interp(x=stage,y=disch,kind=kind)
                        # ax.plot(f(stage),stage,c='purple',linewidth=1)

                    if raw == False:  # Plot interpolated data (ie. 'div' many interpolated points)
                        interval = disch[-1] / div
                        qvals = scipy.arange(0, (disch[-1] + interval),
                                             interval)  # [1:]
                        ax.plot(qvals, f(qvals), c='grey', linewidth=2)

                # Add one label for all cross-section curves
                ax.plot([], [], label='HEC-RAS', c='grey', linewidth=2)
                # Plot graph
                fig.set_size_inches(20, 16, forward=True)
                plt.gca().set_xlim(left=0, right=self.max_disch)
                plt.gca().set_ylim(bottom=0, top=self.max_stage)
                ax.set_xticks(ax.get_xticks()[::2])
                ax.set_yticks(ax.get_yticks()[::2])
                title = 'COMID {0}, ({1},{2})'.format(self.comid, ('%.2f' % l),
                                                      ('%.2f' % u))
                ax.set_title(title, y=1.04, fontsize=56)
                plt.xlabel('Q (cfs)', fontsize=56)
                plt.ylabel('H (ft)', fontsize=56)
                ax.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
                plt.rc('font', size=56)
                plt.legend(loc='upper left', fontsize=40)
                plt.tick_params(axis='both', labelsize=56)
                plt.grid()

                # print '\n------------------------\n'
                if xsapprox:
                    # Add approximate rating curve from average n-values
                    qvals, hvals = self.get_xs_q(low=0, upto=83)
                    f = self.interp(x=qvals, y=hvals, kind=kind)
                    ax.plot(qvals,
                            f(qvals),
                            label='Resistance Function',
                            c='red',
                            linewidth=5)

                    # Add approximate rating curve for these indices
                    idxqvals, idxhvals = self.get_xs_q(low=idx[0],
                                                       upto=idx[-1])

                    if len(idxqvals) == 0:
                        print 'No data found for profiles {0} to {1}'.format(
                            ('%.2f' % l), ('%.2f' % u))
                        break

                    # f = self.interp(x=idxqvals,y=idxhvals,kind=kind)
                    # ax.plot(idxqvals,f(idxqvals),label='Resistance Function Local Average',c='orange',linewidth=5)

        else:
            fig, ax = plt.subplots()

        if save: fig.savefig(fname)
        else:
            # mng = plt.get_current_fig_manager()
            # mng.resize(*mng.window.maxsize())
            plt.show()
Example #13
0
def run_simulation_fast(vol, lam, sprd_client, sprd_dealer, delta_lim,
                        hedge_style, dt, nsteps, nruns, seed):
    '''Runs a Monte Carlo simulation and returns statics on PNL, client trades, and hedge trades.
    "_fast" because it uses vectorized operations.

    vol:         lognormal volatility of the spot process
    lam:         Poisson process frequency
    sprd_client: fractional bid/ask spread for client trades. eg 1e-4 means 1bp.
    sprd_dealer: fractional bid/ask spread for inter-dealer hedge trades. eg 1e-4 means 1bp.
    delta_lim:   the delta limit at or beyond which the machine will hedge in the inter-dealer market
    hedge_style: 'Zero' or 'Edge', defining the hedging style. 'Zero' means hedge to zero position,
                 'Edge' means hedge to the nearer delta limit.
    dt:          length of a time step
    nsteps:      number of time steps for each run of the simulation
    nruns:       number of Monte Carlo runs
    seed:        RNG seed
    '''

    scipy.random.seed(seed)

    trade_prob = 1 - exp(-lam * dt)
    sqrtdt = sqrt(dt)

    spots = scipy.zeros(nruns) + 1  # initial spot == 1
    posns = scipy.zeros(nruns)
    trades = scipy.zeros(nruns)
    hedges = scipy.zeros(nruns)
    pnls = scipy.zeros(nruns)

    for step in range(nsteps):
        dzs = scipy.random.normal(0, sqrtdt, nruns)
        qs = scipy.random.uniform(0, 1, nruns)
        ps = scipy.random.binomial(
            1, 0.5,
            nruns) * 2 - 1  # +1 or -1 - trade quantities if a trade happens

        # check if there are client trades for each path

        indics = scipy.less(qs, trade_prob)
        posns += indics * ps
        trades += scipy.ones(nruns) * indics
        pnls += scipy.ones(nruns) * indics * sprd_client * spots / 2.

        # check if there are hedges to do for each path

        if hedge_style == 'Zero':
            indics = scipy.logical_or(scipy.less_equal(posns, -delta_lim),
                                      scipy.greater_equal(posns, delta_lim))
            pnls -= scipy.absolute(posns) * indics * sprd_dealer * spots / 2.
            posns -= posns * indics
            hedges += scipy.ones(nruns) * indics
        elif hedge_style == 'Edge':
            # first deal with cases where pos>delta_lim

            indics = scipy.greater(posns, delta_lim)
            pnls -= (posns - delta_lim) * indics * sprd_dealer * spots / 2.
            posns = posns * scipy.logical_not(indics) + scipy.ones(
                nruns) * indics * delta_lim
            hedges += scipy.ones(nruns) * indics

            # then the cases where pos<-delta_lim

            indics = scipy.less(posns, -delta_lim)
            pnls -= (-delta_lim - posns) * indics * sprd_dealer * spots / 2.
            posns = posns * scipy.logical_not(indics) + scipy.ones(
                nruns) * indics * (-delta_lim)
            hedges += scipy.ones(nruns) * indics
        else:
            raise ValueError('hedge_style must be "Edge" or "Zero"')

        # advance the spots and calculate period PNL

        dspots = vol * spots * dzs
        pnls += posns * dspots
        spots += dspots

    return {
        'PNL': (pnls.mean(), pnls.std()),
        'Trades': (trades.mean(), trades.std()),
        'Hedges': (hedges.mean(), hedges.std())
    }
    def compute(self, frame, lamda):
        Y_lambda = sp.fft(frame * self._window)
        #eq9
        alpha_c_lambda_tilde = 1.0 / (
            1.0 + (sp.sum(self._P_lambda) / sp.sum(sp.absolute(Y_lambda)**2) -
                   1.0)**2)
        #eq10
        self._alpha_c_lambda = 0.7 * self._alpha_c_lambda + 0.3 * sp.maximum(
            alpha_c_lambda_tilde, 0.7)
        #eq11
        alpha_lambda_hat = (self._alpha_max * self._alpha_c_lambda) / (
            1 + (self._P_lambda / self._sn2_lambda - 1)**2)
        #eq12
        snr = sp.sum(self._P_lambda) / sp.sum(self._sn2_lambda)
        alpha_lambda_hat = sp.maximum(alpha_lambda_hat,
                                      sp.minimum(0.3, snr**self._snrexp))
        #eq4 smoothed periodgram
        self._P_lambda = alpha_lambda_hat * self._P_lambda + (
            1.0 - alpha_lambda_hat) * (sp.absolute(Y_lambda)**2)
        #eq20
        beta_lambda = sp.minimum(alpha_lambda_hat**2, self._beta_max)
        self._eP_lambda = beta_lambda * self._eP_lambda + (
            1.0 - beta_lambda) * self._P_lambda
        self._eP2_lambda = beta_lambda * self._eP2_lambda + (
            1.0 - beta_lambda) * (self._P_lambda**2)
        #eq22
        vP_lambda = self._eP2_lambda - (self._eP_lambda**2)
        #eq23 modification
        Qeq_lambda_inverse = sp.maximum(
            sp.minimum(vP_lambda / (2 * (self._sn2_lambda**2)), 0.5),
            self._qeqimin / (lamda + 1))

        #eq23 + 12 lines
        eQ_lambda = sp.sum(Qeq_lambda_inverse) / self._winsize
        #eq23 + 11 lines
        Bc_lambda = 1.0 + self._av * sp.sqrt(eQ_lambda)

        # ----------------------------------------------------------------------------------------
        # for overall window of length D
        # ----------------------------------------------------------------------------------------

        #eq16
        M, H = M_H(self._D)
        Qeq_lambda_tilde = (1.0 / Qeq_lambda_inverse - 2 * M) / (1 - M)
        #eq17
        Bmin_lambda = 1.0 + (self._D - 1) * 2.0 / Qeq_lambda_tilde

        # ----------------------------------------------------------------------------------------
        # for subwindow U of length V
        # ----------------------------------------------------------------------------------------

        #eq16
        M, H = M_H(self._V)
        Qeq_lambda_tilde_sub = (1.0 / Qeq_lambda_inverse - 2 * M) / (1 - M)
        #eq17
        Bmin_lambda_sub = 1.0 + (self._V - 1) * 2.0 / Qeq_lambda_tilde_sub

        # ----------------------------------------------------------------------------------------

        #set k_mod(k) = 0 for all frequency bin k
        k_mod = sp.zeros(self._winsize)

        #calc actmin
        actmin_lambda_current = self._P_lambda * Bmin_lambda * Bc_lambda
        #if(P*Bmin*Bc < actmin)
        k_mod = sp.less(actmin_lambda_current, self._actmin_lambda)
        if (k_mod.any()):
            # update new minimum of D
            self._actmin_lambda[k_mod] = actmin_lambda_current[k_mod]
            # update new minimum of V
            actmin_lambda_current_sub = self._P_lambda * Bmin_lambda_sub * Bc_lambda
            self._actmin_lambda_sub[k_mod] = actmin_lambda_current_sub[k_mod]

        if (0 < self._subwc and self._subwc < self._V - 1):
            #sample is NOT the fisrt or the last; middle of buffer allows a local minimum
            self._lmin_flag_lambda = sp.minimum(self._lmin_flag_lambda + k_mod,
                                                1)
            #calc Pmin_u_lamda == sigma_n_lamda_hat**2
            self._Pmin_u_lambda = sp.minimum(self._actmin_lambda_sub,
                                             self._Pmin_u_lambda)
            #store sn2 for later use
            self._sn2_lambda = self._Pmin_u_lambda
            self._subwc = self._subwc + 1
        elif (self._subwc >= self._V - 1):
            #sample IS the last; end of buffer lets finishing subwindow process and a buffer switch
            self._lmin_flag_lambda = sp.maximum(self._lmin_flag_lambda - k_mod,
                                                0)
            #store actmin_lamnda, note actbuf is NOT cyclic!
            self._ibuf = sp.mod(self._ibuf, self._U)
            self._actbuf[self._ibuf] = self._actmin_lambda
            self._ibuf = self._ibuf + 1
            #find Pmin_u, the minimum of the last U stored value of actmin
            self._Pmin_u_lambda = self._actbuf.min(axis=0)

            #calc noise_slope_max
            if (eQ_lambda < 0.03):
                noise_slope_max = 8.0
            elif (eQ_lambda < 0.05):
                noise_slope_max = 4.0
            elif (eQ_lambda < 0.06):
                noise_slope_max = 2.0
            else:
                noise_slope_max = 1.2
            #replace all previously stored values of actmin by actminsub
            lmin = self._lmin_flag_lambda * sp.less(
                self._actmin_lambda_sub,
                noise_slope_max * self._Pmin_u_lambda) * sp.less(
                    self._Pmin_u_lambda, self._actmin_lambda_sub)
            lmin = sp.int16(lmin)
            if (lmin.any()):
                self._Pmin_u_lambda[lmin] = self._actmin_lambda_sub[lmin]
                r = sp.ones((self._U, self._winsize))
                rp = r * self._Pmin_u_lambda
                self._actbuf[:, lmin] = rp[:, lmin]

            #update flags
            self._lmin_flag_lambda = sp.zeros(self._winsize)
            self._actmin_lambda = sp.ones(self._winsize) * self._clear_max
            self._subwc = 0
        else:
            self._subwc = self._subwc + 1

        x = self._sn2_lambda
        #stderror; paper [2], Fig 15; not use
        #qisq = sp.sqrt(Qeq_lambda_inverse)
        #xs = x*sp.sqrt(0.266*(self._D+100*qisq)*qisq/(1+0.005*self._D+6.0/self._D)/(0.5*(1.0/Qeq_lambda_inverse)+self._D-1))
        self._x_data.append(x)

        self._x_data_all.append(
            10 * sp.log10(sp.sum(sp.absolute(x)) / self._winsize))
        self._p_data_all.append(
            10 * sp.log10(sp.sum(self._P_lambda) / self._winsize))
        self._y_data.append(sp.absolute(Y_lambda)**2)
        self._y_data_all.append(
            10 * sp.log10(sp.sum(sp.absolute(Y_lambda)**2) / self._winsize))
        return x
    def compute(self,frame,lamda):
        Y_lambda = sp.fft(frame*self._window)
        #eq9
        alpha_c_lambda_tilde = 1.0 / ( 1.0 + ( sp.sum(self._P_lambda)/ sp.sum(sp.absolute(Y_lambda)**2) - 1.0 )**2 )
        #eq10
        self._alpha_c_lambda = 0.7*self._alpha_c_lambda + 0.3*sp.maximum(alpha_c_lambda_tilde, 0.7)
        #eq11
        alpha_lambda_hat = (self._alpha_max*self._alpha_c_lambda) / ( 1 + (self._P_lambda/self._sn2_lambda - 1)**2 )
        #eq12
        snr = sp.sum(self._P_lambda)/sp.sum(self._sn2_lambda)
        alpha_lambda_hat = sp.maximum(alpha_lambda_hat, sp.minimum(0.3, snr**self._snrexp))
        #eq4 smoothed periodgram
        self._P_lambda = alpha_lambda_hat*self._P_lambda + (1.0-alpha_lambda_hat)*(sp.absolute(Y_lambda)**2)
        #eq20
        beta_lambda = sp.minimum(alpha_lambda_hat**2, self._beta_max)
        self._eP_lambda = beta_lambda*self._eP_lambda + (1.0-beta_lambda)*self._P_lambda;
        self._eP2_lambda = beta_lambda*self._eP2_lambda + (1.0-beta_lambda)*(self._P_lambda**2)
        #eq22
        vP_lambda = self._eP2_lambda-(self._eP_lambda**2)
        #eq23 modification
        Qeq_lambda_inverse = sp.maximum(sp.minimum(vP_lambda/(2*(self._sn2_lambda**2)),0.5),self._qeqimin/(lamda+1))

        #eq23 + 12 lines
        eQ_lambda = sp.sum(Qeq_lambda_inverse)/self._winsize
        #eq23 + 11 lines
        Bc_lambda = 1.0 + self._av*sp.sqrt(eQ_lambda)

        # ----------------------------------------------------------------------------------------
        # for overall window of length D
        # ----------------------------------------------------------------------------------------

        #eq16
        M,H = M_H(self._D)
        Qeq_lambda_tilde = (1.0/Qeq_lambda_inverse - 2*M)/(1-M)
        #eq17
        Bmin_lambda = 1.0 + (self._D-1)*2.0/Qeq_lambda_tilde

        # ----------------------------------------------------------------------------------------
        # for subwindow U of length V
        # ----------------------------------------------------------------------------------------

        #eq16
        M,H = M_H(self._V)
        Qeq_lambda_tilde_sub = (1.0/Qeq_lambda_inverse - 2*M)/(1-M)
        #eq17
        Bmin_lambda_sub = 1.0 + (self._V-1)*2.0/Qeq_lambda_tilde_sub

        # ----------------------------------------------------------------------------------------

        #set k_mod(k) = 0 for all frequency bin k
        k_mod = sp.zeros(self._winsize)

        #calc actmin
        actmin_lambda_current = self._P_lambda*Bmin_lambda*Bc_lambda
        #if(P*Bmin*Bc < actmin)
        k_mod = sp.less(actmin_lambda_current, self._actmin_lambda)
        if(k_mod.any()):
            # update new minimum of D
            self._actmin_lambda[k_mod] = actmin_lambda_current[k_mod]
            # update new minimum of V
            actmin_lambda_current_sub = self._P_lambda*Bmin_lambda_sub*Bc_lambda
            self._actmin_lambda_sub[k_mod] = actmin_lambda_current_sub[k_mod]

        if(0 < self._subwc and self._subwc < self._V-1):
            #sample is NOT the fisrt or the last; middle of buffer allows a local minimum
            self._lmin_flag_lambda = sp.minimum(self._lmin_flag_lambda + k_mod, 1)
            #calc Pmin_u_lamda == sigma_n_lamda_hat**2
            self._Pmin_u_lambda = sp.minimum(self._actmin_lambda_sub, self._Pmin_u_lambda)
            #store sn2 for later use
            self._sn2_lambda = self._Pmin_u_lambda
            self._subwc = self._subwc + 1
        elif(self._subwc >= self._V-1):
            #sample IS the last; end of buffer lets finishing subwindow process and a buffer switch
            self._lmin_flag_lambda = sp.maximum(self._lmin_flag_lambda - k_mod, 0)
            #store actmin_lamnda, note actbuf is NOT cyclic!
            self._ibuf = sp.mod(self._ibuf,self._U)
            self._actbuf[self._ibuf] = self._actmin_lambda
            self._ibuf = self._ibuf + 1
            #find Pmin_u, the minimum of the last U stored value of actmin
            self._Pmin_u_lambda = self._actbuf.min(axis=0)

            #calc noise_slope_max
            if(eQ_lambda < 0.03):
                noise_slope_max = 8.0
            elif(eQ_lambda < 0.05):
                noise_slope_max = 4.0
            elif(eQ_lambda < 0.06):
                noise_slope_max = 2.0
            else:
                noise_slope_max = 1.2
            #replace all previously stored values of actmin by actminsub
            lmin = self._lmin_flag_lambda * sp.less(self._actmin_lambda_sub,noise_slope_max*self._Pmin_u_lambda) * sp.less(self._Pmin_u_lambda,self._actmin_lambda_sub)
            lmin = sp.int16(lmin)
            if(lmin.any()):
                self._Pmin_u_lambda[lmin] = self._actmin_lambda_sub[lmin]
                r = sp.ones((self._U,self._winsize))
                rp = r*self._Pmin_u_lambda
                self._actbuf[:,lmin] = rp[:,lmin]

            #update flags
            self._lmin_flag_lambda = sp.zeros(self._winsize)
            self._actmin_lambda = sp.ones(self._winsize)*self._clear_max
            self._subwc = 0
        else:
            self._subwc = self._subwc + 1

        x  = self._sn2_lambda
        #stderror; paper [2], Fig 15; not use
        #qisq = sp.sqrt(Qeq_lambda_inverse)
        #xs = x*sp.sqrt(0.266*(self._D+100*qisq)*qisq/(1+0.005*self._D+6.0/self._D)/(0.5*(1.0/Qeq_lambda_inverse)+self._D-1))
        self._x_data.append(x)

        self._x_data_all.append(10*sp.log10(sp.sum(sp.absolute(x))/self._winsize))
        self._p_data_all.append(10*sp.log10(sp.sum(self._P_lambda)/self._winsize))
        self._y_data.append(sp.absolute(Y_lambda)**2)
        self._y_data_all.append(10*sp.log10(sp.sum(sp.absolute(Y_lambda)**2)/self._winsize))
        return x
Example #16
0
 def reconstruction(self, probs):
     U = np.random.uniform(
         0, 1, [probs.shape[0], probs.shape[1], self.visible_binom_n])
     visible_rebuilt = 0 + sp.less(
         U, probs.reshape([probs.shape[0], probs.shape[1], -1]))
     return (visible_rebuilt.sum(2))
Example #17
0
 def hidden_sampling(self, probs):
     U = np.random.uniform(0, 1, probs.shape)
     hidden = 0 + sp.less(U, probs)
     return (hidden)