11 tips from 11 months of learning Python

I’ve been learning Python for around 11 months. It’s been a wonderful journey! This post is a list of 11 things that I’ve learned along the way.

1 – Setup

The hardest thing about learning Python can be getting it setup in the first place! I recommend using the Anaconda distribution of Python.

Regarding Python 2 vs Python 3 – if you are starting out now it makes sense to learn Python 3. It’s worth knowing what the differences are between the two – once you’ve made some progress with Python 3.

The installation process is pretty straight forward – you can check that Anaconda installed correctly by typing ‘python’ into Terminal or Command Prompt. You should get something like the following:

python_terminal

2 – pip

pip is a way to manage packages in Python. pip is run from a Terminal. Below are the pip commands I use the most.

To install a package (Note that the -U argument forces pip to install the upgraded version of the package)

pip install pandas -U

To remove a package

pip remove pandas

To print all installed packages

pip freeze

3 – Virtual environments

Virtual environments are best practice for managing Python on your machine. Ideally you should have one virtual environment for each project you work on.

This gives you the ability to work with different versions of packages in different projects and to understand the package dependencies of your project.

There are two main packages for managing virtual environments. Personally I use conda (as I always use the Anaconda distribution of Python).

One cool trick is that once you activate your environment, you can start programs such as Atom or Jupyter and they will use your environment.

For example if you use a terminal plugin within Atom, starting Atom this way will mean the terminal uses your environment Python – not your system Python.

4 – Running Python scripts interactively

Running a script interactively can be very useful when you are learning Python – both for debugging and getting and understanding of what is going on!

cd folder_where_script_lives
python -i script_name.py

After the script has run you will be left with an interactive console. If Python encounters an error in the script then you will still end up in interactive mode (at the point where the script broke).

5 – enumerate

Often you want to loop over a list and keep information about the index of the current item in the list.

This can naively be done by

idx = 0
for item in a_list:
    other_list[idx] = item
    idx += idx

Python offers a cleaner way to implement this

for idx, item in enumerate(a_list):
    other_list[idx] = item

We can also start the index at a value other than zero

for idx, item in enumerate(a_list, 2):
    other_list[idx] = item

6 – zip

Often we want to iterate over two lists together. A naive approach would be to

for idx, item_1 in enumerate(first_list):
    item_2 = second_list[idx]
    result = item_1 * item_2

A better approach is to make use of zip – part of the Python standard library

for item_1, item_2 in zip(first_list, second_list):
    result = item_1 * item_2

We can even combine zip with enumerate

for idx, (item_1, item_2) in zip(first_list, second_list):
    other_list[idx] = item_1 * item_2

7 – List comprehensions

List comprehensions are baffling at first. They offer a much cleaner way to implement list creation.

A naive approach to making a list would be

new_list = []
for item in old_list:
    new_list.append(2 * item)

List comprehensions offers a way to do this in a single line

new_list = [item * 2 for item in old_list]

You can also create other iterables such as tuples or dictionaries using similar notation.

8 – Default values for functions

Often we create a function with inputs that only need to be changed rarely. We can set a default value for a function by

def my_function(input_1, input_2=10):
    return input_1 * input_2

We can run this function using

result = my_function(input_1=5)

Which will return result = 50.

If we wanted to change the value of the second input we could

result_2 = my_function(input_1=5, input_2=5)

Which will return result = 25.

9 – git

Git is a fantastic tool that I highly recommend using. As with Python I’m no expert! A full write up of how to use git is outside the scope of this article – these commands are useful to get started. Note that all of these commands should be entered in a Terminal that is inside the git repo.

To check the status of the repo

git status

To add files to a commit and push to your master branch

git add file_name
git commit -m 'commit message'
git push origin master

Note that you can do multiple commits in a single push.

We can also add multiple files at once. To add all files that are already tracked (i.e. part of the repo)

git add -u

To add all files (tracked & untracked)

git add *

Another useful command is

git reset HEAD~

What this command allows you to do is to undo local commits. Sometimes you will add files to your commit you didn’t mean to – this allows you to undo them one by one (ie commit by commit).

10 – Text editors

There are a range of text editors you can use to write Python
– Atom
– vi
– vim
– sypder (comes with anaconda)
– Sublime Text
– Pycharm
– notepad ++

All have their positives and negatives. When you are starting out I reccomend using whatever feels the most comfortable.

Personally I started out using notepad ++, then went to spyder, then to Atom and vim.

It’s important to not focus too much on what editor you are using – more important to just write code.

11 – Books & resources

I can recommend the following resources for Python:
Python Reddit
The Hitchhiker’s Guide to Python

Python 3 Object Oriented Programming
Effective Python: 59 Specific Ways to Write Better Python
Python for Data Analysis: Data Wrangling with Pandas, NumPy, and IPython
Python Machine Learning
Automate the Boring Stuf with Python

I can also recommend the following for getting an understanding of git:
Github For The Rest Of Us
Understanding Git Conceptually

Thanks for reading!

Getting Wind and Sun onto the Grid – Energy Insights

Energy Insights highlights interesting energy content from around the web.

Previous posts include Elon Musk on autonomous cars and the CAISO Stage 1 Grid Emergency.


This post highlights three interesting insights from the 2017 IEA report Getting Wind & Sun onto the Grid.

1 – decorrelation of risks to grid stability

