Skip to content

Instantly share code, notes, and snippets.

@ltiao
Last active December 26, 2015 16:29
Show Gist options
  • Select an option

  • Save ltiao/7180436 to your computer and use it in GitHub Desktop.

Select an option

Save ltiao/7180436 to your computer and use it in GitHub Desktop.
Collaborative Reputation Iterarative Filtering
#!/usr/bin/python
import numpy as np
import matplotlib
matplotlib.use('TkAgg') # Required for Mac OSX
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import logging.config
from settings import LOGGING
logging.config.dictConfig(LOGGING)
logger = logging.getLogger('iterative_filtering')
def iterative_filter(evaluations, trust_function, accuracy=10**(-8)):
"""Iterative Filtering Algorithm
Keyword arguments:
evaluations -- n by m numpy array of m evaluations or readings performed by n agents or sensors
evaluations.shape = (n, m)
trust_function -- trust function. Must be vectorized with np.vectorize.
accuracy --
Returns:
number of iterations, error, trust weight for each agent/sensor, approximate true rank for each object
"""
logger.info('Evaluations matrix dimension: {dim}'.format(dim=evaluations.shape))
n, m = evaluations.shape
w = np.ones(n) # initialize weight vector of size n (numpy array with shape (n,))
r = np.zeros(m) # initialize ranks vector of size m (numpy array with shape (m,))
epsilon = 1 # emulate a do-while loop by always satisfying the condition on the first iteration
iteration = 0
result = []
while epsilon > accuracy:
old_r = r
r = np.dot(w, evaluations)/np.sum(w)
d = np.sum(np.power(evaluations-r, 2), axis=1)/float(TT)
w = trust_function(d)
epsilon = np.linalg.norm(r-old_r)
result.append(w)
logger.info(u'\u03B5-value: {e} | Iteration: {i}'.format(e=epsilon, i=iteration))
iteration += 1
return np.array(result)
NN = 16
TT = 24*60/5
temperature = lambda x: 20 + 5*np.sin(2*np.pi*x/288-np.pi/2)
var = np.arange(1, NN+1)
std = np.sqrt(var)
TN = np.array(map(lambda x: np.random.normal(scale=x, size=TT), std))
SG = np.array(map(temperature, xrange(1, TT+1)))
TS = np.tile(SG, (NN, 1))
Readings = TN + TS
# g = np.vectorize(lambda d: 1/float(d))
g = lambda d: np.amax(d) - d
w = iterative_filter(Readings, g, 10**(-8))
num_iterations, num_sensors = w.shape
index = np.arange(num_sensors)
bar_width = 0.5
iteration = 0
def keyboard(event):
global iteration
if event.key == 'right':
iteration += 1
if event.key == 'left':
iteration -= 1
plt.clf()
plt.plot(np.arange(TT), np.dot(w[iteration%num_iterations], Readings)/np.sum(w[iteration%num_iterations]))
plt.xlabel('Time of day (every 5 minutes)')
plt.ylabel('Temperature')
plt.title('Agent Trust Weights - Iteration: {i}'.format(i=iteration%num_iterations))
plt.axis('tight')
plt.draw()
plt.gcf().canvas.mpl_connect('key_press_event', keyboard)
plt.show()
exit(0)
errorML = np.linalg.norm(np.dot(1/var, Readings)/np.sum(1/var)-SG)/np.sqrt(TT)
WML = (1/var)/np.sum(1/var)
estimateML = np.dot(1/var, Readings)/np.sum(1/var)
errorBEST = np.amin(np.sqrt(np.mean(TN**2, axis=1)))
estimate = np.zeros(TT)
w = np.ones(NN)
epsilon = 1000
counter = 0
# print np.average(Readings, axis=0)
while epsilon > accuracy and counter < 100:
counter += 1
oldr = estimate
estimate = np.dot(w, Readings)/np.sum(w)
dist = np.sum((Readings-estimate)**2, axis=1)/float(TT)
w = g(dist)
epsilon = np.linalg.norm(estimate-oldr)
print counter, epsilon
W1 = w/np.sum(w)
COUNTER1 = counter
ESTIMATE1 = np.dot(W1, Readings)
errorIF1 = np.linalg.norm(estimate-SG)/np.sqrt(TT)
print ESTIMATE1
x = np.arange(TT)
plt.plot(x, TS)
plt.xlabel('Time of day (every 5 minutes)')
plt.ylabel('Temperature')
plt.axis('tight')
plt.show()
#print estimateML.shape
# x = np.arange(TT)
# plt.plot(x, estimateML)
# plt.xlabel('Time of day (every 5 minutes)')
# plt.ylabel('Temperature')
# plt.axis('tight')
# plt.show()
#print var.shape#.reshape(16, 1)
# x = np.arange(TT)
# plt.axhline(linewidth=1, color='k')
# plt.plot(x, TN[0], marker='.', linestyle='None', color='r')
# plt.plot(x, TN[7], marker='.', linestyle='None', color='g')
# plt.plot(x, TN[15], marker='.', linestyle='None', color='b')
# plt.xlabel('Time of day (every 5 minutes)')
# plt.ylabel('Temperature')
# plt.axis('tight')
# plt.show()
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'formatters': {
'verbose': {
'format': '%(asctime)s [%(levelname)s] (%(threadName)-10s): %(message)s',
'datefmt': '%m/%d/%Y %I:%M:%S %p'
},
'simple': {
'format': '%(asctime)s %(levelname)s %(message)s',
'datefmt': '%m/%d/%Y %I:%M:%S %p'
},
},
'handlers': {
'default': {
'level':'INFO',
'class':'logging.handlers.RotatingFileHandler',
'filename': 'iterative_filtering.log',
'maxBytes': 1024 * 1024 * 5, # 5 mb,
'backupCount': 10,
'formatter': 'simple',
},
'console':{
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'verbose'
},
},
'loggers': {
'': {
'handlers': ['default'],
'level': 'DEBUG',
'propagate': True
},
'iterative_filtering': {
'handlers': ['default', 'console'],
'level': 'DEBUG',
'propagate': False,
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment