Comments (1)
Archive of process_exchange_aggregator_uncertainty.py
for posterity. Removed because it is never called and adds unnecessary dependency to list.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# process_exchange_aggregator_uncertainty.py
#
##############################################################################
# REQUIRED MODULES
##############################################################################
import math
import numpy as np
import pandas as pd
from scipy.stats import t
from sympy import var, solve
##############################################################################
# MODULE DOCUMENTATION
##############################################################################
__doc__ = """This module is designed to compile the emission and generation
data to create emission factors, along with uncertainty information for all
relevant emission factors calculated using a log normal distribution.
The emission factors use the weight-based method developed by Troy Hawkins
([email protected]).
Note that these methods do not appear to be called by other modules in eLCI.
Last updated: 2023-11-06
"""
__all__ = [
'compilation',
'max_min',
'uncertainty',
]
##############################################################################
# FUNCTIONS
##############################################################################
def compilation(db, total_gen):
# Troy Method
# Create a copy of database by substitution the NA emissions with zero
db1 = db.fillna(value=0)
# Remove all rows where emissions are not reported for second dataframe,
# keeping the unreported emissions and facilities in separate database
db2 = db.dropna()
# This check is to make sure that the second database is not empty
# after dropping NAs. If empty, then we only use first database.
if db2.empty:
ef1 = np.sum(db1.iloc[:, 1])/total_gen
return ef1
ef1 = np.sum(db1.iloc[:, 1])/total_gen
ef2 = np.sum(db2.iloc[:, 1])/total_gen
# Weight formula.
# NOTE: Why is the weight equal to total_gen/total_gen?
weight = total_gen/total_gen
final_ef = ef2*weight + (1 - weight)*ef1
return final_ef
def uncertainty(db, mean_gen, total_gen, total_facility_considered):
# This is the function for calculating log normal distribution parameters.
# Troy Method
data_1 = db
df2 = pd.DataFrame([[0, 0]], columns=['Electricity', 'FlowAmount'])
for i in range(len(data_1), total_facility_considered):
data = data_1.append(df2, ignore_index=True)
data_1 = data
data = data_1
mean = np.mean(data.iloc[:, 1])
l,b = data.shape
# Compute standard error of the mean (but call it sd...)
sd = np.std(data.iloc[:, 1])/np.sqrt(l)
# Obtain the weighted emissions factor (emissions per MWh)
ef = compilation(db, total_gen)
# Endpoints of the range that contains alpha percent of the distribution
pi1, pi2 = t.interval(0.90, df=l-2, loc=mean, scale=sd)
# Converting prediction interval to emission factors
pi2 = pi2/mean_gen
pi1 = pi1/mean_gen
pi3 = (pi2 - ef)/ef;
x = var('x')
if math.isnan(pi3) == True:
return (None, None)
elif math.isnan(pi3) == False:
# This method will not work if the interval limits are more than
# 280% of the mean.
if pi3 < 2.8:
# Solve roots of the quadratic, ax^2 + bx + c = 0
a = 0.5
b = -(1.16308*np.sqrt(2))
c = np.log(1+pi3)
sd1 = (-b + np.sqrt(b**2 - (4 * a * c))) / (2 * a)
sd2 = (-b - np.sqrt(b**2 - (4 * a * c))) / (2 * a)
else:
# This is a wrong mathematical statement.
# However, we have to use it if something fails.
sd1, sd2 = solve(0.5*x*x - (1.36*np.sqrt(2))*x + (np.log(1+pi3)),x)
# Always choose lower standard deviation from solving the square root
# equation.
if sd1 < sd2:
log_mean = np.log(ef) - 0.5*(sd1**2)
return round(log_mean, 12), round(sd1, 12)
else:
log_mean = np.log(ef) - 0.5*(sd2**2)
return round(log_mean, 12), round(sd2, 12)
def max_min(db, mean_gen, total_gen, total_facility_considered):
# Troy Method
data = db
maximum = (data.iloc[:, 1]/data.iloc[:, 0]).max()
minimum = (data.iloc[:, 1]/data.iloc[:, 0]).min()
return (minimum, maximum)
from electricitylci.
Related Issues (20)
- What impact assessment method? HOT 4
- Globals, references to globals, and editing references of globals
- Forced BA aggregation for FERC and US, but what about eGRID? HOT 3
- Should PC link to petcoke UP? HOT 2
- KeyError in fill_default_provider_uuids
- Missing data file reference in Wiki
- No fuel category in Stewi's getInventoryFacilities for eGRID 2020 HOT 1
- _exchange_table_creation_ref missing renewables HOT 1
- EIA coalpublic2021.xls Excel file format cannot be determined HOT 1
- Missing International Mix data for 2021 onward HOT 3
- Addressing the Industrial Cogeneration Problem and Implementing the filter in model_config HOT 1
- Fix output exchange flows mislabeled as resources HOT 5
- Mexican balancing authority labeled as Canada in BA_Codes_930.xlsx HOT 7
- No 2022 EIA transmission and distribution loss data HOT 3
- Missing Canadian Exports for 2021 and beyond HOT 1
- Fix region mis-match between consumption and distribution mixes
- Incorrect output flow for "at grid; consumption mix" HOT 2
- Issues with the electricity column in generate_plant_water_use() HOT 1
- Update coal model inventories
- Set temporal representativeness attribute for processes to inventory vintage, not target year
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from electricitylci.