def test_sign(self):
        a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0])
        out = np.zeros(a.shape)
        tgt = np.array([1., -1., np.nan, 0.0, 1.0, -1.0])

        with np.errstate(invalid='ignore'):
            res = ncu.sign(a)
            assert_equal(res, tgt)
            res = ncu.sign(a, out)
            assert_equal(res, tgt)
            assert_equal(out, tgt)
Esempio n. 2
0
 def solve(self):
     s=0
     print 'solvinf for ', self.n_wires
     for wire1, wire2 in itertools.combinations(self.wire_tuples, r=2):
         left =  wire1[0] -  wire2[0]
         right =  wire1[1] -  wire2[1]
         if right == 0 or left == 0:
             continue
         if sign(left) != sign(right):
             s+=1
     return s
Esempio n. 3
0
    def test_sign(self):
        a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0])
        out = np.zeros(a.shape)
        tgt = np.array([1., -1., np.nan, 0.0, 1.0, -1.0])

        with np.errstate(invalid='ignore'):
            res = ncu.sign(a)
            assert_equal(res, tgt)
            res = ncu.sign(a, out)
            assert_equal(res, tgt)
            assert_equal(out, tgt)
Esempio n. 4
0
    def test_sign(self):
        a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0])
        out = np.zeros(a.shape)
        tgt = np.array([1., -1., np.nan, 0.0, 1.0, -1.0])

        olderr = np.seterr(invalid='ignore')
        try:
            res = ncu.sign(a)
            assert_equal(res, tgt)
            res = ncu.sign(a, out)
            assert_equal(res, tgt)
            assert_equal(out, tgt)
        finally:
            np.seterr(**olderr)
Esempio n. 5
0
    def test_sign(self):
        a = np.array([np.inf, -np.inf, np.nan, 0.0, 3.0, -3.0])
        out = np.zeros(a.shape)
        tgt = np.array([1.0, -1.0, np.nan, 0.0, 1.0, -1.0])

        olderr = np.seterr(invalid="ignore")
        try:
            res = ncu.sign(a)
            assert_equal(res, tgt)
            res = ncu.sign(a, out)
            assert_equal(res, tgt)
            assert_equal(out, tgt)
        finally:
            np.seterr(**olderr)
Esempio n. 6
0
    def test_transact_transaction_3(self):
        portfolio, dh, _ = self.get_portfolio_and_data_handler()
        contract = self.fut_contract
        ticker = portfolio.contract_ticker_mapper.contract_to_ticker(contract)

        # First transaction
        transaction_1 = Transaction(self.random_time, contract, quantity=50, price=200, commission=7)
        portfolio.transact_transaction(transaction_1)

        # Set new prices
        self.data_handler_prices = self.prices_series
        portfolio.update()

        # Get the new price of the contract
        new_price = dh.get_last_available_price(ticker)

        pnl_1 = (new_price - transaction_1.price) * transaction_1.quantity * transaction_1.contract.contract_size \
            - transaction_1.commission
        cash_move_1 = -transaction_1.commission

        self.assertEqual(portfolio.net_liquidation, self.initial_cash + pnl_1)
        self.assertEqual(portfolio.gross_exposure_of_positions,
                         transaction_1.quantity * contract.contract_size * new_price)
        self.assertEqual(portfolio.current_cash, self.initial_cash + cash_move_1)
        self.assertEqual(len(portfolio.open_positions_dict), 1)

        # Second transaction
        transaction_2 = Transaction(self.random_time, contract, quantity=-20, price=new_price, commission=15)
        portfolio.transact_transaction(transaction_2)
        portfolio.update()

        # Computed the realized pnl, which is already included in the portfolio current cash - it may not be the same as
        # the pnl of the trade, as the commission trades
        trade_size = min(abs(transaction_1.quantity), abs(transaction_2.quantity))

        realised_pnl = (-1) * transaction_1.price * trade_size * sign(transaction_1.quantity) * contract.contract_size
        realised_pnl += (-1) * transaction_2.price * trade_size * sign(transaction_2.quantity) * contract.contract_size
        realised_pnl -= transaction_2.commission  # Only transaction2 commission is yet realised

        self.assertEqual(portfolio.current_cash, self.initial_cash + cash_move_1 + realised_pnl)

        position = portfolio.open_positions_dict[contract]
        position_unrealised_pnl = position.unrealised_pnl
        self.assertEqual(portfolio.net_liquidation, portfolio.current_cash + position_unrealised_pnl)

        exposure = position.quantity() * contract.contract_size * new_price
        self.assertEqual(portfolio.gross_exposure_of_positions, position.total_exposure())
        self.assertEqual(exposure, position.total_exposure())
