コード例 #1
0
    def train_single(self, input_vector, target_vector):
        """
        input_vector and target_vector can be tuple, 
        list or ndarray
        """

        input_vector = np.array(input_vector, ndmin=2).T
        target_vector = np.array(target_vector, ndmin=2).T

        output_vector1 = np.dot(self.weights_in_hidden, input_vector)
        output_hidden = Activation.reLU(output_vector1)

        output_vector2 = np.dot(self.weights_hidden_output, output_hidden)
        #output_network = Activation.sigmoid(output_vector2)
        output_network = Activation.reLU(output_vector2)

        output_errors = target_vector - output_network
        # update the weights:
        #tmp = output_errors * Derivative.sigmoid(output_network)
        tmp = output_errors * Derivative.reLU(output_network)
        tmp = self.learning_rate * np.dot(tmp, output_hidden.T)
        self.weights_hidden_output += tmp
        # calculate hidden errors:
        hidden_errors = np.dot(self.weights_hidden_output.T, output_errors)
        # ----------------------------------------------------------------------
        # update the weights:
        tmp = hidden_errors * Derivative.reLU(output_hidden)
        # -----------------------------------------------------------------------
        self.weights_in_hidden += self.learning_rate * np.dot(
            tmp, input_vector.T)
コード例 #2
0
    def test_p_plus_change_positive(self):
        q1 = Quantity(None, Derivative(DValue.PLUS))
        q2 = Quantity(None, Derivative(DValue.ZERO))

        r.propotionalPositive(q1, q2)

        self.assertEqual(q2.derivative.value, DValue.PLUS)
コード例 #3
0
    def test_isStateValid_multiRelation_valid(self):
        state = {
            "B": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
        }
        relations = [
            {
                "type": "EX",
                "args": None,
                "Q1": ("B", "Q"),
                "Q2": ("B", "Q"),
            },
            {
                "type": "I-",
                "args": None,
                "Q1": ("C", "Q"),
                "Q2": ("B", "Q"),
            },
            {
                "type": "P+",
                "args": None,
                "Q1": ("B", "Q"),
                "Q2": ("C", "Q"),
            },
        ]

        self.assertEqual(isStateValid(state, relations), True)
コード例 #4
0
 def test_isStateValid_relationOrder(self):
     state = {
         "A": {
             "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO))
         },
         "B": {
             "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
         },
         "C": {
             "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
         },
     }
     relations = [
         {
             "type": "I+",
             "args": None,
             "Q1": ("A", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "I-",
             "args": None,
             "Q1": ("C", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "P+",
             "args": None,
             "Q1": ("B", "Q"),
             "Q2": ("C", "Q"),
         },
     ]
     self.assertEqual(isStateValid(state, relations), False)
コード例 #5
0
    def train_single(self, input_vector, target_vector):
        """"
            Forward Propagation
            input:  input_vector  [784], target_vector [10]
            H1:     output_vector [100], weights_in_hidden[100, 784], output_hidden[100]
            output: output_vector2 [10], weight_hidden_output[10, 100], output_network[10]
            loss scalar 2.3

            Backward Propagation
            gradient [10], derived [10], tmp2 [10], hidden_errors [100,10], tmp5 [100,10]
            
        """
        input_vector = np.array(input_vector, ndmin=2).T
        target_vector = np.array(target_vector, ndmin=2).T

        output_vector1 = np.dot(self.weights_in_hidden, input_vector)
        output_hidden = Activation.leakyReLU(output_vector1)

        output_vector2 = np.dot(self.weights_hidden_output, output_hidden)
        output_network = Activation.leakyReLU(output_vector2)

        loss = Cross_Entropy.calc(output_network, target_vector)
        gradient = Cross_Entropy.derived_calc(output_network, target_vector)
        # update the weights:
        derived1 = Derivative.leakyReLU(gradient)
        tmp2 = derived1 * (loss * gradient)
        # calculate hidden errors:
        hidden_errors = np.dot(self.weights_hidden_output.T, loss * derived1)
        # update the weights:
        tmp5 = hidden_errors * Derivative.leakyReLU(output_hidden)
        self.weights_hidden_output += self.learning_rate * np.dot(
            tmp2, output_hidden.T)
        self.weights_in_hidden += self.learning_rate * np.dot(
            tmp5, input_vector.T)
コード例 #6
0
    def test_p_plus_copy_value(self):
        q1 = Quantity(None, Derivative(DValue.PLUS))
        q2 = Quantity(None, Derivative(DValue.ZERO))

        # apply proportion relation
        r.propotionalPositive(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.PLUS)
        # change q1 derivative without affecting q2 derivative
        q1.derivative.value = DValue.MINUS
        self.assertEqual(q2.derivative.value, DValue.PLUS)
コード例 #7
0
    def test_isStateValid_multiRelation_sameTail_ambiguous(self):
        state1 = {
            "A": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "B": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
            },
        }
        state2 = {
            "A": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "B": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
            },
        }
        state3 = {
            "A": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.MINUS))
            },
            "B": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.PLUS))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.PLUS), Derivative(DValue.ZERO))
            },
        }
        relations = [
            {
                "type": "EX",
                "args": None,
                "Q1": ("A", "Q"),
                "Q2": ("A", "Q"),
            },
            {
                "type": "P+",
                "args": None,
                "Q1": ("A", "Q"),
                "Q2": ("B", "Q"),
            },
            {
                "type": "I+",
                "args": None,
                "Q1": ("C", "Q"),
                "Q2": ("B", "Q"),
            },
        ]

        self.assertEqual(isStateValid(state1, relations), True)
        self.assertEqual(isStateValid(state2, relations), True)
        self.assertEqual(isStateValid(state3, relations), True)
