Skip to content

Commit

Permalink
Move ElecMeter.correlation to Electric.correlation. #160
Browse files Browse the repository at this point in the history
  • Loading branch information
JackKelly committed Dec 2, 2014
1 parent 1221360 commit 885039f
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 54 deletions.
47 changes: 0 additions & 47 deletions nilmtk/elecmeter.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,53 +439,6 @@ def switch_times(self, threshold=40):
datetime_switches.append(delta_power_absolute[(delta_power_absolute>threshold)].index.values.tolist())
return flatten(datetime_switches)

def correlation(self, elec):
"""
Finds the correlation between the two ElecMeters. Both the ElecMeters
should be perfectly aligned
Adapted from:
http://www.johndcook.com/blog/2008/11/05/how-to-calculate-pearson-correlation-accurately/
"""
n = 0
x_sum = 0
y_sum = 0

# First pass is used to find x_bar and y_bar
for x_power in self.power_series():
n = n+len(x_power.index)
x_sum = x_power.sum()

for y_power in elec.power_series():
y_sum = y_power.sum()

x_bar = x_sum*1.0/n
y_bar = y_sum*1.0/n

# Second pass is used to find x_s and x_y (std.devs)
x_s_square_sum = 0
y_s_square_sum = 0

for x_power in self.power_series():
x_s_square_sum = x_s_square_sum + ((x_power-x_bar)*(x_power-x_bar)).sum()

for y_power in elec.power_series():
y_s_square_sum = y_s_square_sum + ((y_power-y_bar)*(y_power-y_bar)).sum()

x_s_square = x_s_square_sum*1.0/(n-1)
y_s_square = y_s_square_sum*1.0/(n-1)

x_s = np.sqrt(x_s_square)
y_s = np.sqrt(y_s_square)

numerator = 0
for (x_power, y_power) in izip(self.power_series(), elec.power_series()):
xi_minus_xbar = x_power-x_bar
yi_minus_ybar = y_power-y_bar
numerator = numerator + (xi_minus_xbar*yi_minus_ybar).sum()
denominator = (n-1)*x_s*y_s
corr = numerator*1.0/denominator
return corr

def entropy(self, k=3, base=2):
"""
This implementation is provided courtesy NPEET toolbox,
Expand Down
56 changes: 52 additions & 4 deletions nilmtk/electric.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ def when_on(self, **load_kwargs):
self.min_on_power_threshold())
for chunk in self.power_series(**load_kwargs):
yield chunk > on_power_threshold


def min_on_power_threshold(self):
"""Returns the minimum `on_power_threshold` across all appliances
Expand Down Expand Up @@ -103,7 +102,6 @@ def vampire_power(self, **load_kwargs):
# TODO: this might be a naive approach to calculating vampire power.
return self.power_series_all_data(**load_kwargs).min()


def uptime(self, **load_kwargs):
"""
Returns
Expand All @@ -118,8 +116,6 @@ def uptime(self, **load_kwargs):
uptime += good_section.timedelta
return uptime



def average_energy_per_period(self, offset_alias='D', **load_kwargs):
"""Calculate the average energy per period. e.g. the average
energy per day.
Expand Down Expand Up @@ -177,6 +173,58 @@ def proportion_of_energy(self, other, **loader_kwargs):
other_ac_type = ac_type
return total_energy[ac_type] / other_total_energy[other_ac_type]

def correlation(self, other):
"""
Finds the correlation between the two ElecMeters. Both the ElecMeters
should be perfectly aligned
Adapted from:
http://www.johndcook.com/blog/2008/11/05/how-to-calculate-pearson-correlation-accurately/
Parameters
----------
other : an ElecMeter or MeterGroup object
"""
n = 0
x_sum = 0
y_sum = 0

# First pass is used to find x_bar and y_bar
for x_power in self.power_series():
n = n+len(x_power.index)
x_sum = x_power.sum()

for y_power in other.power_series():
y_sum = y_power.sum()

x_bar = x_sum*1.0/n
y_bar = y_sum*1.0/n

# Second pass is used to find x_s and x_y (std.devs)
x_s_square_sum = 0
y_s_square_sum = 0

for x_power in self.power_series():
x_s_square_sum = x_s_square_sum + ((x_power-x_bar)*(x_power-x_bar)).sum()

for y_power in other.power_series():
y_s_square_sum = y_s_square_sum + ((y_power-y_bar)*(y_power-y_bar)).sum()

x_s_square = x_s_square_sum*1.0/(n-1)
y_s_square = y_s_square_sum*1.0/(n-1)

x_s = np.sqrt(x_s_square)
y_s = np.sqrt(y_s_square)

numerator = 0
for (x_power, y_power) in izip(self.power_series(), other.power_series()):
xi_minus_xbar = x_power-x_bar
yi_minus_ybar = y_power-y_bar
numerator = numerator + (xi_minus_xbar*yi_minus_ybar).sum()
denominator = (n-1)*x_s*y_s
corr = numerator*1.0/denominator
return corr


# def activity_distribution(self):
# * activity distribution:
# - use ElecMeter.get_timeframe() to get start and end
Expand Down
5 changes: 2 additions & 3 deletions nilmtk/metergroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,10 +844,10 @@ def dataframe_of_meters(self, rule='1T'):
submeters_dict[meter.identifier] = power_series
return pd.DataFrame(submeters_dict)


def entropy(self):
"""
Finds the entropy of different meters in this MeterGroup
Finds the entropy of each meter in this MeterGroup.
Returns
-------
pd.Series of entropy
Expand All @@ -859,7 +859,6 @@ def entropy(self):
entropy[id_meter] = meter.entropy()
return entropy


def pairwise_mutual_information(self):
"""
Finds the pairwise mutual information among different
Expand Down

0 comments on commit 885039f

Please sign in to comment.