Esempio n. 7
0
    def fit(self,
            x,
            y,
            lr=1e-3,
            epochs=1e3,
            reg_rate=5,
            ratio=0.1,
            show_curve=False):
        n = len(x)
        lambda1 = reg_rate * ratio
        lambda2 = reg_rate * (1 - ratio)

        self.w = np.zeros((x.shape[1], y.shape[1]))
        self.b = np.zeros((1, y.shape[1]))
        losses = []
        for i in range(int(epochs)):
            y_hat = self.predict(x)
            loss = ols(y, y_hat) + lambda1 / (2 * n) * np.sum(abs(
                self.w)) + lambda2 / 2 * np.sum(self.w**2)
            losses.append(loss)
            self.w -= lr * (1 / n * x.T @ (y_hat - y) +
                            lambda1 * sign(self.w) + lambda2 * self.w)
            self.b -= lr * (1 / n * np.sum(y_hat - y))
        if show_curve:
            plt.plot(epochs, losses)
            plt.xlabel("Epochs")
            plt.ylabel("loss")
            plt.title("Training Curve")
Esempio n. 8
0
 def findEin(self):
     numberWrong = 0
     for index, z in enumerate(self.Z):
         y_expected = self.y[index]
         y = sign(dot(z, self.w.T))
         if y != y_expected:
             numberWrong += 1
     return numberWrong / 100.0
Esempio n. 9
0
 def findEout(self, numSamples = 1000):
     e_out = 0
     dataSamples = [self.createDataPoint() for _ in range(numSamples)]
     for x,y_expected in dataSamples:
         z = np.asarray(self.transformX(x))
         y = sign(dot(self.w.T,z))
         if y != y_expected:
             e_out += 1
     e_out /= float(numSamples)
     return e_out
Esempio n. 10
0
def testDigits(kTup=('rbf', 10)):
    '''
    测试基于SVM的手写数字识别系统
    :param kTup: 输入参数,元组类型
    :return:
    '''
    dataArr, labelArr = loadImages(os.path.dirname(os.getcwd()) +
                                   '\\datas\\digits\\trainingDigits')
    b, alphas = smoP(dataArr, labelArr, 200, 0.0001, 10000, kTup)
    datMat = mat(dataArr)
    labelMat = mat(labelArr).transpose()
    svInd = nonzero(alphas.A > 0)[0]
    sVs = datMat[svInd]
    labelSV = labelMat[svInd]

    print "there are %d Support Vectors" % shape(sVs)[0]
    m, n = shape(datMat)
    errorCount = 0

    for i in range(m):
        kernelEval = kernelTrans(sVs, datMat[i, :], kTup)
        predict = kernelEval.T * multiply(labelSV, alphas[svInd]) + b

        if sign(predict) != sign(labelArr[i]):
            errorCount += 1
    print "the training error rate is: %f" % (float(errorCount) / m)

    dataArr, labelArr = loadImages(os.path.dirname(os.getcwd()) +
                                   '\\datas\\digits\\testDigits')
    errorCount = 0
    datMat = mat(dataArr)
    labelMat = mat(labelArr).transpose()
    m, n = shape(datMat)

    for i in range(m):
        kernelEval = kernelTrans(sVs, datMat[i, :], kTup)
        predict = kernelEval.T * multiply(labelSV, alphas[svInd]) + b

        if sign(predict) != sign(labelArr[i]):
            errorCount += 1
    print "the test error rate is: %f" % (float(errorCount) / m)