コード例 #8
0
def main():
    securities = [
        Security(30, "$", RiskLevel.LOW, Trend.UP, 0, "Ukraine", "I"),
        Equity(20, "$", RiskLevel.MEDIUM, Trend.DOWN, 0, "Russia", "I",
               "Roshen", 0.5),
        Debt(10, "$", RiskLevel.HIGH, Trend.UP, 0, "Belarus", "I"),
        Derivative(0, "$", RiskLevel.DANGER, Trend.UP, 0, "Moldova", "I",
                   "house"),
        Security(0, "$", RiskLevel.LOW, Trend.DOWN, 10, "Ukraine", "I")
    ]
    manager = SecuritiesManager(*securities)

    filteredList = manager.filterByPrice(0)
    for s in filteredList:
        print(s)
    print()

    sortedList = SecuritiesManager.sortByPriceAscending(securities)
    for s in sortedList:
        print(s)
    print()

    sortedFilteredList = SecuritiesManager.sortByDurationDescending(
        filteredList)
    for s in sortedFilteredList:
        print(s)
コード例 #9
0
def test_Derivative():
    # The formula is exact for linear functions, regardless of h
    f = lambda x: a * x + b
    a = 3.5
    b = 8
    dfdx = Derivative(f, h=0.5)
    diff = abs(dfdx(4.5) - a)
    assert diff < 1E-14, 'bug in class Derivative, diff=%s' % diff
コード例 #10
0
def over_generate(blue_print=None, mag_Enum=MValue, der_Enum=DValue):
    """generates all combinations of states.
    Assumes that all quantities have the can take the same magnetude and 
    derivative values.
    Input:
        blue_print: dict(dict: (list, list))) defining the state and the values it 
        can take.
        mag_Enum: IntEnum with the possible values for magntudes.
        der_Enum: IntEnum with the possible values for derivatives.            
    Output:
        list(states) all possible states.
    """

    #defaut state blueprint in case none is given.
    if blue_print == None:

        mags = list(map(int, mag_Enum))
        ders = list(map(int, der_Enum))

        blue_print = {
            "Hoose": {
                "Inflow": ([0, 1], ders)
            },
            "Container": {
                "Volume": (mags, ders)
            },
            "Drain": {
                "Outflow": (mags, ders)
            },
        }

    #Creates all states
    states = []
    t = []

    for e in blue_print:
        for q in blue_print[e]:
            t.append(blue_print[e][q][0])
            t.append(blue_print[e][q][1])

    t = tuple(t)
    combs = list(product(*t))

    for c in combs:
        idx = 0

        state = {}
        for e in blue_print:
            state[e] = {}
            for q in blue_print[e]:
                mag_bound = blue_print[e][q][0][-1]
                state[e][q] = Quantity(Magnitude(c[idx], upperBound=mag_bound),
                                       Derivative(c[idx + 1]))
                idx += 2

        states.append(state)

    return states
コード例 #11
0
    def test_isStateValid_noEX(self):
        state = {
            "A": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.PLUS))
            }
        }
        relations = []

        self.assertEqual(isStateValid(state, relations), False)
