Instituto Superior Técnico
Mestrado Bolonha em Engenharia
Eletrotécnica e de Computadores
Data Analytics for Smart Grids
Analítica de Dados para Redes Inteligentes:
Report
Trabalho realizado por: Número:
João Brum 89210
Ivan D’Amante 104307
Professor Regente: Pedro Manuel Santos de Carvalho
Professor de Laboratório: Hugo Gabriel Valente Morais
Lisboa, 8 de abril de 2022
Contents
Introduction III
5 Markov Dynamics, Viterbi, Forecast 1
5.1 Problem description . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
5.2 Implementation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
5.2.1 Task 1: Viterbi Path . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
5.3 Task 2: Markov chain trajectories in the state space of injections . . . . . . . . 3
List of Figures
Figure 5.1 24-period non-homogeneous Markov chain trajectories in the state space
of Xt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Figure 5.2 Optimal Viterbi path in the state space of the injections that minimises
the estimation error εt,i . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
II
Introduction
The goal of this report is to summarise and give a general overview about the methods
used during the Programming and Computing Part of this ADRI’s class and to comment the
Python code and the obtained figures of these simulations.
III
Problem Class 5
Markov Dynamics, Viterbi, Forecast
5.1 Problem description
Problem-5 deals the structural dynamics of the analysed data can be assessed and repre-
sented by Markov chains. The very first part of the Python script consists on setting the
problem from the grid point of view. Afterwards the Markov Model has been introduced.
By knowing the measurement values of yt and Wt , with t = 1, . . . , t0 we first have to be
able to estimate the stochastic process X(t), which comes from a set of variables. Because
Xt is a Markov process whose state cannot be observed directly in real-time, the problem of
estimating Xt0 based on yt and Wt is a Hidden Markov Model (HMM) problem.
After having setting up the tran-
sition matrices and the optimi-
sation problem, the script plot-
ted the Markov chain trajec-
tories in the state space of
Xt obtained by sampling with
Pr(Xt+1 = x|Xt = xt ) for x1 =
s1 , see Figure 5.1. After hav-
ing defined the output probabil-
ity density function P r(yt |si ) as
a function of the observed out-
puts, yt and Wt , the Viterbi val-
ues Vt,i could finally be com-
puted for each state si .
Figure 5.1: 24-period non-homogeneous Markov chain tra-
jectories in the state space of Xt .
1
ADRI
2021-2022, MEEC
5.2 Implementation
5.2.1 Task 1: Viterbi Path
We now have to compute the Viterbi probabilities
(x,s )
Vt,i = max Pr(yt |si )pt−1 i Vt−1,x . (5.1)
x∈S
These were computed recursively iterating forward in time as shown in the Code 5.1.
# Compute V1 , i
VV [: ,0]= np . multiply ( pv [: ,0] , np . transpose ( pi_k [0 ,:]) )
aux = np . zeros (( pij . shape [1] , pij . shape [1]) )
# Recursively iterate forward in time to compute V1 , i .
# The matrix VV gave the probability to be in each state in period 24
# The matrix ptr is the pointer with the information about the most probable
previous state
for t in range (1 , kPeriod ) :
gg = np . reshape ( g [t ,:] ,( nStates , nStates ) )
pij = np . transpose ( np . multiply ( np . transpose ( nij ) , gg ) )
for stage in range ( nStates ) :
aux [ stage ,:]= pv [: ,t -1]* pij [: , stage ]+ VV [: ,t -1] # calculates every
probability and stores in the array
VV [: , t ] = np . amax ( aux , axis =1) # takes the highest probability for every
state
ptr [: , t ]= np . argmax ( aux , axis =1) # takes the state / cell where the highest
probability is of each state
# Most probable state in t =24
maxP = np . max ( VV [: , kPeriod -1]) # Max Probability
maxPstate = np . argmax ( VV [: , kPeriod -1]) # State of max probability
# Start the backward vectors
Ss [ kPeriod -1] = maxPstate # Start in s4 in t =23 ( the last )
Vs [ kPeriod -1] = maxP # Probality of s4 in the same timestamp
# Build backwards
for temp in range ( kPeriod -1 ,0 , -1) :
Ss [ temp -1]= ptr [ np . int ( Ss [ temp ]) , temp ] # Get the best route of states
Vs [ temp -1]= VV [ np . int ( Ss [ temp -1]) , temp -1] # Get the probability of the
same route Ss
e_min [ temp -1]= ee [ np . int ( Ss [ temp -1]) , temp -1] # Error of the path it ’s
taking
Listing 5.1: Viterbi probabilities Vt,i .
Due to the fact that our matrix VV in t = 0 has 0 in some rows, the equation had to be
2
ADRI
2021-2022, MEEC
(x,s )
adjusted (5.1) as follows, Vt,i = maxx∈S Pr(yt |si )pt−1 i + Vt−1,x . Otherwise the calculations
for a certain state would all turn out as zero. Moreover maxP indicates the highest probability
at time t0 = 23 (24th timestamp) and maxPstate the state si at that timestamp. As for the
error calculation it was used a pre-calculated matrix so the values would be the same as in
the lecture notes.
5.3 Task 2: Markov chain trajectories in the state space
of injections
# Create matrices
nSamplesV =500
Xsv = np . zeros (([ nSamplesV , kPeriod ]) ) # Matrix to fill with all the possible
paths backwards starting from s4 in t =23
xxv = np . zeros (( nStates , nStates ) )
# for each sample
for ii in range ( nSamplesV ) :
# Create matrix of states
xxv = np . zeros ( kPeriod )
# The initial state is s4
xxv [ kPeriod -1]=5
# Generate a random value .
# This value is used to define the transition between periods k
tk = np . random . random ( kPeriod -1)
# Definition of the trajetories in the state space
for t in range ( kPeriod -2 , -1 , -1) :
ggv = np . reshape ( g [t ,:] ,( nStates , nStates ) )
pijv = np . transpose ( np . multiply ( np . transpose ( nij ) , ggv ) ) # Eq .39
iisv = np . int ( xxv [ t +1]) # State in the previous period
cum =0
xxv [ t ]= xxv [ t +1]
# Definition of the state in next period
for j in range ( nStates ) :
cum = cum + pijv [ iisv -1 , j ]
if cum >= tk [ t ]:
xxv [ t ]= j +1
break
Xsv [ ii ,:]=( xxv -4) /2 # transforms results from 0 to 5 to the value of
each state ( -1.5 , -1.0 , -0.5 ,0 ,0.5 ,1.0 ,1.5)
# Most probable path defined in the previous period .
# In that case considering the state values " s "
xsv = (( Ss +1) -4) /2
Listing 5.2: Markov chain trajectories in the state space of injections.
3
ADRI
2021-2022, MEEC
Figure 5.2 shows (thick blue line) the Viterbi optimal path. The most probable state at times-
tamp t0 = 23 is state s4 , as depicted in the graph. This value had been used in Code 5.2 to
as the initial state for the backwards iteration, as shown by the variable xxv[kPeriod-1]=5.
The trajectories in the state space were computed using the transition probabilities nij by
multiplying each element pij by some adjusting coefficients according to [1, Eq.(39)].
S5
S4
S3
i
State S
S2
S1
S0
0 5 10 15 20
Time Stamp [h]
Figure 5.2: Optimal Viterbi path in the state space of the injections that minimises the
estimation error εt,i .
4
Bibliography
[1] P. M. Santos de Carvalho, Lecture notes in data analytics for smart grids, Instituto
Superior Técnico, Universidade de Lisboa, 2021.