An electricity system operator faces different sources of uncertainty when managing a grid. Three of the major sources of uncertainty are:

1 – risk of losing generators or transmission infrastructure – either a large power plant tripping out or loss of part of the transmission system

2 – demand forecasting – electricity supply must be balanced with demand in real time. Backup plant will balance the grid in case demand is higher than forecast

3 – intermittent renewables – the weather dependent nature of wind & solar means that future generation is uncertain. Sudden changes in the weather can lead to dramatic changes in power generated by wind & solar plants. Backup plant must be kept spinning to make up for the lost renewables generation.

The IEA make the point that these three sources of uncertainty are not correlated. I would argue there is a degree of correlation between demand forecasting and the intermittent nature of wind & solar. Generation by low voltage plant like residential solar appears to the system operator as a reduction in demand.

I love the idea of optimizing over all three sources of uncertainty together rather than in isolation. By being smarter about how de-risk these uncertainties we can reduce the grid’s reliance on backup plant.

End consumers of electricity ultimately pay for all grid costs. Avoiding the installation, operation & maintenance of backup plant will mean cheaper & cleaner electricity for us all.

2 – dilemma of resource strength, co-location with grid and geographical smoothing

Optimizing the location of wind & solar plants requires balancing three factors:

1 – renewable resource strength

2 – cost of connecting to the grid

3 – geographical smoothing

Optimizing for resource strength means placing renewables where the wind is strongest or the sun shines brightest. A stronger or more consistent resource availability improves economics by increasing electricity generated. Unfortunately the best wind & solar resources often in remote areas. The cost of connecting to remote resources to the grid is higher.

Co-location with the grid minimizes the investment cost for renewables projects. The IEA quote a study that the cost of expanding the grid had a median value of around 15% of the cost of the generation capacity. Co-location with the grid will keep this cost to a minimum. It will also reduce electricity lost as heat during transmission.

Geographical smoothing is about spreading renewable generation over different areas. Short term variations in renewable generation in different locations tend to cancel out. Concentrating wind turbines in one place means that when the wind stops blowing there it has a major effect on the grid. By spreading the generation geographically, the output is more consistent.

Another concept related to geographical smoothing is technological diversification. This is the smoothing effect gained by diversifying between wind and solar. The IEA make the point that generation from wind & solar is often negatively correlated. This means that changes in solar generation are often balanced by changes in wind generation.

Optimizing the location of a renewables plant requires optimizing all three of these factors together. At first glance we should put wind turbines where it’s windiest – but perhaps a less windy location closer to the grid would be a better use of capital.

3 – technical tools to strengthen the grid

One of the major costs of integrating renewables is grid infrastructure. Renewables plants are often located where the resource is strongest – which can be far away from the transmission system.

The IEA highlight a few techniques to strengthen the grid at minimum investment cost. I’m going to detail two of them.

1 – dynamic line ratings

The capacity of cables to carry electricity is limited by temperature – if the line gets too hot you’re gonna have a bad time. This capacity is also known as the line rating.

Currently the rating of cables are often based on a fixed temperature. Fixing the temperature limits the capacity of the line to carry electricity. A dynamic line rating takes into account the changing ambient temperature. When it gets cold a dynamic line rating would increase the capacity of the cable to carry electricity.

The IEA quote a Swedish project where dynamic line ratings were used to identify a 60% increase in colder periods. These colder periods correlate with windier periods – meaning the increased capacity often coincides with lots of available wind power. I’m not sure if ‘identify’ means these savings were actually realized or just identified as potential savings. Nevertheless – 60% is a significant increase.

Interestingly enough it seems that forecasting for dynamic line rating is an active area of research. It’s another potential application of machine learning – similar to the forecasting of electricity demands or prices I discuss here.

2 – flexible AC transmission systems

This is the use of power electronic devices to improve grid power factor.

Power factor is the ratio of active to reactive power. Active power is useful – reactive power is not useful. But the cables in our electricity systems need to carry both simultaneously.

Reducing reactive power means more space for active power in the cables that make up the grid. The investment required is the solenoids or condensers that are installed midway on the line to absorb or inject reactive power. This is a lot cheaper than building new transmission lines.

Cheat sheet for gas engine & gas turbine CHP – Energy Basics

In my previous life at ENGIE I specialized in the technical modeling of combined heat & power (CHP) plants.

I developed models to support sales projects and to optimize operation of existing sites – such as the district heating scheme at the Olympic Park in London.

CHP is an attractive technology for maximizing the recovery of heat & electricity from fuel. The technologies are mature and will deliver carbon benefits in most electricity grids.

This post aims to give concise practical details about the two most commons forms of gas based CHP. Together the gas engine and gas turbine are around 90 % of installed capacity of CHP in the UK.

Table 1 – CHP in the UK (from DECC)

This post focuses on the facts that matter in the day to day world of energy management.

common for both gas engines & gas turbines
  • Both have total efficiencies (electric + thermal) roughly around 80 % HHV.
  • Both operate with a maximum electric efficiency at full load.
  • In part load operation total efficiency remains around 80 % HHV – reductions in electric efficiency are counteracted by increases in thermal efficiency.
gas engines

Figure 1 – a simple gas engine schematic
Key practical advantages
  • High electric efficiency.
  • Cheap maintenance cost.
  • Cheap capital cost.
