Esempio n. 1
0
    def test_assign_1d(self):
        b = np.random.randn(100)
        sp_b = from_numpy(b)

        #a[:] = b[:] copy entire array
        a = np.random.randn(100)
        region_a = np.s_[0:100]
        region_b = np.s_[0:100]
        sp_a = assign(from_numpy(a), region_a, sp_b[region_b]).glom()
        a[region_a] = b[region_b]
        Assert.all_eq(sp_a, a)

        # a[0] = b[1] copy one value
        a = np.random.randn(100)
        region_a = np.s_[0]
        region_b = np.s_[1]
        sp_a = assign(from_numpy(a), region_a, sp_b[region_b]).glom()
        a[region_a] = b[region_b]
        Assert.all_eq(sp_a, a)

        # a[0:10] = b[20:30] copy range of values
        a = np.random.randn(100)
        region_a = np.s_[0:10]
        region_b = np.s_[20:30]
        sp_a = assign(from_numpy(a), region_a, sp_b[region_b]).glom()
        a[region_a] = b[region_b]
        Assert.all_eq(sp_a, a)

        # a[30:60] = b[:30] copy range of values, not starting from 0.
        a = np.random.randn(100)
        region_a = np.s_[0:10]
        region_b = np.s_[20:30]
        sp_a = assign(from_numpy(a), region_a, sp_b[region_b]).glom()
        a[region_a] = b[region_b]
        Assert.all_eq(sp_a, a)
Esempio n. 2
0
  def test_assign_1d(self):
    b = np.random.randn(100)
    sp_b = from_numpy(b)

    #a[:] = b[:] copy entire array
    a = np.random.randn(100)
    region_a = np.s_[0:100]
    region_b = np.s_[0:100]
    sp_a = assign(from_numpy(a), region_a, sp_b[region_b]).glom()
    a[region_a] = b[region_b]
    Assert.all_eq(sp_a, a)

    # a[0] = b[1] copy one value
    a = np.random.randn(100)
    region_a = np.s_[0]
    region_b = np.s_[1]
    sp_a = assign(from_numpy(a), region_a, sp_b[region_b]).glom()
    a[region_a] = b[region_b]
    Assert.all_eq(sp_a, a)

    # a[0:10] = b[20:30] copy range of values
    a = np.random.randn(100)
    region_a = np.s_[0:10]
    region_b = np.s_[20:30]
    sp_a = assign(from_numpy(a), region_a, sp_b[region_b]).glom()
    a[region_a] = b[region_b]
    Assert.all_eq(sp_a, a)

    # a[30:60] = b[:30] copy range of values, not starting from 0.
    a = np.random.randn(100)
    region_a = np.s_[0:10]
    region_b = np.s_[20:30]
    sp_a = assign(from_numpy(a), region_a, sp_b[region_b]).glom()
    a[region_a] = b[region_b]
    Assert.all_eq(sp_a, a)
Esempio n. 3
0
 def test_assign_array_like(self):
     a = np.zeros((20, 10))
     b = np.ones((10, ))
     region = np.s_[10, ]
     sp_a = assign(from_numpy(a), region, b).glom()
     a[region] = b
     Assert.all_eq(sp_a, a)
Esempio n. 4
0
 def test_assign_array_like(self):
   a = np.zeros((20, 10))
   b = np.ones((10, ))
   region = np.s_[10, ]
   sp_a = assign(from_numpy(a), region, b).glom()
   a[region] = b
   Assert.all_eq(sp_a, a)