コード例 #12
0
    def test_isStateValid_singleRelation_valid(self):
        state = {
            "B": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO))
            },
        }
        relations = [
            {
                "type": "P+",
                "args": None,
                "Q1": ("B", "Q"),
                "Q2": ("C", "Q"),
            },
        ]

        self.assertEqual(isStateValid(state, relations), True)
コード例 #13
0
    def get_derivatives(self):
        derivatives = []
        for layer in self.data:
            derivatives_layer = []
            for _ in layer:
                derivatives_layer.append(Derivative())
            derivatives.append(derivatives_layer)

        # stem is a function of Y
        # derivatives are functions of Y the X_tilde s and the w s. X_tilde s
        # and w s are stored in the neuron objects. This makes the derivatives
        # functions of only Y.

        def differentiate(stem=lambda Y: (Y - self.data[0][0].get_output()),
                          i=0,
                          j=0,
                          a=0,
                          b=0):
            neuron = self.data[i][j]
            derivatives[i][j] += neuron.get_new_derivative(stem, a, b)

            l = 1
            for input_location in self.data[i][j].input_locations:
                if type(input_location) == tuple:
                    if type(neuron) == Convolutional:
                        for new_a in range(a, a + neuron.filter_shape[0]):
                            for new_b in range(b, b + neuron.filter_shape[1]):
                                differentiate(
                                    neuron.get_new_stem(
                                        self.data, input_location, stem, new_a,
                                        new_b, l), input_location[0],
                                    input_location[1], new_a, new_b)
                                l += 1
                    else:
                        differentiate(neuron.get_new_stem(
                            self.data, input_location, stem, l),
                                      input_location[0],
                                      input_location[1],
                                      a=0,
                                      b=0)
                        l += 1

        differentiate()

        # Now all of the components for every derivative have been computed we can form the complete derivative which
        # is a function that returns the sum of all the derivative components. This method greatly reduces the required
        # recursion depth compared to replacing the derivative function every time a new component was computed.
        completed_derivatives = []
        for layer in derivatives:
            completed_derivatives_layer = []
            for derivative in layer:
                completed_derivatives_layer.append(
                    derivative.get_complete_derivative())
            completed_derivatives.append(completed_derivatives_layer)
        return completed_derivatives
コード例 #14
0
    def test_isStateValid_lower_boundary_derivatives(self):
        state_invalid = {
            "A": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.MINUS))
            },
        }
        state_valid = {
            "A": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.PLUS))
            },
        }
        relations = [{
            "type": "EX",
            "args": None,
            "Q1": ("A", "Q"),
            "Q2": ("A", "Q"),
        }]

        self.assertEqual(isStateValid(state_invalid, relations), False)
        self.assertEqual(isStateValid(state_valid, relations), True)
コード例 #15
0
    def test_getRelationQuantities(self):
        state = {
            "B": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.PLUS))
            },
            "C": {
                "Q": Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO))
            },
        }
        relation = {
            "type": "P+",
            "args": None,
            "Q1": ("B", "Q"),
            "Q2": ("C", "Q"),
        }

        head, tail = getRelationQuantities(state, relation)
        self.assertEqual(
            head, Quantity(Magnitude(MValue.ZERO), Derivative(DValue.PLUS)))
        self.assertEqual(
            tail, Quantity(Magnitude(MValue.ZERO), Derivative(DValue.ZERO)))
コード例 #16
0
    def train_single(self, input_vector, target_vector):
        """
        input_vector and target_vector can be tuple, 
        list or ndarray
        """

        input_vector = np.array(input_vector, ndmin=2).T
        target_vector = np.array(target_vector, ndmin=2).T

        output_vector1 = np.dot(self.weights_in_hidden, input_vector)
        output_hidden = Activation.reLU(output_vector1)
        output_hidden *= Dropout.get_mask(output_vector1)
        output_vector2 = np.dot(self.weights_hidden_output, output_hidden)
        output_network = Activation.reLU(output_vector2)
        output_network *= Dropout.get_mask(output_vector2)
        output_errors = target_vector - output_network
        # update the weights:
        #tmp = output_errors * Derivative.sigmoid(output_network)
        try:
            tmp = output_errors * Derivative.reLU(output_network)
            tmp = self.learning_rate * np.dot(tmp, output_hidden.T)
            self.weights_hidden_output += tmp
        except:
            print("Something went wrong when writing to the file")

        # calculate hidden errors:
        try:
            hidden_errors = np.dot(self.weights_hidden_output.T, output_errors)
        except:
            print("Something went wrong when writing to the file")
        # ----------------------------------------------------------------------
        # update the weights:
        tmp1 = Derivative.reLU(output_hidden)
        tmp = hidden_errors * tmp1
        # -----------------------------------------------------------------------
        self.weights_in_hidden += self.learning_rate * np.dot(
            tmp, input_vector.T)
