# Path: algo/cap/models.py
# class Value(object):
# class Parameter(Value):
# class ScalarVarianceParameter(Parameter):
# class PredictionVarianceParameter(ScalarVarianceParameter):
# class ArrayVarianceParameter(Parameter):
# class EntityScalarParameter(Parameter):
# class EntityArrayParameter(Parameter):
# class InteractionScalarParameter(Parameter):
# class Variable(Value):
# class ScalarVariable(Variable):
# class ArrayVariable(Variable):
# class EntityScalarVariable(ScalarVariable):
# class EntityArrayVariable(ArrayVariable):
# class InteractionScalarVariable(ScalarVariable):
# class Group(object):
# class EntityScalarGroup(Group):
# class EntityArrayGroup(Group):
# class InteractionScalarGroup(Group):
#   def __init__(self, name, shape):
#   def _initialize(self):
#   def update(self, value):
#   def __init__(self, name, shape):
#   def __init__(self, name):
#   def optimize(self, variable_group):
#   def __init__(self, name):
#   def optimize(self, groups, votes):
#   def __init__(self, name):
#   def optimize(self, variable_group):
#   def __init__(self, name, shape):
#   def optimize(self, variable_group):
#   def __init__(self, name, shape):
#   def optimize(self, variable_group):
#   def __init__(self, name, shape):
#   def optimize(self, variable_group):
#   def get_derivative_1(self, value, variable_group):
#   def get_derivative_2(self, value, variable_group):
#   def __init__(self, name, shape, entity_id, e_type, features, votes): 
#   def add_sample(self, value):
#   def get_last_sample(self):
#   def get_rest_value(self, groups, vote):
#   def reset_samples(self):
#   def get_related_votes(self, votes):
#   def __init__(self, name, entity_id, e_type, features, votes): 
#   def calculate_empiric_mean(self):
#   def calculate_empiric_var(self):
#   def __init__(self, name, shape, entity_id, e_type, features, votes): 
#   def calculate_empiric_mean(self):
#   def calculate_empiric_var(self):
#   def add_sample(self, value):
#   def __init__(self, name, entity_id, e_type, features, votes): 
#   def get_cond_mean_and_var(self, groups, votes):
#   def __init__(self, name, shape, entity_id, e_type, features, votes): 
#   def get_cond_mean_and_var(self, groups, votes):
#   def reset_samples(self):
#   def __init__(self, name, entity_id, e_type, features, votes): 
#   def get_cond_mean_and_var(self, groups, votes):
#   def get_related_votes(self, votes):
#   def __init__(self, name, shape, e_type, weight_param, var_param, var_H):
#   def iter_variables(self):
#   def get_instance(self, vote):
#   def contains(self, vote):
#   def get_num_samples(self):
#   def get_size(self):
#   def get_shape(self):
#   def set_pair_name(self, pair_name):
#   def __init__(self, name, e_type, weight_param, var_param, var_H):
#   def add_instance(self, entity_id, features, votes):
#   def __init__(self, name, shape, e_type, weight_param, var_param, var_H):
#   def add_instance(self, entity_id, features, votes): 
#   def __init__(self, name, e_type, weight_param, var_param, var_H):
#   def add_instance(self, entity_id, features, votes): 
#     X = array([reshape(v.features, v.features.shape[0]) for v in
#         variable_group.iter_variables()])
#     X = array([v.features.reshape(-1) for v in variable_group.iter_variables()])
# 
# Path: algo/cap/const.py
# K = 5 # size of user and review latent vectors
# EM_ITER = [10]
# BURN_IN = [0]
# SAMPLES = [20]
# NR_ITER = 50
# NR_TOL = 1e-4
# NR_STEP = 0.1     # In paper: 1
# ETA = 0 
# 
# Path: util/aux.py
# _LAMBDA = 100
# def cosine(vector_a, vector_b):
# def vectorize(dict_a, dict_b):
# def shrunk_cosine(votes_a, votes_b):
# def shrunk_pearson(votes_a, votes_b):
# def sigmoid(value):
# def sigmoid_der1(value):
# def sigmoid_der2(value):