Esempio n. 5
0
    def test_assign_expr(self):
        # Small matrix
        a = np.random.randn(20, 10)
        b = np.random.randn(10)
        region_a = np.s_[10, ]
        sp_a = assign(from_numpy(a), region_a, from_numpy(b)).glom()
        a[region_a] = b
        Assert.all_eq(sp_a, a)

        # Larger matrix
        a = np.random.randn(200, 100)
        b = np.random.randn(100)
        region_a = np.s_[50, ]
        sp_a = assign(from_numpy(a), region_a, from_numpy(b)).glom()
        a[region_a] = b
        Assert.all_eq(sp_a, a)

        # Worst case region
        a = np.random.randn(200, 100)
        b = np.random.randn(3, 50)
        region_a = np.s_[99:102, 25:75]
        sp_a = assign(from_numpy(a), region_a, from_numpy(b)).glom()
        a[region_a] = b
        Assert.all_eq(sp_a, a)
Esempio n. 6
0
  def test_assign_expr(self):
    # Small matrix
    a = np.random.randn(20, 10)
    b = np.random.randn(10)
    region_a = np.s_[10, ]
    sp_a = assign(from_numpy(a), region_a, from_numpy(b)).glom()
    a[region_a] = b
    Assert.all_eq(sp_a, a)

    # Larger matrix
    a = np.random.randn(200, 100)
    b = np.random.randn(100)
    region_a = np.s_[50, ]
    sp_a = assign(from_numpy(a), region_a, from_numpy(b)).glom()
    a[region_a] = b
    Assert.all_eq(sp_a, a)

    # Worst case region
    a = np.random.randn(200, 100)
    b = np.random.randn(3, 50)
    region_a = np.s_[99:102, 25:75]
    sp_a = assign(from_numpy(a), region_a, from_numpy(b)).glom()
    a[region_a] = b
    Assert.all_eq(sp_a, a)
Esempio n. 7
0
def als(A, la=0.065, alpha=40, implicit_feedback=False, num_features=20, num_iter=10, M=None):
  '''
  compute the factorization A = U M' using the alternating least-squares (ALS) method.

  where `A` is the "ratings" matrix which maps from a user and item to a rating score,
        `U` and `M` are the factor matrices, which represent user and item preferences.
  Args:
    A(Expr or DistArray): the rating matrix which maps from a user and item to a rating score.
    la(float): the parameter of the als.
    alpha(int): confidence parameter used on implicit feedback.
    implicit_feedback(bool): whether using implicit_feedback method for als.
    num_features(int): dimension of the feature space.
    num_iter(int): max iteration to run.
  '''
  num_users = A.shape[0]
  num_items = A.shape[1]

  AT = expr.transpose(A)

  avg_rating = expr.sum(A, axis=0) * 1.0 / expr.count_nonzero(A, axis=0)

  M = expr.rand(num_items, num_features)
  M = expr.assign(M, np.s_[:, 0], avg_rating.reshape((avg_rating.shape[0], 1)))

  #A = expr.retile(A, tile_hint=util.calc_tile_hint(A, axis=0))
  #AT = expr.retile(AT, tile_hint=util.calc_tile_hint(AT, axis=0))
  for i in range(num_iter):
    # Recomputing U
    shape = (num_users, num_features)
    U = expr.outer((A, M), (0, None), fn=_solve_U_or_M_mapper,
                   fn_kw={'la': la, 'alpha': alpha,
                          'implicit_feedback': implicit_feedback, 'shape': shape},
                   shape=shape, dtype=np.float)
    # Recomputing M
    shape = (num_items, num_features)
    M = expr.outer((AT, U), (0, None), fn=_solve_U_or_M_mapper,
                   fn_kw={'la': la, 'alpha': alpha,
                          'implicit_feedback': implicit_feedback, 'shape': shape},
                   shape=shape, dtype=np.float)
  return U, M