Esempio n. 11
0
def testRbf(k1=1.3):
    '''
    利用核函数进行分类的径向基测试函数, 数据集表现为圆形分类,如figure_3.png
    :param k1: 输入参数,高斯径向基中的用户自定义变量
    :return:
    '''
    dataArr, labelArr = loadDataSet(os.path.dirname(os.getcwd()) +
                                    '\\datas\\testSetRBF.txt')
    b, alphas = smoP(dataArr, labelArr, 200, 0.0001, 10000, ('rbf', k1))
    datMat = mat(dataArr)
    labelMat = mat(labelArr).transpose()
    svInd = nonzero(alphas.A > 0)[0]
    sVs = datMat[svInd]  # 获取唯一的支持向量
    labelSV = labelMat[svInd]

    print "there are %d Support Vectors" % shape(sVs)[0]
    m, n = shape(datMat)
    errorCount = 0

    for i in range(m):
        kernelEval = kernelTrans(sVs, datMat[i, :], ('rbf', k1))
        predict = kernelEval.T * multiply(labelSV, alphas[svInd]) + b

        if sign(predict) != sign(labelArr[i]):
            errorCount += 1
    print "the training error rate is: %f" % (float(errorCount) / m)

    dataArr, labelArr = loadDataSet(os.path.dirname(os.getcwd()) +
                                    '\\datas\\testSetRBF2.txt')
    errorCount = 0
    datMat = mat(dataArr)
    labelMat = mat(labelArr).transpose()
    m, n = shape(datMat)

    for i in range(m):
        kernelEval = kernelTrans(sVs, datMat[i, :], ('rbf', k1))
        predict = kernelEval.T * multiply(labelSV, alphas[svInd]) + b

        if sign(predict) != sign(labelArr[i]):
            errorCount += 1
    print "the test error rate is: %f" % (float(errorCount) / m)
Esempio n. 12
0
def jacob_spinner_equations(m):
    print '\nМетод вращения Якоби на равенствах:\n'
    counter = 0
    a = m
    a_old = np.identity(3)

    print "A(k =", 0, ") \n"
    print_matrix(a)
    while not np.all(less_xs(np.abs(np.diagonal(a - a_old)))):
        a_old = a
        counter += 1
        i, j = find_max_non_diag_hardcode(a)
        maxx = a[i, j]

        d = np.sqrt(np.power(a[i, i] - a[j, j], 2) + 4 * np.power(a[i, j], 2))
        cosfi = np.sqrt(0.5 * (1 + np.abs(a[i, i] - a[j, j]) / d))
        sinfi = sign(a[i, j] * (a[i, i] - a[j, j])) * np.sqrt(
            0.5 * (1 - np.abs(a[i, i] - a[j, j]) / d))

        a = a * 0
        a[i, i] = (a_old[i, i] + a_old[j, j]) / 2 + sign(a_old[i, i] -
                                                         a_old[j, j]) * d / 2
        a[j, j] = (a_old[i, i] + a_old[j, j]) / 2 - sign(a_old[i, i] -
                                                         a_old[j, j]) * d / 2

        for k in range(a.shape[0]):
            for l in range(a.shape[1]):
                if (k != i) and (k != j) and (l != i) and (l != j):
                    a[k, l] = a_old[k, l]
                elif (k != i) and (k != j):
                    a[k, i] = cosfi * a_old[k, i] + sinfi * a_old[k, j]
                    a[i, k] = a[k, i]
                    a[k, j] = -sinfi * a_old[k, i] + cosfi * a_old[k, j]
                    a[j, k] = a[k, j]

        print "---------------------------------------------------------------------------"
        print "k = ", counter, "\t max elem = ", maxx
        print "A( k =", counter, ") = T^T * A( k = ", counter - 1, ") * T "
        print_matrix(a)
    return np.max(a)