from unittest import TestCase, main
from numpy import array, reshape, identity, vstack, diagonal, zeros
from numpy import testing as ntest
from numpy.linalg import pinv, lstsq
from random import random
from algo.cap import models, const
from util import aux
    groups = self.groups
    group = groups['lambda']
    matrix = None
    y = None 
    der1 = 0
    param = group.weight_param
    for variable in group.iter_variables():
      variable.num_samples = 10
      variable.samples = [random() for _ in xrange(10)] 
      feat = variable.features
      dot = param.value.T.dot(feat)[0,0]
      for sample in variable.samples:
        der1 += (sample - aux.sigmoid(dot)) * aux.sigmoid_der1(dot) * feat
    der1 = 1/(group.var_param.value*10) * der1
    ntest.assert_allclose(der1, param.get_derivative_1(param.value, group), rtol=1, atol=1e-7)

  def test_interaction_get_der2(self):
    groups = self.groups
    group = groups['lambda']
    matrix = None
    y = None 
    der2 = 0
    param = group.weight_param
    for variable in group.iter_variables():
      variable.num_samples = 10
      variable.samples = [random() for _ in xrange(10)] 
      feat = variable.features
      dot = param.value.T.dot(feat)[0,0]
      for sample in variable.samples:
        der2 += (- aux.sigmoid_der1(dot) ** 2 + (sample - aux.sigmoid(dot)) * 

# Path: algo/cap/models.py
# class Value(object):
# class Parameter(Value):
# class ScalarVarianceParameter(Parameter):
# class PredictionVarianceParameter(ScalarVarianceParameter):
# class ArrayVarianceParameter(Parameter):
# class EntityScalarParameter(Parameter):
# class EntityArrayParameter(Parameter):
# class InteractionScalarParameter(Parameter):
# class Variable(Value):
# class ScalarVariable(Variable):
# class ArrayVariable(Variable):
# class EntityScalarVariable(ScalarVariable):
# class EntityArrayVariable(ArrayVariable):
# class InteractionScalarVariable(ScalarVariable):
# class Group(object):
# class EntityScalarGroup(Group):
# class EntityArrayGroup(Group):
# class InteractionScalarGroup(Group):
#   def __init__(self, name, shape):
#   def _initialize(self):
#   def update(self, value):
#   def __init__(self, name, shape):
#   def __init__(self, name):
#   def optimize(self, variable_group):
#   def __init__(self, name):
#   def optimize(self, groups, votes):
#   def __init__(self, name):
#   def optimize(self, variable_group):
#   def __init__(self, name, shape):
#   def optimize(self, variable_group):
#   def __init__(self, name, shape):
#   def optimize(self, variable_group):
#   def __init__(self, name, shape):
#   def optimize(self, variable_group):
#   def get_derivative_1(self, value, variable_group):
#   def get_derivative_2(self, value, variable_group):
#   def __init__(self, name, shape, entity_id, e_type, features, votes): 
#   def add_sample(self, value):
#   def get_last_sample(self):
#   def get_rest_value(self, groups, vote):
#   def reset_samples(self):
#   def get_related_votes(self, votes):
#   def __init__(self, name, entity_id, e_type, features, votes): 
#   def calculate_empiric_mean(self):
#   def calculate_empiric_var(self):
#   def __init__(self, name, shape, entity_id, e_type, features, votes): 
#   def calculate_empiric_mean(self):
#   def calculate_empiric_var(self):
#   def add_sample(self, value):
#   def __init__(self, name, entity_id, e_type, features, votes): 
#   def get_cond_mean_and_var(self, groups, votes):
#   def __init__(self, name, shape, entity_id, e_type, features, votes): 
#   def get_cond_mean_and_var(self, groups, votes):
#   def reset_samples(self):
#   def __init__(self, name, entity_id, e_type, features, votes): 
#   def get_cond_mean_and_var(self, groups, votes):
#   def get_related_votes(self, votes):
#   def __init__(self, name, shape, e_type, weight_param, var_param, var_H):
#   def iter_variables(self):
#   def get_instance(self, vote):
#   def contains(self, vote):
#   def get_num_samples(self):
#   def get_size(self):
#   def get_shape(self):
#   def set_pair_name(self, pair_name):
#   def __init__(self, name, e_type, weight_param, var_param, var_H):
#   def add_instance(self, entity_id, features, votes):
#   def __init__(self, name, shape, e_type, weight_param, var_param, var_H):
#   def add_instance(self, entity_id, features, votes): 
#   def __init__(self, name, e_type, weight_param, var_param, var_H):
#   def add_instance(self, entity_id, features, votes): 
#     X = array([reshape(v.features, v.features.shape[0]) for v in
#         variable_group.iter_variables()])
#     X = array([v.features.reshape(-1) for v in variable_group.iter_variables()])
# 
# Path: algo/cap/const.py
# K = 5 # size of user and review latent vectors
# EM_ITER = [10]
# BURN_IN = [0]
# SAMPLES = [20]
# NR_ITER = 50
# NR_TOL = 1e-4
# NR_STEP = 0.1     # In paper: 1
# ETA = 0 
# 
# Path: util/aux.py
# _LAMBDA = 100
# def cosine(vector_a, vector_b):
# def vectorize(dict_a, dict_b):
# def shrunk_cosine(votes_a, votes_b):
# def shrunk_pearson(votes_a, votes_b):
# def sigmoid(value):
# def sigmoid_der1(value):
# def sigmoid_der2(value):


# Path: test/test_cap_models.py
from unittest import TestCase, main
from numpy import array, reshape, identity, vstack, diagonal, zeros
from numpy import testing as ntest
from numpy.linalg import pinv, lstsq
from random import random
from algo.cap import models, const
from util import aux

    groups = self.groups
    group = groups['lambda']
    matrix = None
    y = None 
    der1 = 0
    param = group.weight_param
    for variable in group.iter_variables():
      variable.num_samples = 10
      variable.samples = [random() for _ in xrange(10)] 
      feat = variable.features
      dot = param.value.T.dot(feat)[0,0]
      for sample in variable.samples:
        der1 += (sample - aux.sigmoid(dot)) * aux.sigmoid_der1(dot) * feat
    der1 = 1/(group.var_param.value*10) * der1
    ntest.assert_allclose(der1, param.get_derivative_1(param.value, group), rtol=1, atol=1e-7)

  def test_interaction_get_der2(self):
    groups = self.groups
    group = groups['lambda']
    matrix = None
    y = None 
    der2 = 0
    param = group.weight_param
    for variable in group.iter_variables():
      variable.num_samples = 10
      variable.samples = [random() for _ in xrange(10)] 
      feat = variable.features
      dot = param.value.T.dot(feat)[0,0]
      for sample in variable.samples:
        der2 += (- aux.sigmoid_der1(dot) ** 2 + (sample - aux.sigmoid(dot)) * 

            aux.sigmoid_der2(dot)) * feat.dot(feat.T)