Esempio n. 8
0
def simulate(ts_all, te_all, lamb_all, num_paths):
  '''Range over a number of independent products.

  :param ts_all: DistArray
    Start dates for a series of swaptions.
  :param te_all: DistArray
    End dates for a series of swaptions.
  :param lamb_all: DistArray
    Parameter values for a series of swaptions.
  :param num_paths: Int
    Number of paths used in random walk.

  :rtype: DistArray

  '''
  swaptions = []
  i = 0
  for ts_a, te, lamb in zip(ts_all, te_all, lamb_all):
    for ts in ts_a:
      #start = time()
      print i
      time_structure = arange(None, 0, ts + DELTA, DELTA)
      maturity_structure = arange(None, 0, te, DELTA)

      ############# MODEL ###############
      # Variance reduction technique - Antithetic Variates.
      eps_tmp = randn(time_structure.shape[0] - 1, num_paths)
      eps = concatenate(eps_tmp, -eps_tmp, 1)

      # Forward LIBOR rates for the construction of the spot measure.
      f_kk = zeros((time_structure.shape[0], 2*num_paths))
      f_kk = assign(f_kk, np.s_[0, :], F_0)

      # Plane kxN of simulated LIBOR rates.
      f_kn = ones((maturity_structure.shape[0], 2*num_paths))*F_0

      # Simulations of the plane f_kn for each time step.
      for t in xrange(1, time_structure.shape[0]):
        f_kn_new = f_kn[1:, :]*exp(lamb*mu(f_kn, lamb)*DELTA-0.5*lamb*lamb *
            DELTA + lamb*eps[t - 1, :]*sqrt(DELTA))
        f_kk = assign(f_kk, np.s_[t, :], f_kn_new[0])
        f_kn = f_kn_new

      ############## PRODUCT ###############
      # Value of zero coupon bonds.
      zcb = ones((int((te-ts)/DELTA)+1, 2*num_paths))
      f_kn_modified = 1 + DELTA*f_kn
      for j in xrange(zcb.shape[0] - 1):
        zcb = assign(zcb, np.s_[j + 1], zcb[j] / f_kn_modified[j])

      # Swaption price at maturity.
      last_row = zcb[zcb.shape[0] - 1, :].reshape((20, ))
      swap_ts = maximum(1 - last_row - THETA*DELTA*expr.sum(zcb[1:], 0), 0)

      # Spot measure used for discounting.
      b_ts = ones((2*num_paths, ))
      tmp = 1 + DELTA * f_kk
      for j in xrange(int(ts/DELTA)):
        b_ts *= tmp[j].reshape((20, ))

      # Swaption price at time 0.
      swaption = swap_ts/b_ts

      # Save expected value in bps and std.
      me = mean((swaption[0:num_paths] + swaption[num_paths:])/2) * 10000
      st = std((swaption[0:num_paths] + swaption[num_paths:])/2)/sqrt(num_paths)*10000

      swaptions.append([me.optimized().force(), st.optimized().force()])
      #print time() - start
      i += 1
  return swaptions
Esempio n. 9
0
def als(A,
        la=0.065,
        alpha=40,
        implicit_feedback=False,
        num_features=20,
        num_iter=10,
        M=None):
    '''
  compute the factorization A = U M' using the alternating least-squares (ALS) method.

  where `A` is the "ratings" matrix which maps from a user and item to a rating score,
        `U` and `M` are the factor matrices, which represent user and item preferences.
  Args:
    A(Expr or DistArray): the rating matrix which maps from a user and item to a rating score.
    la(float): the parameter of the als.
    alpha(int): confidence parameter used on implicit feedback.
    implicit_feedback(bool): whether using implicit_feedback method for als.
    num_features(int): dimension of the feature space.
    num_iter(int): max iteration to run.
  '''
    num_users = A.shape[0]
    num_items = A.shape[1]

    AT = expr.transpose(A)

    avg_rating = expr.sum(A, axis=0) * 1.0 / expr.count_nonzero(A, axis=0)

    M = expr.rand(num_items, num_features)
    M = expr.assign(M, np.s_[:, 0], avg_rating.reshape(
        (avg_rating.shape[0], 1)))

    #A = expr.retile(A, tile_hint=util.calc_tile_hint(A, axis=0))
    #AT = expr.retile(AT, tile_hint=util.calc_tile_hint(AT, axis=0))
    for i in range(num_iter):
        # Recomputing U
        shape = (num_users, num_features)
        U = expr.outer(
            (A, M), (0, None),
            fn=_solve_U_or_M_mapper,
            fn_kw={
                'la': la,
                'alpha': alpha,
                'implicit_feedback': implicit_feedback,
                'shape': shape
            },
            shape=shape,
            dtype=np.float)
        # Recomputing M
        shape = (num_items, num_features)
        M = expr.outer(
            (AT, U), (0, None),
            fn=_solve_U_or_M_mapper,
            fn_kw={
                'la': la,
                'alpha': alpha,
                'implicit_feedback': implicit_feedback,
                'shape': shape
            },
            shape=shape,
            dtype=np.float)
    return U, M