Key practical disadvantages
  • Half of the recoverable heat is generated as low quality (<100 °C).
  • Usually only economic at sizes below 5 MWe.

A gas engine has a high electric efficiency (30-38 % HHV).

A gas engine generates roughly the same amount of electricity and heat – i.e. the heat to power ratio is around 1:1.

The recoverable heat generated in a gas engine is split roughly half high grade (>500 °C), half low grade (<100 °C).

Gas engines are typically economic up until 5-6 MWe.  Beyond that size gas turbines become competitive.

Gas engine CHP is low maintenance (0.6 – 1.2 p/kWh @ 8,000 hours/yr) and captial (500 – 1,500 /kWe total project) cost.

Having half of the heat generated as low quality means that a low-grade heat sink is required.

Many industrial processes only require high-temperature heat (typically served using steam). Without a low-grade heat sink for the low-grade heat the economics of a gas engine will suffer.

Typical low-grade heat sinks include space heating, boiler feedwater heating on sites with low condensate return rates and low-temperature process heating. This makes district heating and hospitals good applications of gas engine CHP.

gas turbines
Figure 2 – a simple gas turbine schematic
Key practical advantages
  • All of the heat generated is high quality.
  • Supplementary firing can be used to generate more heat at high efficiency.
  • Potential to combine with steam turbines to generate more power.
Key practical disadvantages
  • Lower electric efficiency.
  • Complex emissions control systems.
  • Usually limited to sizes above 5 MWe.

A gas turbine operates with a lower electric efficiency (25-35% HHV) than a gas engine.

A gas turbine generates roughly twice as much heat as power – ie the heat to power ratio is around 2:1.

Unlike a gas engine, all of the heat generated by a gas turbine is high grade (>500 C).  This makes gas turbines ideal for industrial sites that need high-temperature steam to run their processes.

This also allows gas turbines to be used in combined cycle mode (steam is generated off the exhaust and used to drive a steam turbine).  More gas can be fired into the exhaust to further increase steam generation (known as supplementary firing).

This can be a key advantage of gas turbines, as the marginal efficiency of supplementary firing is higher than generating heat in a shell or water tube boiler.

blockchain in energy – part two – blockchain and the grid

This is the second of a two part series on blockchain in the energy system.

blockchain in energy – part one – what is blockchain
introduction, advantages, description of the system, proof of stake vs. proof of work, smart contracts, bitcoin & Ethereum

blockchain in energy – part two – blockchain and the grid
how far can we go with blockchain and the grid, perspectives of the system operator, consumers, generators, machines & regulators


The first part of this series introduced blockchain and concepts such as smart contracts and proof of work.  In this second post I will theorize on how far we could go with blockchain and the electricity grid.

Today the most successful applications of blockchain are in cryptocurrencies such as bitcoin. Using blockchain to support the grid will offer additional challenges – some of which may necessitate keeping third parties in place.

It’s unclear how far we can go with blockchain and the grid.  Currently applications of blockchain in the energy industry are limited to small pilot scale projects.  Even in more widespread cryptocurrency applications the technology is still not considered mature.

Our transition towards a physically decentralized, small scale and clean electricity system is well underway. Blockchain is not necessary for this transition to happen. Yet it could be a useful tool in developing a more secure, efficient and open electricity grid.

The transmission & distribution system operator

It would be a waste to just use the blockchain to just do meter billing – Joi Ito

I’m excited about the potential for blockchain to be the transaction and control layer for the grid. Blockchain could enable a living, self-organizing grid powered by smart contracts. Smart contracts would automatically match generators with consumers, balancing the grid in near real time. Virtual power plants and storage could also be dispatched using smart contracts.

A blockchain could make smaller scale (in both time and volume) transactions more viable. This would make a more diverse range of resources available to the system operator for real time balancing. It would also reduce the length of the settlement process – no need for complex and costly processes such as profiling.

This blockchain would provide an excellent database for managing grid operations. Data would be available for every transaction – price, quantity and time all down to a fine scale. This dataset would also allow more accurate forecasts of generation & demand. More accurate forecasts will reduce spinning reserve and imbalance costs.

Blockchain is the first technology that can identify of the origin of electricity. This would simplify certification of renewable generation or tracking carbon emissions.

It would also allow a system operator to see which parts of the grid were used to supply electricity. This would allow charging based on how the transmission and distribution system was actually used.

Theoretically a blockchain allows a fully decentralized system. In the context of an electricity grid it’s hard to see how the centralized role of transmission and distribution system operators disappearing.

Electricity consumers

A blockchain could create a more dynamic and competitive marketplace. A smart contract could purchase electricity from many different sources within the same day. An eaiser ability to switch should mean more competition and lower electricity prices. Combine this with the efficiency improvements of removing third parties and we would expect that electricity would get cheaper for consumers.

Smart contracts would also give consumers more power over where their electricity comes from. Environmentally conscious consumers could purchase 100% renewable electricity. Community focused consumers could prefer locally generated electricity. Price concious consumers could minimize costs by always purchasing the cheapest electricity available.

Allowing consumers more control over their electricity mix could increase customer engagement. Getting consumers engaged with where their energy comes from would be a powerful ally in speeding up our energy transition.  It would also be fantastic for utilities looking for loyal customers.

A blockchain could also be powerful in developing communities. Imagine knowing all of the electricity you used was generated on your street. Money would be kept in the local economy, improving the business case for local generation. It’s a virtuous cycle that could accelerate our energy transition.