コード例 #17
0
    def test_ex_increase_minus(self):
        q1 = Quantity(None, Derivative(DValue.MINUS))
        q2 = Quantity(None, Derivative(DValue.MINUS))

        r.getFunc("EX", 1)(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.ZERO)
コード例 #18
0
    def test_i_minus_active_boundary(self):
        q1 = Quantity(Magnitude(MValue.PLUS), None)
        q2 = Quantity(None, Derivative(DValue.MINUS))

        r.influenceNegative(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.MINUS)
コード例 #19
0
    def test_i_minus_inactive(self):
        q1 = Quantity(Magnitude(MValue.ZERO), None)
        q2 = Quantity(None, Derivative(DValue.ZERO))

        r.influenceNegative(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.ZERO)
コード例 #20
0
    def test_i_plus_active(self):
        q1 = Quantity(Magnitude(MValue.PLUS), None)
        q2 = Quantity(None, Derivative(DValue.ZERO))

        r.influencePositive(q1, q2)
        self.assertEqual(q2.derivative.value, DValue.PLUS)
コード例 #21
0
 def test_isStateValid_complexValid(self):
     state = {
         "A": {
             "Q": Quantity(Magnitude(0), Derivative(0))
         },
         "B": {
             "Q": Quantity(Magnitude(0), Derivative(0))
         },
         "C": {
             "Q": Quantity(Magnitude(0), Derivative(0))
         },
     }
     relations = [
         {
             "type": "EX",
             "args": 1,
             "Q1": ("A", "Q"),
             "Q2": ("A", "Q"),
         },
         {
             "type": "I+",
             "args": None,
             "Q1": ("A", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "I-",
             "args": None,
             "Q1": ("C", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "P+",
             "args": None,
             "Q1": ("B", "Q"),
             "Q2": ("C", "Q"),
         },
         {
             "type": "VC",
             "args": MValue.MAX,
             "Q1": ("B", "Q"),
             "Q2": ("C", "Q"),
         },
         {
             "type": "VC",
             "args": MValue.ZERO,
             "Q1": ("B", "Q"),
             "Q2": ("C", "Q"),
         },
         {
             "type": "VC",
             "args": MValue.MAX,
             "Q1": ("C", "Q"),
             "Q2": ("B", "Q"),
         },
         {
             "type": "VC",
             "args": MValue.ZERO,
             "Q1": ("C", "Q"),
             "Q2": ("B", "Q"),
         },
     ]
     self.assertEqual(isStateValid(state, relations), True)
コード例 #22
0
 def copy(self):
     return Quantity(Magnitude(self.magnitude.value, self.magnitude.upperBound), Derivative(self.derivative.value))
コード例 #23
0
 def __init__(self, S0, vol, mean, N, delta, r):
     Derivative.__init__(self, S0, vol, mean, N, r, delta)
コード例 #24
0
 def __init__(self, S0, vol, mean, N, delta, r, strike):
     Derivative.__init__(self, S0, vol, mean, N, delta, r)
     self.strike = strike
コード例 #25
0
from derivative import Derivative


def Newton2(f, dfdx, x0, max_it=20, tol=1e-6):
    f0 = f(x0)
    iter = 0
    while abs(f0) > tol and iter < max_it:
        x1 = x0 - f0 / dfdx(x0)
        x0 = x1
        f0 = f(x0)
        iter += 1

    converged = iter < max_it
    return x0, converged, iter


def f(x):
    return 100000 * (x - 0.9)**2 * (x - 1.1)**3


dfdx = Derivative(f)
xstart = 1.01
result = Newton2(f, dfdx, xstart)
sol, converged, its = result

if converged:
    print(f'The method converged in {its} iterations')
    print(f'Solution x0={sol}, f(x0) = {f(sol)}')
else:
    print('The method did not converge')