def jacob_spinner_equations(m):
    print '\nМетод вращения Якоби на равенствах:\n'
    counter = 0
    a = m
    a_old = np.identity(3)

    print "A(k =", 0, ") \n"
    print_matrix(a)
    while not np.all(less_xs(np.abs(np.diagonal(a - a_old)))):
        a_old = a
        counter += 1
        i, j = find_max_non_diag_hardcode(a)
        maxx = a[i, j]

        d = np.sqrt( np.power(a[i, i] - a[j, j], 2) + 4 * np.power(a[i, j], 2) )
        cosfi = np.sqrt( 0.5 * (1 + np.abs(a[i, i] - a[j, j]) / d) )
        sinfi = sign(a[i, j] * (a[i, i] - a[j, j])) * np.sqrt( 0.5 * (1 - np.abs(a[i, i] - a[j, j]) / d) )

        a = a * 0
        a[i, i] = (a_old[i, i] + a_old[j, j]) / 2 + sign(a_old[i, i] - a_old[j, j]) * d / 2
        a[j, j] = (a_old[i, i] + a_old[j, j]) / 2 - sign(a_old[i, i] - a_old[j, j]) * d / 2

        for k in range(a.shape[0]):
            for l in range(a.shape[1]):
                if (k != i) and (k != j) and (l != i) and (l != j):
                    a[k, l] = a_old[k, l]
                elif (k != i) and (k != j):
                    a[k, i] = cosfi * a_old[k, i] + sinfi * a_old[k, j]
                    a[i, k] = a[k, i]
                    a[k, j] = -sinfi * a_old[k, i] + cosfi * a_old[k, j]
                    a[j, k] = a[k, j]


        print "---------------------------------------------------------------------------"
        print "k = ", counter, "\t max elem = ", maxx
        print "A( k =", counter, ") = T^T * A( k = ", counter - 1, ") * T "
        print_matrix(a)
    return np.max(a)
Esempio n. 14
0
 def best_direction(self, pos: Vec2d) -> None:
     deltax = pos.x() - self.client.player.position.x()
     deltay = pos.y() - self.client.player.position.y()
     if deltax > self.client.mapSize.x() / 2:
         deltax = self.client.mapSize.x() - deltax
     if deltay > self.client.mapSize.y() / 2:
         deltay = self.client.mapSize.y() - deltay
     # print(f'deltax {deltax}, deltay {deltay}')
     if abs(deltax) > abs(deltay):
         direction = 2 + -1 * sign(deltax)
     else:
         direction = 1 + -1 * sign(deltay)
     # print(f'should go {direction}, is looking at {self.client.player.orientation}')
     l_turns = (self.client.player.orientation - direction + 4) % 4
     r_turns = (direction - self.client.player.orientation + 4) % 4
     turns = min((l_turns, self.client.turn_left),
                 (r_turns, self.client.turn_right),
                 key=lambda x: x[0])
     if turns[0] == 0:
         self.client.move_forward()
         self.take_all()
     else:
         turns[1]()
Esempio n. 15
0
    def test_transact_transaction_split_position(self):
        portfolio, dh, _ = self.get_portfolio_and_data_handler()
        contract = self.fut_contract
        ticker = portfolio.contract_ticker_mapper.contract_to_ticker(contract)

        # Transact two transaction, which will result in transactions splitting
        quantity_after_first_transaction = 50
        quantity_after_second_transaction = -10
        initial_price = 200
        commission = 7

        # Transact the initial transaction
        transaction_1 = Transaction(self.random_time, contract,
                                    quantity_after_first_transaction,
                                    initial_price, commission)
        portfolio.transact_transaction(transaction_1)

        # Set new prices
        self.data_handler_prices = self.prices_series
        new_price = dh.get_last_available_price(ticker)  # == 250
        portfolio.update()

        # Transact the second transaction
        transaction_quantity = -quantity_after_first_transaction + quantity_after_second_transaction
        transaction_2 = Transaction(self.end_time, contract,
                                    transaction_quantity, new_price,
                                    commission)
        portfolio.transact_transaction(transaction_2)
        portfolio.update()

        # Compute the pnl of the position, which was closed
        quantity = max(abs(quantity_after_first_transaction),
                       abs(quantity_after_second_transaction))
        quantity *= sign(quantity_after_first_transaction)
        all_commissions = transaction_1.commission + transaction_2.commission
        pnl = (new_price - initial_price
               ) * quantity * contract.contract_size - all_commissions

        position = list(portfolio.open_positions_dict.values())[0]
        self.assertEqual(position.quantity(), -10)

        self.assertEqual(portfolio.current_cash, self.initial_cash + pnl)
        self.assertEqual(portfolio.net_liquidation, self.initial_cash + pnl)

        self.assertEqual(
            portfolio.gross_exposure_of_positions,
            abs(quantity_after_second_transaction * self.contract_size *
                new_price))
        self.assertEqual(len(portfolio.open_positions_dict), 1)