Using our energy system to develop a community spirit is potentially one of the most powerful impacts a blockchain could have. Technology often leads to more isolated communities – it would be great for technology to bring communities together.

It’s not clear exactly how far a blockchain system could go in completely removing third parties from the grid. A number of duties currently fulfilled by suppliers would need to be done by consumers themselves.

Take forecasting for example. Today electricity suppliers submit forecasts and get charged based on their imbalance. These forecasts are crucial for the system operator to schedule generation. How would this forecasting process be done when there is no supplier responsible for consumers?

Blockchain could be a major positive for electricity consumers in third world countries. Third world economies are often high inflation and low trust – a blockchain using prepaid smart meters could help with both these issues.  Investements in solar panels can be made using cryptocurrencies with the generation supported by a grid blockchainm

Establishing blockchains in a third world countries could allow that economy to skip over the dirty & centralized systems historically used in developed countries.

Electricity generators

A blockchain system could be very powerful for small scale generators. Removing third parties and their inefficiencies should improve the economics of all generators. We are already seeing impressive reductions in the cost of renewables and storage – an increased share of the value of electricity generated kept by investors will only make these technologies more lucrative.

A blockchain would democratize access to the marketplace, removing barriers that currently hurt small scale generators, flexibility, and storage. Today small scale generators often get a very unattractive fixed export rate. A blockchain would allow small scale generators to compete on equal footing in the same marketplace as a 2 GW power station.

Other technologies such as flexibility and storage are often locked out of the market through high minimum capacities or out of market subsidies. A blockchain is the antithesis of this.

The blockchain would also provide an excellent data source for investors in electricity generation. An investor could accurately understand the historical value of electricity generated through the transaction history of the blockchain.

Machines

The first role for machines in a blockchain system is to put the smart into smart contracts. The basis for this intelligence could be either a set of human designed heuristics or a narrow machine learning system. Reinforcement learning agents could be deployed as smart contracts, built to provide useful functions such as controlling voltage or frequency.

Blockchain also allows machines to do something quite interesting – own property. A machine with access to the private key of a blockchain account essentially owns any property associated with that private key. A blockchain could lead to the rise of a machine to machine economy, with machines trading and building assets within their blockchain accounts.

Regulators & the legal system

Regulators have a lot of work to do to prepare for a blockchain marketplace. Three broad areas require attention:

  1. protecting consumers,
  2. the physical nature of the electricity system,
  3. nurturing a technology to maturity.

Our current legal framework protects customers in centralized marketplaces. Additional protection is needed for a decentralized system.

The major challenge is the lack of a central authority.  Today’s legal system involves a clear allocation of organizational & legal responsibility – this doesn’t really exist in a system where consensus is reached by the majority. It’s one reason why an electricity grid would likely be supported by a private blockchain.

As discussed in Part One the optimal mechanism for verifying blockchain truth is not clear.  Mechanisms such as proof of work or proof of stake all have drawbacks.  Another implication of verification mechanism design is the balance of power between miners and other nodes.  Paradigms such as the tangle have evolved to adjust this balance.

Another issue is the boundary between the blockchain and the grid (the smart meter). Today suppliers appoint meter operators to install and maintain electricity meters. In a fully decentralized blockchain system it’s unclear who would be responsible for calibrating meters. A blockchain is excellent at ensuring only valid transactions are accepted, but there is still potential for fraud at the interface between the real and blockchain world.

The second area relates to the physical nature of the electricity system. The electricity grid is critical infrastructure that has additional layers of electricity supply security and physical safety.

Using blockchain in an electricity system requires an additional level of care over pure data systems such as cryptocurrencies. Managing the electricity grid involves not only data but the physical delivery of electricity.

Finally regulators will need to be careful in dealing with a technology that is not fully mature. Any immature technology can give surprising results as it develops and deployed at scale.

Thanks for reading!

Sources and further reading

The tangle whitepaper

energy_py update – July 2017

energy_py is a collection of reinforcement learning agents and environments for energy systems. You can read the introductory blog post for the project and check out the repo on GitHub.

Saving of memory and value function after each episode

This quality of life improvement has a major impact on the effectiveness of training agents using energy_py. It means an agent can keep learning from experience that occurred during a different training session.

As I train models on my local machine I often can only dedicate enough time for 10 episodes of training. Saving the memory & value functions an agent to learn from hundreds of episodes without training every episode in one run.

Running each episode on a different time series

Training agents with randomly selected weeks in the year. It’s much more useful for an agent to experience two different weeks of CHP operation than to experience the same week over and over again. It also should help the agent to generalize to operating data sets it hasn’t seen before.

Double Q-Learning

Building another agent has been a todo for energy_py for a long time. I’ve built a Double Q-Leaner – based on the algorithm given in Sutton & Barto. The key extension in Double Q-Learning is to maintain two value functions.

The policy is generated using the average of the estimate for both Q networks. One network is then randomly selected for training using a target created by the other network.

The thinking behind Double Q-Learning is that we can avoid the maximization bias of Q-Learning. A positive bias is caused by the use of maximization operations for estimating the value of states. The maximization functions lead to overoptimistic estimates of the value of state actions.

Next major tasks are:
1 – build a policy gradient method – most likely a Monte Carlo policy gradient algorithm,
2 – build a demand side response environment.

