def value_iteration(self, agent, max_iterator=-1):
        iteration = 0
        while True:
            iteration += 1
            new_value_pi = np.zeros_like(agent.value_pi)
            value_sas = []
            for i in range(1, agent.s_len):
                for j in range(agent.a_len):
                    value_sa = np.dot(agent.p[j, i, :],
                                      agent.r + agent.gamma * agent.value_pi)
                    value_sas.append(value_sa)
                new_value_pi[i] = max(value_sas)
            diff = np.sqrt(np.sum(np.square(agent.value_pi - new_value_pi)))
            if diff < 1e-6:
                break
            else:
                agent.value_pi = new_value_pi
            if iteration == max_iterator:
                break

        print('Iteration converged at round %d' % iteration)

        for i in range(1, agent.s_len):
            for j in range(0, agent.a_len):
                agent.value_q[i, j] = np.dot(
                    agent.p[i, j], agent.r + agent.gamma * agent.value_pi)
            max_act = np.argmax(agent.value_q[i, :])
            agent.pi[i] = max_act
Example #2
0
 def fit(self, x, y, w=None):
     if w is None:
         w = np.ones(len(y)) / len(y)
     data = zip(x, y, w)
     data = sorted(data, key=lambda s: s[0])
     [x, y, w] = zip(*data)
     y = np.array(y)
     w = np.array(w)
     correct = np.zeros((2, len(y)))  # 0 row for x < v, 1 row for x >= v
     for i in range(len(y)):
         w_front = w[:i]
         w_back = w[i:]
         correct[0, i] += np.sum(w_front[y[:i] == 1]) + np.sum(
             w_back[y[i:] == -1])
         correct[1, i] += np.sum(w_front[y[:i] == -1]) + np.sum(
             w_back[y[i:] == 1])
     idx = np.argmax(correct, axis=1)
     if correct[0, int(idx[0])] > correct[1, int(idx[1])]:
         self.sign = "smaller"
         self.thres = x[idx[0]]
     else:
         self.sign = "equal to or bigger"
         self.thres = x[idx[1]]
Example #3
0
def auto_corr(comp_vc, p_wf, t_wf):
    """Function computes the autocorrelation function from given vectors
    Args:
        wf_n(numpy array, complex): Wave function over time

    Returns:
        numpy array, complex: The autocorrelation function over time.
    """

    # autocorrelation fuction
    ac_file = np.zeros([p_wf, t_wf + 1], dtype=np.complex_)
    for n in range(p_wf):
        ac_file[n] = np.sum(comp_vc[:, 0] * np.conjugate(comp_vc[:, n]))
    return ac_file
Example #4
0
 def fit(self, X, y, verbose=False):
     weights = np.ones(len(y)) / len(
         y)  # initial the weights for each data sample
     accuracy = 0
     epoch = 0
     while 1 - accuracy > self.epsilon or epoch <= self.iters:
         # stop when acc is okay or epoch number is larger than iters
         epoch += 1
         weak_learner = weak_learner_unit()
         alpha = AdaBoost.calcAlpha(weak_learner.score(X, y, weights))
         self.alphas.append(alpha)
         self.weak_learners_list.append(weak_learner)
         # update the weights by w :=
         weights = weights * np.exp(-alpha * y * self.predict(X))
         weights = weights / np.sum(weights)
         accuracy = self.accuracy(X, y)
Example #5
0
def getRetRange(rets, naLower, naUpper, naExpected="False", s_type="long"):
    """
    @summary Returns the range of possible returns with upper and lower bounds on the portfolio participation
    @param rets: Expected returns
    @param naLower: List of lower percentages by stock
    @param naUpper: List of upper percentages by stock
    @return tuple containing (fMin, fMax)
    """

    # Calculate theoretical minimum and maximum theoretical returns """
    fMin = 0
    fMax = 0

    rets = deepcopy(rets)

    if naExpected == "False":
        naExpected = np.average(rets, axis=0)

    na_signs = np.sign(naExpected)
    indices, = np.where(na_signs == 0)
    na_signs[indices] = 1
    if s_type == "long":
        na_signs = np.ones(len(na_signs))
    elif s_type == "short":
        na_signs = np.ones(len(na_signs)) * (-1)

    rets = na_signs * rets
    naExpected = na_signs * naExpected

    naSortInd = naExpected.argsort()

    # First add the lower bounds on portfolio participation """
    for i, fRet in enumerate(naExpected):
        fMin = fMin + fRet * naLower[i]
        fMax = fMax + fRet * naLower[i]

    # Now calculate minimum returns"""
    # allocate the max possible in worst performing equities """
    # Subtract min since we have already counted it """
    naUpperAdd = naUpper - naLower
    fTotalPercent = np.sum(naLower[:])
    for i, lInd in enumerate(naSortInd):
        fRetAdd = naUpperAdd[lInd] * naExpected[lInd]
        fTotalPercent = fTotalPercent + naUpperAdd[lInd]
        fMin = fMin + fRetAdd
        # Check if this additional percent puts us over the limit """
        if fTotalPercent > 1.0:
            fMin = fMin - naExpected[lInd] * (fTotalPercent - 1.0)
            break

    # Repeat for max, just reverse the sort, i.e. high to low """
    naUpperAdd = naUpper - naLower
    fTotalPercent = np.sum(naLower[:])
    for i, lInd in enumerate(naSortInd[::-1]):
        fRetAdd = naUpperAdd[lInd] * naExpected[lInd]
        fTotalPercent = fTotalPercent + naUpperAdd[lInd]
        fMax = fMax + fRetAdd

        # Check if this additional percent puts us over the limit """
        if fTotalPercent > 1.0:
            fMax = fMax - naExpected[lInd] * (fTotalPercent - 1.0)
            break

    return (fMin, fMax)
'''
numpy.sum函数和python的求和函数功能基本⼀致,但是还是有⼀些⼩区别:
numpy的求和函数具有维度的概念,求和多项可以是数组
同时参数含义跟python的sum并不⼀致
numpy.sum速度快⼀些
'''
from numpy import np

a = np.arange(100).reshape((10, 10))
b = np.sum(a)
print(b)
Example #7
0
 def score(self, x, y, w=None):  # the wrong percent
     if w is None:
         w = np.ones(len(y)) / len(y)
     return 1 - np.sum(w[self.predict(x) == y])
Example #8
0
 def accuracy(self, x, y):
     acc = np.sum(self.predict(x) == y) / len(y)
     return acc