def toa_normalize(x0, y0):
    xdim = x0.shape[0]
    m = x0.shape[1]
    n = x0.shape[1]

    t = -x0[:, 1]
    x = x0 + np.tile(t, (1, m))
    y = y0 + np.tile(t, (1, n))

    qr_a = x[:, 2:(1 + xdim)]
    q, r = scipy.linalg.qr(qr_a)

    x = (q.conj().T) * x
    y = (q.conj().T) * y
    M = np.diag(sign(np.diag(qr_a)))
    x1 = M * x
    y1 = M * y

    return x1, y1
Esempio n. 17
0
def adaClassify(datToClass, classifierArr):
    '''
    利用训练得到的多个弱分类器进行分类
    :param datToClass: 一个或多个待分类样例
    :param classifierArr: 多个弱分类器组成的数组
    :return:
    '''
    dataMatrix = mat(datToClass)
    m = shape(dataMatrix)[0]
    aggClassEst = mat(zeros((m, 1)))

    for j in range(len(classifierArr)):
        classEst = stumpClassify(dataMatrix, classifierArr[int(j)]['dim'], \
                                 classifierArr[int(j)]['thresh'], \
                                 classifierArr[int(j)]['ineq'])
        aggClassEst += classifierArr[int(j)]['alpha'] * classEst
        print aggClassEst

    return sign(aggClassEst)
Esempio n. 18
0
    def get_weight(self):
        """Get weighting function for satellite image for overlaying"""

        weight = np.array([
            max((self.weight_width - min([
                abs(self.longitude - x),
                abs(self.longitude - x + 360),
                abs(self.longitude - x - 360)
            ])) / 180, 1e-7) for x in np.linspace(-180, 180, self.outwidth)
        ])

        weight = np.array([
            1 / (2.5 / 9) * weight * max(
                1e-7, -sign(self.latitude) *
                (i - self.outheight / 2) / self.outheight * 2 - 6.5 / 9)
            for i in range(self.outheight)
        ])

        return weight
Esempio n. 19
0
def adaBoostTrainDS(dataArr, classLabels, numIt=40):
    '''
    基于单层决策树的AdaBoost训练过程
    :param dataArr: 数据集
    :param classLabels: 类标签
    :param numIt: 迭代次数, 用户自定义指定
    :return: weakClassArr, 弱分类器集合;aggClassEst,每个数据点的类别估计累计值
    '''
    # 初始化
    weakClassArr = []
    m = shape(dataArr)[0]
    D = mat(ones((m, 1)) / m)  # 初始化概率分布向量,其元素之和为 1
    aggClassEst = mat(zeros((m, 1)))

    for i in range(numIt):
        # 构建单层决策树
        bestStump, error, classEst = buildStump(dataArr, classLabels, D)
        print "D:", D.T

        # alpha每个分类器配备的权重值, 计算公式:alpha = (1/2) * ln[(1-e) / e]
        alpha = float(0.5 * log((1.0 - error) / max(error, 1e-16)))
        bestStump['alpha'] = alpha
        weakClassArr.append(bestStump)  # 存储最佳决策树
        print "classEst: ", classEst.T

        # 更新权重向量D
        # 若正确分类,D[t + 1] = [D[t]*exp(-a) / sum(D)]
        # 若错误分类,D[t + 1] = [D[t]*exp(+a) / sum(D)]
        expon = multiply(-1 * alpha * mat(classLabels).T, classEst)
        D = multiply(D, exp(expon))  # Calc New D for next iteration
        D = D / D.sum()

        aggClassEst += alpha * classEst  # 更新累计类别估计值
        print "aggClassEst: ", aggClassEst.T
        aggErrors = multiply(sign(aggClassEst) != mat(classLabels).T, ones((m, 1)))
        errorRate = aggErrors.sum() / m  # 计算错误率
        print "total error: ", errorRate

        if errorRate == 0.0:
            break  # 为0, 退出循环

    return weakClassArr, aggClassEst