Thanks for reading!

blockchain in energy – part one – what is blockchain

This is the first of a two part series on blockchain in the energy system.

blockchain in energy – part one – what is blockchain
introduction, advantages, description of the system, proof of stake vs. proof of work, smart contracts, bitcoin & Ethereum

blockchain in energy – part two – blockchain and the grid
how far can we go with blockchain and the grid, perspectives of the system operator, consumers, generators, machines & regulators


Decentralization is the future of our physical energy system. Should we manage this decentralized system using large scale, centralized third parties?

Blockchain is a technology that enables decentralization. It allows a true peer to peer economy – no third party needed. Blockchain can offer progress in three dimensions – security, access and efficiency.  Blockchain aligns with our transition towards a decentralized and clean energy future.

Our current energy transition is moving us away from dispatchable, centralized and large-scale generation towards intermittent, distributed and small scale generation.

Figure 1 – Our current energy transition is moving us away from dispatchable, centralized and large-scale generation towards intermittent, distributed and small scale generation.

This post will give an introduction to the blockchain technology – what it is, how it works, the advantages and the challenges.   We will also take a look at two of the largest implementations of blockchain, bitcoin and Ethereum.

Part Two theorizes how far we might be able to go with blockchain and the grid,  We look what a smart contract enabled blockchain might mean for system operators, electricity consumers & generators, machines and regulators.

The Times They Are a-Chanin’

Large scale third parties provide the trust in today’s transaction systems and markets. They perform vital market roles such as identification, clearing, settling and keeping records.

But large scale third parties have downsides that blockchains can address. Blockchain offers advantages in three dimensions – security, access and efficiency.

A centralized system offers a single point of failure for attack – reducing security. Third parties can limit access to markets and data – reducing access. They introduce temporal & monetary costs by increasing the complexity of transactions – reducing efficiency.

Blockchain stores data on every member node – increasing security. Public blockchains are open to all, with individuals able to transact on an equal footing with large corporations – increasing access. Peer to peer transactions are made quickly and at low cost – improving efficiency.

Blockchain can also enable new kinds of transactions and business models. There are exciting applications in the energy industry beyond managing financial transactions – which I expand upon in Part Two.

The future of blockchain is not clear. There are significant technical, legal and regulatory challenges to overcome. Yet these challenges could be far smaller than the potential benefit blockchains could bring to our distributed and clean energy future.

Queen Chain Approximately

Two key technical innovations in blockchain are the decentralization of data storage and decentralization of verification. They both contribute towards the increased security, access and efficiency of the blockchain.

Decentralization of data storage improves security and access. Each member stores a copy of the entire blockchain. This removes the possibility losing data from a central database – improving security.

As each member stores the entire blockchain, she can access infomation about any transaction – democratizing data access. A public blockchain is open for all to join – democratizing market access.

Decentralization of verification improves access and efficiency. By allowing any node to verify transactions, no central authority can limit access to the market – improving access. Rewarding the fastest verification incentivizes reduced transaction time and cost – improving efficiency.

Verification of decentralization is what allows peer to peer transactions. The ability of the collective to verify transactions means you don’t need to wait for a central authority to authorize your transaction.

So decentralization of data and verification leads to improved security, access and efficiency. But what actually is allowing these improvements?

The magic of blockchain is how it maintains the true blockchain state. Blockchain truth is measured by the amount of a scarce resource attached to that blockchain. The scarce resource is supplied by blockchain members.

By making the correct blockchain require resource behind it, it makes proof of the blockchain rely on something that can’t be faked. This is the magic of the blockchain – making digital infomation behave like a scarce physical asset.

Falisfying the blockchain requires attaching more resource to a competitor blockchain. This means a blockchain is not immutable. A transaction can be modified by altering all subsequent blocks with the collusion of the majortiy of the blockchain (a 51% attack).

This fake blockchain could outcompete the true blockchain if enough resource was put behind it. However, once a true blockchain gets far enough ahead this task becomes very difficult.

Jenny From The Block

A key challenge in blockchain is what mechanism to use for validating transactions.  A simplifed description of the mechanics of a proof of work and a proof of stake blockchains are given below.  Currently there is no clear consensus on which mechanism is optimal – so there is significant innovation occuring in developing new mechanisms.

Proof of work

Private and public keys ensure the security of each member. A private key can access an account and execute a transaction. A public key can view the transaction history of the blockchain.

Although this public key is visible to all the identity of the member is not revealed. In this way a blockchain paradoxically provides both complete anonymity but no privacy about transaction history.

Blockchain members (known as nodes) run the blockchain. Each node can generate & digitally sign transactions using their private key. Transactions are added to a list held locally by the node and rapidly forwarded onto other nodes.

Figure 2 – the blockchain process (source = PWC)

It’s then the role of ‘miners’ (verification nodes) to create blocks from these lists of transactions. Miners first check the balances of both parties – available through the distributed blockchain. Miners then compete to solve a mathematical problem that allows transactions to be chained together. Solving the problem requires scarce real world resources (hardware and energy) meaning there is no way to cheat.

Figure 3 – the verification process (source = PWC)

Solving this problem is not computing anything useful – but requires computational resource to be solved correctly. This is how proof of work attaches a scarce resource (hardware and energy) to digital infomation (the true blockchain).