Esempio n. 10
0
def simulate(ts_all, te_all, lamb_all, num_paths):
    """Range over a number of independent products.

  :param ts_all: DistArray
    Start dates for a series of swaptions.
  :param te_all: DistArray
    End dates for a series of swaptions.
  :param lamb_all: DistArray
    Parameter values for a series of swaptions.
  :param num_paths: Int
    Number of paths used in random walk.

  :rtype: DistArray

  """
    swaptions = []
    i = 0
    for ts_a, te, lamb in zip(ts_all, te_all, lamb_all):
        for ts in ts_a:
            # start = time()
            print i
            time_structure = arange(None, 0, ts + DELTA, DELTA)
            maturity_structure = arange(None, 0, te, DELTA)

            ############# MODEL ###############
            # Variance reduction technique - Antithetic Variates.
            eps_tmp = randn(time_structure.shape[0] - 1, num_paths)
            eps = concatenate(eps_tmp, -eps_tmp, 1)

            # Forward LIBOR rates for the construction of the spot measure.
            f_kk = zeros((time_structure.shape[0], 2 * num_paths))
            f_kk = assign(f_kk, np.s_[0, :], F_0)

            # Plane kxN of simulated LIBOR rates.
            f_kn = ones((maturity_structure.shape[0], 2 * num_paths)) * F_0

            # Simulations of the plane f_kn for each time step.
            for t in xrange(1, time_structure.shape[0]):
                f_kn_new = f_kn[1:, :] * exp(
                    lamb * mu(f_kn, lamb) * DELTA - 0.5 * lamb * lamb * DELTA + lamb * eps[t - 1, :] * sqrt(DELTA)
                )
                f_kk = assign(f_kk, np.s_[t, :], f_kn_new[0])
                f_kn = f_kn_new

            ############## PRODUCT ###############
            # Value of zero coupon bonds.
            zcb = ones((int((te - ts) / DELTA) + 1, 2 * num_paths))
            f_kn_modified = 1 + DELTA * f_kn
            for j in xrange(zcb.shape[0] - 1):
                zcb = assign(zcb, np.s_[j + 1], zcb[j] / f_kn_modified[j])

            # Swaption price at maturity.
            last_row = zcb[zcb.shape[0] - 1, :].reshape((20,))
            swap_ts = maximum(1 - last_row - THETA * DELTA * expr.sum(zcb[1:], 0), 0)

            # Spot measure used for discounting.
            b_ts = ones((2 * num_paths,))
            tmp = 1 + DELTA * f_kk
            for j in xrange(int(ts / DELTA)):
                b_ts *= tmp[j].reshape((20,))

            # Swaption price at time 0.
            swaption = swap_ts / b_ts

            # Save expected value in bps and std.
            me = mean((swaption[0:num_paths] + swaption[num_paths:]) / 2) * 10000
            st = std((swaption[0:num_paths] + swaption[num_paths:]) / 2) / sqrt(num_paths) * 10000

            swaptions.append([me.optimized().force(), st.optimized().force()])
            # print time() - start
            i += 1
    return swaptions