Esempio n. 20
0
    def get_weight(self):
        """Get weighting function for satellite image for overlaying"""

        weight = np.array([max((self.weight_width -
                                min([abs(self.longitude - x),
                                    abs(self.longitude - x + 360),
                                    abs(self.longitude - x - 360)])) / 180,
                               1e-7)
                           for x in np.linspace(-180,
                                                180,
                                                self.outwidth)])

        weight = np.array([1/(2.5/9) * weight *
                           max(1e-7,
                               -sign(self.latitude) *
                               (i - self.outheight/2)/self.outheight*2 -
                               6.5/9)
                           for i in range(self.outheight)])

        return weight
Esempio n. 21
0
    def _execute_order(self, order: OrderCommon) -> Fill:
        assert self.tick_num < len(self.ohlcv)

        if order.status != OrderStatus.Pending and not order.is_open():
            raise ValueError("expected order to be opened, but got " + str(order.status) + ". Order = \n"
                             + order.get_header() + "\n" + str(order))
        current_candle = self.current_candle()
        current_time = current_candle.name  # pd.Timestamp
        high = current_candle.high
        low = current_candle.low
        open = current_candle.open
        close = current_candle.close

        position = self.positions[order.symbol]  # type: PositionSim
        current_qty = position.signed_qty
        qty_fill = qty_to_close = outstanding_qty = None
        crossed = False

        if order.type is OrderType.Market:
            crossed = True
            price_fill = self._estimate_price()
            qty_fill = order.signed_qty
        elif order.type is OrderType.Limit:
            price_fill = order.price
            max_qty_fill = order.leaves_qty * order.side()
            # clip fill
            if (open <= order.price <= close) or (close <= order.price <= open):
                qty_fill = max_qty_fill
            elif high == low == order.price:
                qty_fill = round(0.5 * max_qty_fill)
            else:
                if low < order.price < high:
                    if order.is_sell():
                        factor = max((high - order.price) / (high - low), 0.50001)
                        assert factor >= 0
                    else:
                        factor = max((low - order.price) / (low - high), 0.50001)
                        assert factor >= 0
                    qty_fill = round(factor * max_qty_fill)
            if qty_fill is not None:
                crossed = True
        else:
            raise ValueError("order type " + str(order.type) + " not supported")

        if not crossed:
            return None  # type: Fill

        if position.is_open and position.would_change_side(qty_fill):
            qty_to_close = float(sign(qty_fill)) * min(abs(current_qty), abs(qty_fill))
            outstanding_qty = qty_fill - qty_to_close

        if order.fill(qty_fill) or order.type == OrderType.Market:
            order.status = OrderStatus.Filled
            order.fill_price = price_fill

        if order.price is not None and ((open <= order.price <= close) or (close <= order.price <= open)):
            assert order.status == OrderStatus.Filled

        fee = self.FEE[order.type]

        if outstanding_qty:
            position = self._update_position(order.symbol,
                                             qty=qty_to_close,
                                             price=price_fill,
                                             leverage=self.leverage[order.symbol],
                                             current_timestamp=current_time,
                                             fee=fee)
            assert not position.is_open

            position = self._update_position(order.symbol,
                                             qty=outstanding_qty,
                                             price=price_fill,
                                             leverage=self.leverage[order.symbol],
                                             current_timestamp=current_time,
                                             fee=fee)
            assert position.is_open
        else:
            self._update_position(order.symbol,
                                  qty=qty_fill,
                                  price=price_fill,
                                  leverage=self.leverage[order.symbol],
                                  current_timestamp=current_time,
                                  fee=fee)

        fill = Fill(order=order,
                    qty_filled=qty_fill,
                    price_fill=price_fill,
                    fill_time=current_time,
                    fill_type=FillType.complete if (order.status == OrderStatus.Filled) else FillType.partial)
        self.fills_hist += [fill]
        self.active_orders = drop_closed_orders_dict(self.active_orders)
        if self.can_call_handles:
            order.tactic.handle_fill(fill)
        return fill
Esempio n. 22
0
def f(x1, x2):
    return sign(x2 - x1 + (0.25 * sin(pi * x1)))