After the new block is added to the blockchain, it is propagated around the network in a similar fashion to transactions. Any conflicting transactions on nodes are discarded. It makes sense for other miners to add that validated block to their own blockchain and to work on the next block. This alignment of incentives allows the true blockchain to outgrow the others as more and more miners get rewarded to extend it.

The disadvantage of the proof of work consensus process is that all this computation requires a lot of electricity. This cost is also paid even if no one is trying to disrupt the blockchain.

In 2015 the electricity consumed in a single Bitcoin transaction was enough to power 1.57 American households for one day. If we assume an annual average consumption of 10.8 MWh and an electricity cost of £30/MWh, this equates to a cost of £1.60 per transaction. There is also the carbon impact of this electricity consumption.

Proof of stake

Proof of stake is a solution to the electricity consumption problem of proof of work. In a proof of stake system validators must place a deposit of cryptocurrency in their block. Blockchain truth is deterministically determined by the amount of currency attached.

In both systems the influence of a a user depends on the amount of scarce resource they put behind a blockchain. In a proof of work system the resource is computational; in a proof of stake system the resource is financial.

Smart contracts

The ability to deploy smart contracts is one of the blockchains key strengths. It’s how we can infuse a blockchain system with intelligence and movement. It enables new types of transactions, business models and markets on the blockchain.

A smart contract is an application that runs on the blockchain. Smart contracts improve the efficiency of the blockchain by automating manual processes. Smart contracts can allow new kinds of business models on the blockchain.

Smart contracts allow innovation to occur on a small scale. No central third party can limit the experimentation of smart contracts. A blockchain can support diverse ecosystem of smart contracts.

Bitcoin and Ethereum

Today the most largest and most visible use of the blockchain technology are in cryptocurrencies like bitcoin and Ethereum.

Bitcoin is both a cryptocurrency and a digital payment system. The main blockchain of bitcoin is public – anyone can mine, buy and trade bitcoin. The technology behind bitcoin is open source. Yet bitcoin is not an ideal blockchain implementation – on key KPIs such as throughput speed, latency bitcoin lags well behind VISA.

Storage of the blockchain is also an issue – in April 2017 the size of the bitcoin ledger was around 200 GB. If VISA were implemented using a blockchain it would take up around 200 PB per year. Electricity consumption is also a problem – in April 2017 bitcoin mining consumed around 300 MW of electricity.

Ethereum is a bitcoin competitor. Ethereum is also a public, open source blockchain with it’s own currency known as ‘ether’. The total value of both bitcoin and Ethererum in circulation is over $80B USD.

The key advantage of Ethereum over bitcoin is that it allows more flexible smart contracts. Ethereum has an integrated, Turing complete and all purpose programming language. Bitcoin’s language is not Turing complete – meaning there are solvable problems that bitcoin’s language cannot solve.

This limits the flexbility of smart contracts on the bitcoin implementation of blockchain. Bitcoin is limited to a currency and payment system. Ethererum could allow many different kinds of assets to be traded for many different business purposes.

Another difference between bitcoin and Ethereum is the algorithm used for reaching a consensus on the true state of the blockchain. Both historically use proof of work to reach a consensus on blockchain truth. Recently Ethereum is phasing in proof of stake as a lower cost way of reaching consensus on blockchain truth.

Thanks for reading!

Sources and further reading
White papers
Talks & Podcasts
Reports & Articles

A Glance at Q-Learning

‘A Glance at Q-Learning’ is a talk I recently gave at the Data Science Festival in London. The talk was one I also gave in Berlin at the Berlin Machine Learning group.

Q-Learning is a reinforcement learning algorithm that DeepMind used to play Atari games – work which some call the first step towards a general artificial intelligence. The original 2013 paper is available here (I cover this paper in the talk).

It was a wonderful experience being able to present – I recommend checking out more of the talks on the Data Science Festival YouTube – all of which are higher quality, more interesting and better presented than mine!

You can download a copy of my slides here – A Glance at Q-Learning slides.

Thanks for reading!

Elon Musk on autonomous cars – Energy Insights

Energy Insights highlights interesting energy content from around the web.

Previous posts include The Complexity of a Zero Carbon Grid and the CAISO Stage 1 Grid Emergency.


In this Energy Insights post I highlight an interesting insight from a TED talk with Elon Musk. The talk is as wide ranging as Musk’s talents – well worth a watch.

Impact of autonomous cars

Musk highlights that shared autonomous vehicles will likely become more affordable than a bus, leading to an increase in miles driven in cars.

“the amount of driving that will occur will be greater with shared autonomy and traffic will get far worse” – Elon Musk

Increasing traffic is not as significant a problem for passengers in autonomous cars. Time in an autonomous car can be spent productively. But what will be the impact on transport carbon emissions?

Let’s have a look at one positive and one neutral scenario. Both of these scenarios require a clean electricity grid power electric vehicles.

If the increased miles driven are spread across the fleet, then the only way for autonomous cars to lead to a global carbon saving is for electric cars to replace fossil fuels. The absolute number of fossil fuel cars must go down.

If all the increased miles driven are taken up by electric cars, then carbon emissions would stay stagnant. For me this seems like the route we must take – making sure that the bulk of the driving load is being done by electric vehicles. Ideally all autonomous cars are electric.

The number of fossil fuel cars will increase. BP expect an increase from 1 billion to 2 billion from now until 2050. This trend can’t be stopped. But we can smartly operate our passenger fleet to favour electric cars over fossil fuels.

Thanks for reading!

machine learning in energy – part two

This is the first of a two part series on the intersection of machine learning and the energy industry.

machine learning in energy – part one
Introduction, why it’s so exciting, challenges.

machine learning in energy – part two
Time series forecasting, energy disaggregation, reinforcement learning, Google data
centre optimization.


This post will detail three exciting applications of machine learning in energy:
– forecasting of electricity generation, consumption and price
– energy disaggregation
– reinforcement learning

We will also take a look at one of the most famous applications of machine learning in an energy system – Google’s work in their own data centers.

Forecasting of electricity generation, consumption and price

What’s the problem

In a modern electricity system time has a massive economic and environmental impact. The temporal variation in electricity generation and consumption can be significant. Periods of high consumption means generating electricity using expensive & inefficient peaking plants. In periods of low consumption electricity can be so abundant that the price becomes negative.

Electric grid stability requires a constant balance between generation and consumption. Understanding future balancing actions requires accurate forecasts by the system operator.

Our current energy transition is not making the forecasting problem any eaiser. We are moving away from dispatchable, centralized and large-scale generation towards intermittent, distributed and small scale generation.

Our current energy transition is moving us away from dispatchable, centralized and large-scale generation towards intermittent, distributed and small scale generation.

Historically the majority of generation was dispatchable and predictable – making forecasting easy. The only uncertainty was plant outages for unplanned maintenance.

Intermittent generation is by nature hard to forecast. Wind turbine power generation depends on forecasting wind speeds over vast areas. Solar power is more predictable but can still see variation as cloud cover changes.

As grid scale wind & solar penetration increase balancing the grid is more difficult. Higher levels of renewables can lead to more fossil fuel backup kept in reserve in case forecasts are wrong.

It’s not just forecasting of generation that has become more challenging.  The distributed and small scale of many wind & solar plants is also making consumption forecasting more difficult.

A solar panel sitting on a residential home is not directly metered – the system operator has no idea it is there. As this solar panel generates throughout the day it appears to the grid as reduced consumption.

Our current energy transition is a double whammy for grid balancing. Forecasting of both generation and consumption is becoming more challenging.

This has a big impact on electricity prices. In a wholesale electricity market price is set by the intersection of generation and consumption. Volatility and uncertainty on both sides spill over into more volatile electricity prices.

How machine learning will help

Many supervised machine learning models can be used for time series forecasting. Both regression and classification models are able to help understand the future.

Regression models can directly forecast electricity generation, consumption and price. Classification models can forecast the probability of a spike in electricity prices.

Well trained random forests, support vector machines and neural networks can all be used to solve these problems.

A key challenge is data. As renewables are weather driven forecasts of weather can be useful exogenous variables. It’s key that we only train models on data that will be available at the time of the forecast. This means that historical information about weather forecasts can be more useful than the actual weather data.

What’s the value to the world

Improving forecasts allows us to better balance the grid, reduce fossil fuels and increase renewables.

It’s not only the economic & environmental cost of keeping backup plant spinning. Incorrect forecasts can lead to fossil fuel generators paid to reduce output. This increases the cost to supply electricity to customers.

There are benefits for end consumers of electricity as well. Improved prediction can also allow flexible electricity consumption to respond to market signals.

More accurate forecasts that can look further ahead will allow more electricity consumers to be flexible. Using flexible assets to manage the grid will reduce our reliance on fossil fuels for grid balancing.

Sources and further reading

– Forecasting UK Imbalance Price using a Multilayer Perceptron Neural Network
Machine Learning in Energy (Fayadhoi Ibrahima)
7 reasons why utilities should be using machine learning
Germany enlists machine learning to boost renewables revolution
Weron (2014) Electricity price forecasting: A review of the state-of-the-art with a look into the future

Energy disaggregation

What’s the problem

Imagine if every time you went to the restaurant you only got the total bill. Understanding the line by line breakdown of where your money went is valuable. Energy disaggregation can help give customers this level of infomation about their utility bill.

Energy disaggregation estimates appliance level consumption using only total consumption.

In an ideal world we would have visibility of each individual consumer of energy. We would know when a TV is on or a pump is running in an industrial process. One solution would be to install metering on every consumer – a very expensive and complex process.

Energy disaggregation is a more elegant solution. A good energy disaggregation model can estimate appliance level consumption through a single aggregate meter.

How machine learning will help

Supervised machine learning is all about learning patterns in data. Many supervised machine learning algorithms can learn the patterns in the total consumption. Kelly & Knottenbelt (2015) used recurrent and convolutional neural networks to disaggregate residential energy consumptions.

A key challenge is data. Supervised learning requires labeled training data. Measurement and identification of sub-consumers forms training data for a supervised learner. Data is also required at a very high temporal frequency – ideally less than one second.

What’s the value to the world

Energy disaggregation has two benefits for electricity consumers. It can identify & verify savings opportunities. It can also increase customer engagement.

Imagine if you got an electricity bill that told you how much it cost you to run your dishwasher that month. The utility could help customers understand what they could have saved if they ran their dishwasher at different times.

This kind of feedback can be very effective in increasing customer engagement – a key challenge for utilities around the world.

Sources and further reading

7 reasons why utilities should be using machine learning
Neural NILM: Deep Neural Networks Applied to Energy Disaggregation
– Energy Disaggregation: The Holy Grail (Carrie Armel)
– Putting Energy Disaggregation Tech to the Test

Reinforcement learning

What’s the problem

Controlling energy systems is hard. Key variables such as price and energy consumption constantly change. Operators control systems with a large number of actions, with the optimal action changing throughout the day.

Our current energy transition is making this problem even harder. The transition is increasing volatility in key variables (such as electricity prices) and the number of actions to choose from.

Today deterministic sets of rules or abstract models are used to guide operation. Deterministic rules for operating any non-stationary system can’t guarantee optimality. Changes in key variables can turn a profitable operation to one that loses money.

Abstract models (such as linear programming) can account for changes in key variables. But abstract models often force the use of unrealistic models of energy systems. More importantly the performance of the model is limited by the skill and experience of the modeler.

How machine learning will help

Reinforcement learning gives a machine the ability to learn to take actions. The machine takes actions in an environment to optimize a reward signal. In the context of an energy system that reward signal could be energy cost, carbon or safety – whatever behavior we want to incentivize.

reinforcement_learning_in_energy

What is exciting about reinforcement learning is that we don’t need to build any domain knowledge into the model. A reinforcement learner learns from its own experience of the environment. This allows a reinforcement learner to see patterns that we can’t see – leading to superhuman levels of performance.

Another exciting thing about reinforcement learning is that you don’t need a data set. All you need is an environment (real or virtual) that the learner can interact with.

What’s the value to the world

Better control of our energy systems will allow us to reduce cost, reduce environmental impact and improve safety. Reinforcement learning allows us to do this at superhuman levels of performance.

Sources and further reading

– energy_py – reinforcement learning in energy systems
Minh et. al (2016) Human-level control through deep reinforcement learning
Reinforcement learning course by David Silver (Google DeepMind)

Alphabet/Google data centre optimization

One of the most famous applications of machine learning in an energy system is Google’s work in their own data centers.

In 2014 Google used supervised machine learning to predict the Power Usage Effectiveness (PUE) of data centres.

This supervised model did no control of its own. Operators used the predictive model to create a target PUE for the plant. The predictive model also allowed operators to simulate the impact of changes in key parameters on PUE.

In 2016 DeepMind published details of a how they applied machine learning to optimizing data centre efficiency. The technical details of this implementation are not as clear as the 2014 work. It is pretty clear that both supervised and reinforcement learning techniques were used.

The focus on the project again was on improving PUE. Deep neural networks predicted future PUE as well as future temperatures & pressures. The predictions of future temperature & pressures simulated the effect of recommended actions.

DeepMind claim a ’40 percent reduction in the amount of energy used for cooling’ which equates to a ’15 percent reduction in overall PUE overhead after accounting for electrical losses and other non-cooling inefficiencies’. Without seeing actual data it’s hard to know exactly what this means.

What I am able to understand is that this ‘produced the lowest PUE the site had ever seen’.

This is why as an energy engineer I’m so excited about machine learning. Google’s data centers were most likely well optimized before these projects. The fact that machine learning was able to improve PUE beyond what human operators had been able to achieve before is inspiring.

The potential level of savings across the rest of our energy systems is exciting to think about. The challenges & impact of our energy systems are massive – we need the intelligence of machine learning to help us solve these challenges.

Sources and further reading

Jim Gao (Google) – Machine Learning Applications for Data Center Optimization
– DeepMind AI Reduces Google Data Centre Cooling Bill by 40%

Thanks for reading!

 

CAISO Stage 1 Grid Emergency – Energy Insights

Energy Insights highlights interesting energy content from around the web.

Previous posts include The Complexity of a Zero Carbon Grid and the 2017 BP Energy Outlook.


California Grid Emergency Comes Days After Reliability Warning


On May 3rd 2017 the California grid experienced its first Stage 1 grid emergency in nearly a decade.

The reasons for this emergency notice were:
– a 330 MW gas-fired plant outage
– 800 MW of imports that were unavailable
– a demand forecasting error of 2 GW

A Stage 1 grid emergency doesn’t mean a blackout – it forces the ISO to dip into reserves and slip below required reserve margins.  It allows CAISO to access interruptible demand side managment programs.

I wanted to highlight two features of this event I found interesting.

1 – The demand forecasting error of 2 GW

This is a massive error in absolute terms – equivalent to a large power station!

To put this error in perspective demand on the 11th of May for the same time period was around 28 GW – giving a relative error of around 7%.

It’s important to note that this error isn’t actually an error in forecasting the actual demand – it’s distributed & small scale solar that is appearing to the ISO as reduced demand.

2 – Lack of flexibility

It was unusual that the issues began developing around the peak, and demand wasn’t ramping down much, but solar was ramping off faster than what the thermal units online at the time could keep up with in serving loadCAISO spokesperson Steven Greenlee

In a previous post I highlighted the concept of flexibility.  This event demonstrates why flexibility is so important for managing a modern electric grid.

Even if you have the capacity (MW) you might not have the flexibility (MW/min) to cope with the intermittent nature of renewables.

It’s also made clear in the RTO article that interruptible demand side management programs are only called upon in a Stage 1 emergency.  Prior to this thermal units are used to balance the system.

Using flexible demand side assets as a first step to balance the grid could be a more optimal way to deal with this problem.

Thanks for reading!