# Player Performance Estimation using AI Collaborative Filtering

## 1. Introduction

Often times before crucial matches, or in general, we would like to know the performance of a batsman against a bowler or vice-versa, but we may not have the data. We generally have data where different batsmen would have faced different sets of bowlers with certain performance data like ballsFaced, totalRuns, fours, sixes, strike rate and timesOut. Similarly different bowlers would have performance figures(deliveries, runsConceded, economyRate and wicketTaken) against different sets of batsmen. We will never have the data for all batsmen against all bowlers. However, it would be good estimate the performance of batsmen against a bowler, even though we do not have the performance data. This could be done using collaborative filtering which identifies and computes based on the similarity between batsmen vs bowlers & bowlers vs batsmen.

This post shows an approach whereby we can estimate a batsman’s performance against bowlers even though the batsman may not have faced those bowlers, based on his/her performance against other bowlers. It also estimates the performance of bowlers against batsmen using the same approach. This is based on the recommender algorithm which is used to recommend products to customers based on their rating on other products.

This idea came to me while generating the performance of batsmen vs bowlers & vice-versa for 2 IPL teams in this IPL 2022 with my Shiny app GooglyPlusPlus in the optimization tab, I found that there were some batsmen for which there was no data against certain bowlers, probably because they are playing for the first time in their team or because they were new (see picture below)

In the picture above there is no data for Dewald Brevis against Jasprit Bumrah and YS Chahal. Wouldn’t be great to estimate the performance of Brevis against Bumrah or vice-versa? Can we estimate this performance?

While pondering on this problem, I realized that this problem formulation is similar to the problem formulation for the famous Netflix movie recommendation problem, in which user’s ratings for certain movies are known and based on these ratings, the recommender engine can generate ratings for movies not yet seen.

This post estimates a player’s (batsman/bowler) using the recommender engine This post is based on R package recommenderlab

“Michael Hahsler (2021). recommenderlab: Lab for Developing and Testing Recommender Algorithms. R package version 0.2-7. https://github.com/mhahsler/recommenderlab

Note 1: Thw data for this analysis is taken from Cricsheet after being processed by my R package yorkr.

You can also read this post in RPubs at Player Performance Estimation using AI Collaborative Filtering

A PDF copy of this post is available at Player Performance Estimation using AI Collaborative Filtering.pdf

You can download this R Markdown file and the associated data and perform the analysis yourself using any other recommender engine from Github at playerPerformanceEstimation

## Problem statement

In the table below we see a set of bowlers vs a set of batsmen and the number of times the bowlers got these batsmen out.
By knowing the performance of the bowlers against some of the batsmen we can use collaborative filter to determine the missing values. This is done using the recommender engine.

The Recommender Engine works as follows. Let us say that there are feature vectors $x^1$, $x^2$ and $x^3$ for the 3 bowlers which identify the characteristics of these bowlers (“fast”, “lateral drift through the air”, “movement off the pitch”). Let each batsman be identified by parameter vectors $\theta^1$, $\theta^2$ and so on

For e.g. consider the following table

Then by assuming an initial estimate for the parameter vector $\theta$ and the feature vector xx we can formulate this as an optimization problem which tries to minimize the error for $\theta^T*x$ This can work very well as the algorithm can determine features which cannot be captured. So for e.g. some particular bowler may have very impressive figures. This could be due to some aspect of the bowling which cannot be captured by the data for e.g. let’s say the bowler uses the ‘scrambled seam’ when he is most effective, with a slightly different arc to the flight. Though the algorithm cannot identify the feature as we know it, but the ML algorithm should pick up intricacies which cannot be captured in data.

Hence the algorithm can be quite effective.

Note: The recommender lab performance is not very good and the Mean Square Error is quite high. Also, the ROC and AUC curves show that not in aLL cases the algorithm is doing a clean job of separating the True positives (TPR) from the False Positives (FPR)

Note: This is similar to the recommendation problem

The collaborative optimization object can be considered as a minimization of both $\theta$ and the features x and can be written as

J($x^{(1)},x^{(2)},..x^{(n_{u})}$, $\theta^{(1)},\theta^{(2)},..,\theta^{(n_{m})}$}= 1/2$\sum(\theta^{j})^{T}x^{i}- y^{(i,j)})^{2} + \lambda\sum\sum (x_{k}^{i})^{2} + \lambda\sum\sum (_\theta{k}^{j})^{2}$

The collaborative filtering algorithm can be summarized as follows

1. Initialize $\theta^1$, $\theta^2$$\theta^{n_{u}}$ and the set of features be $x^1$,$x^2$, … ,$x^{n_{m}}$ to small random values
2. Minimize J($\theta^1$, $\theta^2$$\theta^{n_{u}}$,$x^1$, $x^2$, … ,$x^{n_{m}}$) using gradient descent. For every
j=1,2, …$n_{u}$, i= 1,2,.., $n_{m}$
3. $x_{k}^{i}$ := $x_{k}^{i}$$\alpha$ ( $\sigma$ $(\theta^j)^T$)$x^i$$y^(i,j)\theta_{k}^{j} + \lambda x_{k}^i$

&

$\theta_{k}^{i}$ := $\theta_{k}^{i}$$\alpha$ ( $\sigma$ $(\theta^j)^T)x^i - y^(i,j)\theta_{k}^{j} + \lambda x_{k}^i$
4. Hence for a batsman with parameters $\theta$ and a bowler with (learned) features x, predict the “times out” for the player where the value is not known using $\theta^Tx$

The above derivation for the recommender problem is taken from Machine Learning by Prof Andrew Ng at Coursera from the lecture Collaborative filtering

There are 2 main types of Collaborative Filtering(CF) approaches

1. User based Collaborative Filtering User-based CF is a memory-based algorithm which tries to mimics word-of-mouth by analyzing rating data from many individuals. The assumption is that users with similar preferences will rate items similarly.
2. Item based Collaborative Filtering Item-based CF is a model-based approach which produces recommendations based on the relationship between items inferred from the rating matrix. The assumption behind this approach is that users will prefer items that are similar to other items they like.

## 1a. A note on ROC and Precision-Recall curves

A small note on interpreting ROC & Precision-Recall curves in the post below

ROC Curve: The ROC curve plots the True Positive Rate (TPR) against the False Positive Rate (FPR). Ideally the TPR should increase faster than the FPR and the AUC (area under the curve) should be close to 1

Precision-Recall: The precision-recall curve shows the tradeoff between precision and recall for different threshold. A high area under the curve represents both high recall and high precision, where high precision relates to a low false positive rate, and high recall relates to a low false negative rate

library(reshape2)
library(dplyr)
library(ggplot2)
library(recommenderlab)
library(tidyr)


## 2. Define recommender lab helper functions

Helper functions for the RMarkdown notebook are created

• eval – Gives details of RMSE, MSE and MAE of ML algorithm
• evalRecomMethods – Evaluates different recommender methods and plot the ROC and Precision-Recall curves
# This function returns the error for the chosen algorithm and also predicts the estimates
# for the given data
eval <- function(data, train1, k1,given1,goodRating1,recomType1="UBCF"){
set.seed(2022)
e<- evaluationScheme(data,
method = "split",
train = train1,
k = k1,
given = given1,
goodRating = goodRating1)

r1 <- Recommender(getData(e, "train"), recomType1)
print(r1)

p1 <- predict(r1, getData(e, "known"), type="ratings")
print(p1)

error = calcPredictionAccuracy(p1, getData(e, "unknown"))

print(error)
p2 <- predict(r1, data, type="ratingMatrix")
p2
}
# This function will evaluate the different recommender algorithms and plot the AUC and ROC curves
evalRecomMethods <- function(data,k1,given1,goodRating1){
set.seed(2022)
e<- evaluationScheme(data,
method = "cross",
k = k1,
given = given1,
goodRating = goodRating1)

models_to_evaluate <- list(
IBCF Cosinus = list(name = "IBCF",
param = list(method = "cosine")),
IBCF Pearson = list(name = "IBCF",
param = list(method = "pearson")),
UBCF Cosinus = list(name = "UBCF",
param = list(method = "cosine")),
UBCF Pearson = list(name = "UBCF",
param = list(method = "pearson")),
Zufälliger Vorschlag = list(name = "RANDOM", param=NULL)
)

n_recommendations <- c(1, 5, seq(10, 100, 10))
list_results <- evaluate(x = e,
method = models_to_evaluate,
n = n_recommendations)
plot(list_results, annotate=c(1,3), legend="bottomright")
plot(list_results, "prec/rec", annotate=3, legend="topleft")
}


## 3. Batsman performance estimation

The section below regenerates the performance for batsmen based on incomplete data for the different fields in the data frame namely balls faced, fours, sixes, strike rate, times out. The recommender lab allows one to test several different algorithms all at once namely

1. User based – Cosine similarity method, Pearson similarity
2. Item based – Cosine similarity method, Pearson similarity
3. Popular
4. Random
5. SVD and a few others

## 3a. Batting dataframe

head(df)

##   batsman1         bowler1 ballsFaced totalRuns fours sixes  SR timesOut
## 1 A Badoni        A Mishra          0         0     0     0 NaN        0
## 2 A Badoni        A Nortje          0         0     0     0 NaN        0
## 3 A Badoni         A Zampa          0         0     0     0 NaN        0
## 4 A Badoni     Abdul Samad          0         0     0     0 NaN        0
## 5 A Badoni Abhishek Sharma          0         0     0     0 NaN        0
## 6 A Badoni      AD Russell          0         0     0     0 NaN        0


## 3b Data set and data preparation

For this analysis the data from Cricsheet has been processed using my R package yorkr to obtain the following 2 data sets – batsmenVsBowler – This dataset will contain the performance of the batsmen against the bowler and will capture a) ballsFaced b) totalRuns c) Fours d) Sixes e) SR f) timesOut – bowlerVsBatsmen – This data set will contain the performance of the bowler against the difference batsmen and will include a) deliveries b) runsConceded c) EconomyRate d) wicketsTaken

Obviously many rows/columns will be empty

This is a large data set and hence I have filtered for the period > Jan 2020 and < Dec 2022 which gives 2 datasets a) batsmanVsBowler20_22.rdata b) bowlerVsBatsman20_22.rdata

I also have 2 other datasets of all batsmen and bowlers in these 2 dataset in the files c) all-batsmen20_22.rds d) all-bowlers20_22.rds

You can download the data and this RMarkdown notebook from Github at PlayerPerformanceEstimation

Feel free to download and analyze the data and use any recommendation engine you choose

## 3c. Exploratory analysis

Initially an exploratory analysis is done on the data

df3 <- select(df, batsman1,bowler1,timesOut)
df6 <- xtabs(timesOut ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
print(df8[1:10,1:10])

##                 A Mishra A Nortje A Zampa Abdul Samad Abhishek Sharma
## A Badoni              NA       NA      NA          NA              NA
## A Manohar             NA       NA      NA          NA              NA
## A Nortje              NA       NA      NA          NA              NA
## AB de Villiers        NA        4       3          NA              NA
## Abdul Samad           NA       NA      NA          NA              NA
## Abhishek Sharma       NA       NA      NA          NA              NA
## AD Russell             1       NA      NA          NA              NA
## AF Milne              NA       NA      NA          NA              NA
## AJ Finch              NA       NA      NA          NA               3
## AJ Tye                NA       NA      NA          NA              NA
##                 AD Russell AF Milne AJ Tye AK Markram Akash Deep
## A Badoni                NA       NA     NA         NA         NA
## A Manohar               NA       NA     NA         NA         NA
## A Nortje                NA       NA     NA         NA         NA
## AB de Villiers           3       NA      3         NA         NA
## Abdul Samad             NA       NA     NA         NA         NA
## Abhishek Sharma         NA       NA     NA         NA         NA
## AD Russell              NA       NA      6         NA         NA
## AF Milne                NA       NA     NA         NA         NA
## AJ Finch                NA       NA     NA         NA         NA
## AJ Tye                  NA       NA     NA         NA         NA


The dots below represent data for which there is no performance data. These cells need to be estimated by the algorithm

set.seed(2022)
r <- as(df8,"realRatingMatrix")
getRatingMatrix(r)[1:15,1:15]

## 15 x 15 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 15 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## A Badoni         . . . . . . . . . . . . . . .
## A Manohar        . . . . . . . . . . . . . . .
## A Nortje         . . . . . . . . . . . . . . .
## AB de Villiers   . 4 3 . . 3 . 3 . . . 4 3 . .
## Abdul Samad      . . . . . . . . . . . . . . .
## Abhishek Sharma  . . . . . . . . . . . 1 . . .
## AD Russell       1 . . . . . . 6 . . . 3 3 3 .
## AF Milne         . . . . . . . . . . . . . . .
## AJ Finch         . . . . 3 . . . . . . 1 . . .
## AJ Tye           . . . . . . . . . . . 1 . . .
## AK Markram       . . . 3 . . . . . . . . . . .
## AM Rahane        9 . . . . 3 . 3 . . . 3 3 . .
## Anmolpreet Singh . . . . . . . . . . . . . . .
## Anuj Rawat       . . . . . . . . . . . . . . .
## AR Patel         . . . . . . . 1 . . . . . . .

r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:15,1:15]

## 15 x 15 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 15 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## AB de Villiers  . 4 3 . . 3 . 3 . . . 4 3 . .
## Abdul Samad     . . . . . . . . . . . . . . .
## Abhishek Sharma . . . . . . . . . . . 1 . . .
## AD Russell      1 . . . . . . 6 . . . 3 3 3 .
## AJ Finch        . . . . 3 . . . . . . 1 . . .
## AM Rahane       9 . . . . 3 . 3 . . . 3 3 . .
## AR Patel        . . . . . . . 1 . . . . . . .
## AT Rayudu       2 . . . . . 1 . . . . 3 . . .
## B Kumar         3 . 3 . . . . . . . . . . 3 .
## BA Stokes       . . . . . . 3 4 . . . 3 . . .
## CA Lynn         . . . . . . . 9 . . . 3 . . .
## CH Gayle        . . . . . 6 . 3 . . . 6 . . .
## CH Morris       . 3 . . . . . . . . . 3 . . .
## D Padikkal      . 4 . . . 3 . . . . . . 3 . .
## DA Miller       . . . . . 3 . . . . . 3 . . .

# Get the summary of the data
summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##   1.000   3.000   3.000   3.463   4.000  21.000

# Normalize the data
r0_m <- normalize(r0)
getRatingMatrix(r0_m)[1:15,1:15]

## 15 x 15 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 15 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## AB de Villiers   .         -0.7857143 -1.7857143 .  .       -1.7857143
## Abdul Samad      .          .          .         .  .        .
## Abhishek Sharma  .          .          .         .  .        .
## AD Russell      -2.6562500  .          .         .  .        .
## AJ Finch         .          .          .         . -0.03125  .
## AM Rahane        4.6041667  .          .         .  .       -1.3958333
## AR Patel         .          .          .         .  .        .
## AT Rayudu       -2.1363636  .          .         .  .        .
## B Kumar          0.3636364  .          0.3636364 .  .        .
## BA Stokes        .          .          .         .  .        .
## CA Lynn          .          .          .         .  .        .
## CH Gayle         .          .          .         .  .        1.5476190
## CH Morris        .          0.3500000  .         .  .        .
## D Padikkal       .          0.6250000  .         .  .       -0.3750000
## DA Miller        .          .          .         .  .       -0.7037037
##
## AB de Villiers   .         -1.7857143 . . . -0.7857143 -1.785714  .         .
## Abdul Samad      .          .         . . .  .          .         .         .
## Abhishek Sharma  .          .         . . . -1.6000000  .         .         .
## AD Russell       .          2.3437500 . . . -0.6562500 -0.656250 -0.6562500 .
## AJ Finch         .          .         . . . -2.0312500  .         .         .
## AM Rahane        .         -1.3958333 . . . -1.3958333 -1.395833  .         .
## AR Patel         .         -2.3333333 . . .  .          .         .         .
## AT Rayudu       -3.1363636  .         . . . -1.1363636  .         .         .
## B Kumar          .          .         . . .  .          .         0.3636364 .
## BA Stokes       -0.6086957  0.3913043 . . . -0.6086957  .         .         .
## CA Lynn          .          5.3200000 . . . -0.6800000  .         .         .
## CH Gayle         .         -1.4523810 . . .  1.5476190  .         .         .
## CH Morris        .          .         . . .  0.3500000  .         .         .
## D Padikkal       .          .         . . .  .         -0.375000  .         .
## DA Miller        .          .         . . . -0.7037037  .         .         .


## 4. Create a visual representation of the rating data before and after the normalization

The histograms show the bias in the data is removed after normalization

r0=r[(m=rowCounts(r) > 10),]
getRatingMatrix(r0)[1:15,1:10]

## 15 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## AB de Villiers  . 4 3 . . 3 . 3 . .
## Abdul Samad     . . . . . . . . . .
## Abhishek Sharma . . . . . . . . . .
## AD Russell      1 . . . . . . 6 . .
## AJ Finch        . . . . 3 . . . . .
## AM Rahane       9 . . . . 3 . 3 . .
## AR Patel        . . . . . . . 1 . .
## AT Rayudu       2 . . . . . 1 . . .
## B Kumar         3 . 3 . . . . . . .
## BA Stokes       . . . . . . 3 4 . .
## CA Lynn         . . . . . . . 9 . .
## CH Gayle        . . . . . 6 . 3 . .
## CH Morris       . 3 . . . . . . . .
## D Padikkal      . 4 . . . 3 . . . .
## DA Miller       . . . . . 3 . . . .

#Plot ratings
image(r0, main = "Raw Ratings")

#Plot normalized ratings
r0_m <- normalize(r0)
getRatingMatrix(r0_m)[1:15,1:15]

## 15 x 15 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 15 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## AB de Villiers   .         -0.7857143 -1.7857143 .  .       -1.7857143
## Abdul Samad      .          .          .         .  .        .
## Abhishek Sharma  .          .          .         .  .        .
## AD Russell      -2.6562500  .          .         .  .        .
## AJ Finch         .          .          .         . -0.03125  .
## AM Rahane        4.6041667  .          .         .  .       -1.3958333
## AR Patel         .          .          .         .  .        .
## AT Rayudu       -2.1363636  .          .         .  .        .
## B Kumar          0.3636364  .          0.3636364 .  .        .
## BA Stokes        .          .          .         .  .        .
## CA Lynn          .          .          .         .  .        .
## CH Gayle         .          .          .         .  .        1.5476190
## CH Morris        .          0.3500000  .         .  .        .
## D Padikkal       .          0.6250000  .         .  .       -0.3750000
## DA Miller        .          .          .         .  .       -0.7037037
##
## AB de Villiers   .         -1.7857143 . . . -0.7857143 -1.785714  .         .
## Abdul Samad      .          .         . . .  .          .         .         .
## Abhishek Sharma  .          .         . . . -1.6000000  .         .         .
## AD Russell       .          2.3437500 . . . -0.6562500 -0.656250 -0.6562500 .
## AJ Finch         .          .         . . . -2.0312500  .         .         .
## AM Rahane        .         -1.3958333 . . . -1.3958333 -1.395833  .         .
## AR Patel         .         -2.3333333 . . .  .          .         .         .
## AT Rayudu       -3.1363636  .         . . . -1.1363636  .         .         .
## B Kumar          .          .         . . .  .          .         0.3636364 .
## BA Stokes       -0.6086957  0.3913043 . . . -0.6086957  .         .         .
## CA Lynn          .          5.3200000 . . . -0.6800000  .         .         .
## CH Gayle         .         -1.4523810 . . .  1.5476190  .         .         .
## CH Morris        .          .         . . .  0.3500000  .         .         .
## D Padikkal       .          .         . . .  .         -0.375000  .         .
## DA Miller        .          .         . . . -0.7037037  .         .         .

image(r0_m, main = "Normalized Ratings")

set.seed(1234)
hist(getRatings(r0), breaks=25)

hist(getRatings(r0_m), breaks=25)


## 4a. Data for analysis

The data frame of the batsman vs bowlers from the period 2020 -2022 is read as a dataframe. To remove rows with very low number of ratings(timesOut, SR, Fours, Sixes etc), the rows are filtered so that there are at least more 10 values in the row. For the player estimation the dataframe is converted into a wide-format as a matrix (m x n) of batsman x bowler with each of the columns of the dataframe i.e. timesOut, SR, fours or sixes. These different matrices can be considered as a rating matrix for estimation.

A similar approach is taken for estimating bowler performance. Here a wide form matrix (m x n) of bowler x batsman is created for each of the columns of deliveries, runsConceded, ER, wicketsTaken

## 5. Batsman’s times Out

The code below estimates the number of times the batsmen would lose his/her wicket to the bowler. As discussed in the algorithm above, the recommendation engine will make an initial estimate features for the bowler and an initial estimate for the parameter vector for the batsmen. Then using gradient descent the recommender engine will determine the feature and parameter values such that the over Mean Squared Error is minimum

From the plot for the different algorithms it can be seen that UBCF performs the best. However the AUC & ROC curves are not optimal and the AUC> 0.5

df3 <- select(df, batsman1,bowler1,timesOut)
df6 <- xtabs(timesOut ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
# Filter only rows where the row count is > 10
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## AB de Villiers  . 4 3 . . 3 . 3 . .
## Abdul Samad     . . . . . . . . . .
## Abhishek Sharma . . . . . . . . . .
## AD Russell      1 . . . . . . 6 . .
## AJ Finch        . . . . 3 . . . . .
## AM Rahane       9 . . . . 3 . 3 . .
## AR Patel        . . . . . . . 1 . .
## AT Rayudu       2 . . . . . 1 . . .
## B Kumar         3 . 3 . . . . . . .
## BA Stokes       . . . . . . 3 4 . .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##   1.000   3.000   3.000   3.463   4.000  21.000

# Evaluate the different plotting methods
evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

#Evaluate the error
a=eval(r0[1:dim(r0)[1]],0.8,k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 70 users.
## 18 x 145 rating matrix of class 'realRatingMatrix' with 1755 ratings.
##     RMSE      MSE      MAE
## 2.069027 4.280872 1.496388

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
m=as(c,"data.frame")
names(m) =c("batsman","bowler","TimesOut")


## 6. Batsman’s Strike rate

This section deals with the Strike rate of batsmen versus bowlers and estimates the values for those where the data is incomplete using UBCF method.

Even here all the algorithms do not perform too efficiently. I did try out a few variations but could not lower the error (suggestions welcome!!)

df3 <- select(df, batsman1,bowler1,SR)
df6 <- xtabs(SR ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## AB de Villiers   96.8254 171.4286  33.33333  . 66.66667 223.07692   .
## Abdul Samad       .      228.0000   .        .  .       100.00000   .
## Abhishek Sharma 150.0000   .        .        .  .        66.66667   .
## AD Russell      111.4286   .        .        .  .         .         .
## AJ Finch        250.0000 116.6667   .        . 50.00000  85.71429 112.5000
## AJ Tye            .        .        .        .  .         .       100.0000
## AK Markram        .        .        .       50  .         .         .
## AM Rahane       121.1111   .        .        .  .       113.82979 117.9487
## AR Patel        183.3333   .      200.00000  .  .       433.33333   .
## AT Rayudu       126.5432 200.0000 122.22222  .  .       105.55556   .
##
## AB de Villiers  109.52381 .   .
## Abdul Samad       .       .   .
## Abhishek Sharma   .       .   .
## AD Russell      195.45455 .   .
## AJ Finch          .       .   .
## AJ Tye            .       .   .
## AK Markram        .       .   .
## AM Rahane        33.33333 . 200
## AR Patel        171.42857 .   .
## AT Rayudu       204.76190 .   .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##   5.882  85.714 116.667 128.529 160.606 600.000

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

a=eval(r0[1:dim(r0)[1]],0.8, k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 105 users.
## 27 x 145 rating matrix of class 'realRatingMatrix' with 3220 ratings.
##       RMSE        MSE        MAE
##   77.71979 6040.36508   58.58484

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
n=as(c,"data.frame")
names(n) =c("batsman","bowler","SR")


## 7. Batsman’s Sixes

The snippet of code estimes the sixes of the batsman against bowlers. The ROC and AUC curve for UBCF looks a lot better here, as it significantly greater than 0.5

df3 <- select(df, batsman1,bowler1,sixes)
df6 <- xtabs(sixes ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## AB de Villiers  3 3 . . . 18 .  3 . .
## AD Russell      3 . . . .  . . 12 . .
## AJ Finch        2 . . . .  . .  . . .
## AM Rahane       7 . . . .  3 1  . . .
## AR Patel        4 . 3 . .  6 .  1 . .
## AT Rayudu       5 2 . . .  . .  1 . .
## BA Stokes       . . . . .  . .  . . .
## CA Lynn         . . . . .  . .  9 . .
## CH Gayle       17 . . . . 17 .  . . .
## CH Morris       . . 3 . .  . .  . . .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    1.00    3.00    3.00    4.68    6.00   33.00

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

## Timing stopped at: 0.003 0 0.002

a=eval(r0[1:dim(r0)[1]],0.8, k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 52 users.
## 14 x 145 rating matrix of class 'realRatingMatrix' with 1634 ratings.
##      RMSE       MSE       MAE
##  3.529922 12.460350  2.532122

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
o=as(c,"data.frame")
names(o) =c("batsman","bowler","Sixes")


## 8. Batsman’s Fours

The code below estimates 4s for the batsmen

df3 <- select(df, batsman1,bowler1,fours)
df6 <- xtabs(fours ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## AB de Villiers   . 1 . . . 24 . 3 . .
## Abhishek Sharma  . . . . .  . . . . .
## AD Russell       1 . . . .  . . 9 . .
## AJ Finch         . 1 . . .  3 2 . . .
## AK Markram       . . . . .  . . . . .
## AM Rahane       11 . . . .  8 7 . . 3
## AR Patel         . . . . .  . . 3 . .
## AT Rayudu       11 2 3 . .  6 . 6 . .
## BA Stokes        1 . . . .  . . . . .
## CA Lynn          . . . . .  . . 6 . .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##   1.000   3.000   4.000   6.339   9.000  55.000

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

## Timing stopped at: 0.008 0 0.008

## Warning in .local(x, method, ...):
##   Recommender 'UBCF Pearson' has failed and has been removed from the results!

a=eval(r0[1:dim(r0)[1]],0.8, k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 67 users.
## 17 x 145 rating matrix of class 'realRatingMatrix' with 2083 ratings.
##      RMSE       MSE       MAE
##  5.486661 30.103447  4.060990

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
p=as(c,"data.frame")
names(p) =c("batsman","bowler","Fours")


## 9. Batsman’s Total Runs

The code below estimates the total runs that would have scored by the batsman against different bowlers

df3 <- select(df, batsman1,bowler1,totalRuns)
df6 <- xtabs(totalRuns ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## A Badoni         .  . . . .   . .   . . .
## A Manohar        .  . . . .   . .   . . .
## A Nortje         .  . . . .   . .   . . .
## AB de Villiers  61 36 3 . 6 261 .  69 . .
## Abdul Samad      . 57 . . .  12 .   . . .
## Abhishek Sharma  3  . . . .   6 .   . . .
## AD Russell      39  . . . .   . . 129 . .
## AF Milne         .  . . . .   . .   . . .
## AJ Finch        15  7 . . 3  18 9   . . .
## AJ Tye           .  . . . .   . 4   . . .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    1.00    9.00   24.00   41.36   54.00  452.00

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given1=7,goodRating1=median(getRatings(r0)))

a=eval(r0[1:dim(r0)[1]],0.8, k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 105 users.
## 27 x 145 rating matrix of class 'realRatingMatrix' with 3256 ratings.
##       RMSE        MSE        MAE
##   41.50985 1723.06788   29.52958

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
q=as(c,"data.frame")
names(q) =c("batsman","bowler","TotalRuns")


## 10. Batsman’s Balls Faced

The snippet estimates the balls faced by batsmen versus bowlers

df3 <- select(df, batsman1,bowler1,ballsFaced)
df6 <- xtabs(ballsFaced ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Mishra', 'A Nortje', 'A Zampa' ... ]]

##
## A Badoni         .  . . . .   . .  . . .
## A Manohar        .  . . . .   . .  . . .
## A Nortje         .  . . . .   . .  . . .
## AB de Villiers  63 21 9 . 9 117 . 63 . .
## Abdul Samad      . 25 . . .  12 .  . . .
## Abhishek Sharma  2  . . . .   9 .  . . .
## AD Russell      35  . . . .   . . 66 . .
## AF Milne         .  . . . .   . .  . . .
## AJ Finch         6  6 . . 6  21 8  . . .
## AJ Tye           .  . . . .   9 4  . . .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    1.00    9.00   18.00   30.21   39.00  384.00

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

a=eval(r0[1:dim(r0)[1]],0.8, k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 112 users.
## 28 x 145 rating matrix of class 'realRatingMatrix' with 3378 ratings.
##       RMSE        MSE        MAE
##   33.91251 1150.05835   23.39439

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
r=as(c,"data.frame")
names(r) =c("batsman","bowler","BallsFaced")


## 11. Generate the Batsmen Performance Estimate

This code generates the estimated dataframe with known and ‘predicted’ values

a1=merge(m,n,by=c("batsman","bowler"))
a2=merge(a1,o,by=c("batsman","bowler"))
a3=merge(a2,p,by=c("batsman","bowler"))
a4=merge(a3,q,by=c("batsman","bowler"))
a5=merge(a4,r,by=c("batsman","bowler"))
a6= select(a5, batsman,bowler,BallsFaced,TotalRuns,Fours, Sixes, SR,TimesOut)

##          batsman          bowler BallsFaced TotalRuns Fours Sixes  SR TimesOut
## 1 AB de Villiers        A Mishra         94       124     7     5 144        5
## 2 AB de Villiers        A Nortje         26        42     4     3 148        3
## 3 AB de Villiers         A Zampa         28        42     5     7 106        4
## 4 AB de Villiers Abhishek Sharma         22        28     0    10 136        5
## 5 AB de Villiers      AD Russell         70       135    14    12 207        4
## 6 AB de Villiers        AF Milne         31        45     6     6 130        3


## 12. Bowler analysis

Just like the batsman performance estimation we can consider the bowler’s performances also for estimation. Consider the following table

As in the batsman analysis, for every batsman a set of features like (“strong backfoot player”, “360 degree player”,“Power hitter”) can be estimated with a set of initial values. Also every bowler will have an associated parameter vector θθ. Different bowlers will have performance data for different set of batsmen. Based on the initial estimate of the features and the parameters, gradient descent can be used to minimize actual values {for e.g. wicketsTaken(ratings)}.

load("recom_data/bowlerVsBatsman20_22.rdata")


## 12a. Bowler dataframe

Inspecting the bowler dataframe

head(df2)

##    bowler1        batsman1 balls runsConceded       ER wicketTaken
## 1 A Mishra        A Badoni     0            0 0.000000           0
## 2 A Mishra       A Manohar     0            0 0.000000           0
## 3 A Mishra        A Nortje     0            0 0.000000           0
## 4 A Mishra  AB de Villiers    63           61 5.809524           0
## 5 A Mishra     Abdul Samad     0            0 0.000000           0
## 6 A Mishra Abhishek Sharma     2            3 9.000000           0

names(df2)

## [1] "bowler1"      "batsman1"     "balls"        "runsConceded" "ER"
## [6] "wicketTaken"


## 13. Balls bowled by bowler

The below section estimates the balls bowled for each bowler. We can see that UBCF Pearson and UBCF Cosine both perform well

df3 <- select(df2, bowler1,batsman1,balls)
df6 <- xtabs(balls ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Badoni', 'A Manohar', 'A Nortje' ... ]]

##
## A Mishra        . . .  63  .  2 35 .  6 .
## A Nortje        . . .  21 25  .  . .  6 .
## A Zampa         . . .   9  .  .  . .  . .
## Abhishek Sharma . . .   9  .  .  . .  6 .
## AD Russell      . . . 117 12  9  . . 21 9
## AF Milne        . . .   .  .  .  . .  8 4
## AJ Tye          . . .  63  .  . 66 .  . .
## Akash Deep      . . .   .  .  .  . .  . .
## AR Patel        . . . 188  5  1 84 . 29 5
## Arshdeep Singh  . . .   6  6 24 18 . 12 .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    1.00    9.00   18.00   29.61   36.00  384.00

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

a=eval(r0[1:dim(r0)[1]],0.8,k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 96 users.
## 24 x 195 rating matrix of class 'realRatingMatrix' with 3954 ratings.
##      RMSE       MSE       MAE
##  30.72284 943.89294  19.89204

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
s=as(c,"data.frame")
names(s) =c("bowler","batsman","BallsBowled")


## 14. Runs conceded by bowler

This section estimates the runs conceded by the bowler. The UBCF Cosinus algorithm performs the best with TPR increasing fastewr than FPR

df3 <- select(df2, bowler1,batsman1,runsConceded)
df6 <- xtabs(runsConceded ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Badoni', 'A Manohar', 'A Nortje' ... ]]

##
## A Mishra        . . .  61  .  3  41 . 15  .
## A Nortje        . . .  36 57  .   . .  8  .
## A Zampa         . . .   3  .  .   . .  .  .
## Abhishek Sharma . . .   6  .  .   . .  3  .
## AD Russell      . . . 276 12  6   . . 21  .
## AF Milne        . . .   .  .  .   . . 10  4
## AJ Tye          . . .  69  .  . 138 .  .  .
## Akash Deep      . . .   .  .  .   . .  .  .
## AR Patel        . . . 205  5  . 165 . 33 13
## Arshdeep Singh  . . .  18  3 51  51 .  6  .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##    1.00    9.00   24.00   41.34   54.00  458.00

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

## Timing stopped at: 0.004 0 0.004

## Warning in .local(x, method, ...):
##   Recommender 'UBCF Pearson' has failed and has been removed from the results!

a=eval(r0[1:dim(r0)[1]],0.8,k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 95 users.
## 24 x 195 rating matrix of class 'realRatingMatrix' with 3820 ratings.
##       RMSE        MSE        MAE
##   43.16674 1863.36749   30.32709

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
t=as(c,"data.frame")
names(t) =c("bowler","batsman","RunsConceded")


## 15. Economy Rate of the bowler

This section computes the economy rate of the bowler. The performance is not all that good

df3 <- select(df2, bowler1,batsman1,ER)
df6 <- xtabs(ER ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Badoni', 'A Manohar', 'A Nortje' ... ]]

##
## A Mishra        . . .  5.809524  .     9.00  7.028571 . 15.000000  .
## A Nortje        . . . 10.285714 13.68  .     .        .  8.000000  .
## A Zampa         . . .  2.000000  .     .     .        .  .         .
## Abhishek Sharma . . .  4.000000  .     .     .        .  3.000000  .
## AD Russell      . . . 14.153846  6.00  4.00  .        .  6.000000  .
## AF Milne        . . .  .         .     .     .        .  7.500000  6.0
## AJ Tye          . . .  6.571429  .     .    12.545455 .  .         .
## Akash Deep      . . .  .         .     .     .        .  .         .
## AR Patel        . . .  6.542553  6.00  .    11.785714 .  6.827586 15.6
## Arshdeep Singh  . . . 18.000000  3.00 12.75 17.000000 .  3.000000  .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##  0.3529  5.2500  7.1126  7.8139  9.8000 36.0000

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

## Timing stopped at: 0.003 0 0.004

## Warning in .local(x, method, ...):
##   Recommender 'UBCF Pearson' has failed and has been removed from the results!

a=eval(r0[1:dim(r0)[1]],0.8,k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 95 users.
## 24 x 195 rating matrix of class 'realRatingMatrix' with 3839 ratings.
##      RMSE       MSE       MAE
##  4.380680 19.190356  3.316556

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
u=as(c,"data.frame")
names(u) =c("bowler","batsman","EconomyRate")


## 16. Wickets Taken by bowler

The code below computes the wickets taken by the bowler versus different batsmen

df3 <- select(df2, bowler1,batsman1,wicketTaken)
df6 <- xtabs(wicketTaken ~ ., df3)
df7 <- as.data.frame.matrix(df6)
df8 <- data.matrix(df7)
df8[df8 == 0] <- NA
r <- as(df8,"realRatingMatrix")
r0=r[(rowCounts(r) > 10),]
getRatingMatrix(r0)[1:10,1:10]

## 10 x 10 sparse Matrix of class "dgCMatrix"

##    [[ suppressing 10 column names 'A Badoni', 'A Manohar', 'A Nortje' ... ]]

##
## A Mishra       . . . . . . 1 . . .
## A Nortje       . . . 4 . . . . . .
## A Zampa        . . . 3 . . . . . .
## AD Russell     . . . 3 . . . . . .
## AJ Tye         . . . 3 . . 6 . . .
## AR Patel       . . . 4 . 1 3 . 1 1
## Arshdeep Singh . . . 3 . . 3 . . .
## AS Rajpoot     . . . . . . 3 . . .
## Avesh Khan     . . . . . . 1 . 3 .
## B Kumar        . . . 9 . . 3 . 1 .

summary(getRatings(r0))

##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
##   1.000   3.000   3.000   3.423   3.000  21.000

evalRecomMethods(r0[1:dim(r0)[1]],k1=5,given=7,goodRating1=median(getRatings(r0)))

## Timing stopped at: 0.003 0 0.003

## Warning in .local(x, method, ...):
##   Recommender 'UBCF Pearson' has failed and has been removed from the results!

a=eval(r0[1:dim(r0)[1]],0.8,k1=5,given1=7,goodRating1=median(getRatings(r0)),"UBCF")

## Recommender of type 'UBCF' for 'realRatingMatrix'
## learned using 64 users.
## 16 x 195 rating matrix of class 'realRatingMatrix' with 1908 ratings.
##     RMSE      MSE      MAE
## 2.672677 7.143203 1.956934

b=round(as(a,"matrix")[1:10,1:10])
c <- as(b,"realRatingMatrix")
v=as(c,"data.frame")
names(v) =c("bowler","batsman","WicketTaken")


## 17. Generate the Bowler Performance estmiate

The entire dataframe is regenerated with known and ‘predicted’ values

r1=merge(s,t,by=c("bowler","batsman"))
r2=merge(r1,u,by=c("bowler","batsman"))
r3=merge(r2,v,by=c("bowler","batsman"))
r4= select(r3,bowler, batsman, BallsBowled,RunsConceded,EconomyRate, WicketTaken)

##     bowler         batsman BallsBowled RunsConceded EconomyRate WicketTaken
## 1 A Mishra  AB de Villiers         102          144           8           4
## 2 A Mishra     Abdul Samad          13           20           7           4
## 3 A Mishra Abhishek Sharma          14           26           8           2
## 4 A Mishra      AD Russell          47           85           9           3
## 5 A Mishra        AJ Finch          45           61          11           4
## 6 A Mishra          AJ Tye          14           20           5           4


## 18. Conclusion

This post showed an approach for performing the Batsmen Performance Estimate & Bowler Performance Estimate. The performance of the recommender engine could have been better. In any case, I think this approach will work for player estimation provided the recommender algorithm is able to achieve a high degree of accuracy. This will be a good way to estimate as the algorithm will be able to determine features and nuances of batsmen and bowlers which cannot be captured by data.

## Also see

To see all posts click Index of posts

# IPL 2022: Near real-time analytics with GooglyPlusPlus!!!

It is that time of the year when there is “a song in the air, the lark’s on the wing, and the snail’s on the the thorn“. Yes, it is the that time of year when the grand gala event of IPL 2022 is underway. So, I managed to wake myself from my Covid-induced slumber, worked up my ‘creaking bones‘ and cranked up the GooglyPlusPlus machinery.

So now, every morning, a scheduled CRON tab entry will automatically download the previous night’s match data from Cricsheet, unzip, process and transform it into the necessary format required by my R package yorkr, and make it available to my Shiny app GooglyPlusPlus. Hence the data is current and you have access to ‘analytics-in-the-now’!.

As you know in 2021, I added a lot of new features to GooglyPlusPlus, new tabs to do even more. analytics – or in other words there is “more GooglyPlusPlus per click!!”. So now, you have the following

• Batsman tab: For detailed analysis of batsmen
• Bowler tab: For detailed analysis of bowlers
• Match tab: Analysis of individual matches, plot of Runs vs SR, Wickets vs ER in power play, middle and death overs
• Head-to-head tab: Detailed analysis of team-vs-team batting/bowling scorecard, batting, bowling performances, performances in power play, middle and death overs
• Team performance tab: Analysis of team-vs-all other teams with batting /bowling scorecard, batting, bowling performances, performances in power play, middle and death overs
• Optimisation tab: Allows one to pit batsmen vs bowlers and vice-versa. This tab also uses integer programming to optimise batting and bowling lineup
• Batting analysis tab: Ranks batsmen using Runs or SR. Also plots performances of batsmen in power play, middle and death overs and plots them in a 4×4 grid
• Bowling analysis tab: Ranks bowlers based on Wickets or ER. Also plots performances of bowlers in power play, middle and death overs and plots them in a 4×4 grid

Also note all these tabs and features are available for all T20 formats namely IPL, Intl. T20 (men, women), BBL, NTB, PSL, CPL, SSM.

Note: All charts are interactive, which means that you can hover, zoom-in, zoom-out, pan etc on the charts

The latest avatar of GooglyPlusPlus2022 is based on my R package yorkr with data from Cricsheet.

Go ahead, give GooglyPlusPlus a try!!!

To know all the new features and how to use them, check out these posts

1. Ranking of batsmen, bowlers – GooglyPlusPlus2021 interactively ranks T20 batsmen and bowlers!!!
2. Interactive charts – GooglyPlusPlus2021 is now fully interactive!!!
3. Detailed batsmen/bowler analytics – GooglyPlusPlus2021 enhanced with drill-down batsman, bowler analytics
4. Addition of Date Range picker to charts – GooglyPlusPlus2021 adds new bells and whistles!!
5. Analysis of power play, middle and death overs across players, teams – GooglyPlusPlus2021 now with power play, middle and death over analysis
6. Analysis based on 4 x 4 grid of players – GooglyPlusPlus2021: Towards more picturesque analytics!
7. Optimisation of batsmen/bowlers – GooglyPlusPlus2022 optimizes batting/bowling lineup

Here are some random analysis that can be done by GooglyPlusPlus across the tabs. Note the app will be updated daily and the analytics will be current throughout the season of IPL 2022

A) Match tab

a) GT vs DC – 2 Apr 2022

Runs vs SR – Gujarat Titans

b) CSK vs LSG – 31 Mar 2022

Runs across 20 overs

c) KKR vs PBKS -Match wicket worm chart – 1 Apr 2022

B) Batsmen tab

a) Faf Du Plessis – Runs vs Deliveries

b) Sanju Samson – Runs against opposition

C) Bowler’s tab

a) D J Bravo – No of deliveries to wicket

b) Trent Boult – Wickets at Venues

a) DC vs MI – Mar -2019 till date : Batting scorecard

b) CSK vs KKR – Jan 2019 till date : Runs vs SR

E) Team vs All Teams tab

a) Punjab Kings vs all Teams – Wickets vs ER in Power play

b) Rajasthan Royals vs all Teams : Jan 2019 till date : Runs vs SR in Power play

F) Optimisation tab

a) Batsmen vs Bowlers

b) Bowlers vs batsmen

G) Batting analysis

This tab is for ranking batsmen

a) Batsmen rank from 2019 till date (Runs over SR)

b) Overall Runs vs SR (Jan 2020 till date)

Best batsmen in top right quadrant

zooming in on the above (right-most)

H) Bowling analysis tab

a) Best middle over bowlers in IPL (2019 onwards)

The bottom right quadrant are the best bowlers

b) Best bowlers in death overs (bottom-right)

Check out GooglyPlusPlus!!!

Also see

To see all posts click Index of posts

# GooglyPlusPlus2022 optimizes batting/bowling lineup

GooglyPlusPlus2022 is the new avatar of last year’s GooglyPlusPlus2021. Roughly, about 5 years back I had written a post on Using linear programming to optimize T20 batting and bowling line up. This post has been on the back of my mind for a long time and I decided to pay this post a revisit. This requires computing performance of individual batsmen vs bowlers and vice-versa for performing the optimization. So in this latest incarnation, there are 4 new functions

1. batsmanVsBowlerPerf – Performance of batsmen against chosen bowlers
2. bowlerVsBatsmanPerf – Performance of bowlers versus specific batsmen
3. battingOptimization – Optimizing batting line up based on strike rates ad remaining overs
4. bowlingOptimization – Optimizing bowling line up based on economy rates and remaining overs

These 4 functions have been incorporated in all the supported 9 T20 formats namely a. IPL b. Intl. T20(men) c. Intl. T20 (women) d. BBL e. NTB f. PSL g. WBB h. CPL i. SSM

Check out GooglyPlusPlus2022!!

You can clone/fork the code for GooglyPlusPlus2022 from Github from gpp2022-1

With this latest update you can do a myriad of analyses of batsmen, bowlers, teams, matches. This is just-in-time for the IPL Mega-auction!! Do check out these other posts of GooglyPlusPlus for other detailed analysis

A) Batsman Vs Bowlers – This option computes the performance of individual batsman against individual bowlers

a) IPL Batsmen vs Bowlers

Included below are the performances of Dhoni, Raina and Kohli against Malinga, Ashwin and Bumrah. Note: The last 2 text box input are not required for this.

b) Intl. T20 (men) Batsmen vs Bowlers

Note: You can type the name and choose from the drop down list

B) Bowler vs Batsmen – You can check the performance of specific bowlers against specific batsmen

a) Intl. T20 (women) India vs Australia

b) PSL Bowlers vs Batsmen

C) Strategy for optimizing batting and bowling line up

From the above 2 tabs, it is obvious, that different bowlers have different ER and wicket rate against different batsmen. In other words, the effectiveness of the bowlers varies by batsmen. Conversely, batsmen are more comfortable with certain bowlers versus others and this shows up in different strike rates.

Hence during the death overs, when trying to restrict batsmen to a certain score or on the flip side when the batting side needs to score a target within certain overs, we need to take advantage of the relative effectiveness of bowlers vs batsmen for optimising bowling and aggressiveness of batsmen versus bowlers to quickly reach the target.

This is the approach that is used for bowling and batting optimisation. For optimising bowling, we need to formulate a minimisation problem based on ER rates and for optimising batting, a maximisation strategy is chosen based on SR. ‘Integer programming’ is used to compute during the last set of overs

This latest version includes optimization using “integer programming” based on R package lpSolve.

Here are the 2 formulations

Assume there are 3 bowlers – $bwlr_{1},bwlr_{2},bwlr_{3}$
and there are 3 batsmen – $bman_{1},bman_{2},bman_{3}$

## I) LP Formulation for bowling order

Let the economy rate $er_{ij}$ be the Economy Rate of the jth bowler to the ith batsman. Also if remaining overs for the bowlers are $o_{1},o_{2},o_{3}$
and the total number of overs left to be bowled are
$o_{1}+o_{2}+o_{3} = N$

Let the economy rate $er_{ij}$ be the Economy Rate of the jth bowler to the ith batsman.
Objective function : Minimize –
$er_{11}*o_{11} + er_{12}*o_{12} +..+er_{1n}*o_{1n}+ er_{21}*o_{21} + er_{22}*o_{22}+.. + er_{22}*o_{2n}+ er_{m1}*o_{m1}+..+ er_{mn}*o_{mn}$
i.e.
$\sum_{i=1}^{i=m}\sum_{j=1}^{i=n}er_{ij}*o_{ij}$
Constraints
Where $o_{j}$ is the number of overs remaining for the jth bowler against  ‘k’ batsmen
$o_{j1} + o_{j2} + .. o_{jk} < o_{j}$
and if the total number of overs remaining to be bowled is N then
$o_{1} + o_{2} +...+ o_{k} = N$ or
$\sum_{j=1}^{j=k} o_{j} =N$
The overs that any bowler can bowl is $o_{j} >=0$

## II) LP Formulation for batting lineup

Let the strike rate $sr_{ij}$  be the Strike Rate of the ith batsman to the jth bowler
Objective function : Maximize –
$sr_{11}*o_{11} + sr_{12}*o_{12} +..+ sr_{1n}*o_{1n}+ sr_{21}*o_{21} + sr_{22}*o_{22}+.. sr_{2n}*o_{2n}+ sr_{m1}*o_{m1}+..+ sr_{mn}*o_{mn}$
i.e.
$\sum_{i=1}^{i=4}\sum_{j=1}^{i=3}sr_{ij}*o_{ij}$
Constraints
Where $o_{j}$ is the number of overs remaining for the jth bowler against  ‘k’ batsmen
$o_{j1} + o_{j2} + .. o_{jk} < o_{j}$
and the total number of overs remaining to be bowled is N then
$o_{1} + o_{2} +...+ o_{k} = N$ or
$\sum_{j=1}^{j=k} o_{j} =N$
The overs that any bowler can bowl is
$o_{j} >=0$

C) Optimized bowling lineup

a) IPL – Optimizing bowling line up

Note: For computing the Optimal bowling lineup, the total number of overs remaining and the number of overs for each bowler have to be entered.

b) PSL – Optimizing batting line up

d) Optimized batting lineup

a) Intl. T20 (men) India vs England

b) Carribean Premier LeagueOptimizing batting line up

Give GooglyPlusPlus2022 a spin!

You can also check the code here gpp2022-1

Hope you have a good time with GooglyPlusPlus2022!

Also see

To see all posts click Index of posts

# GooglyPlusPlus2021: Towards more picturesque analytics!

Analytics for e.g. sports analytics, business analytics or analytics in e-commerce or in other domain has 2 main requirements namely a) What kind of analytics (set of parameters,function) will squeeze out the most intelligence from the data b) How to represent the analytics so that an expert can garner maximum insight?

While it may appear that the former is more important, the latter is also equally, if not, more vital to the problem. Indeed, a picture is worth a thousand words, and often times is more insightful than a large table of numbers. However, in the case of sports analytics, for e.g. in cricket a batting or bowling scorecard captures more information and can never be represented in chart.

So, my Shiny app GooglyPlusPlus includes both charts and tables for different aspects of the analysis. In this post, a newer type of chart, popular among senior management experts, namely the 4 quadrant graph is introduced, which helps in categorising batsmen and bowlers into 4 categories as shown below

a) Batting Performances – Top right quadrant (High runs, High Strike rate)

b) Bowling Performances – Bottom right quadrant( High wickets, Low Economy Rate)

A. Match Tab

All the functions below are at match level

1. Team Runs vs SR Plot
2. Team Wickets vs ER Plot
3. Team Runs vs SR Power play plot
4. Team Runs vs SR Middle overs plot
5. Team Runs vs SR Death overs plot
6. Team Wickets vs ER Power Play
7. Team Wickets vs ER Middle overs
8. Team Wickets vs ER Death overs

The below functions are based on all matches between 2 teams’

1. Team Runs vs SR Plot all Matches
2. Team Wickets vs ER Plot all Matches
3. Team Runs vs SR Power play plot all Matches
4. Team Runs vs SR Middle overs plot all Matches
5. Team Runs vs SR Death overs plot all Matches
6. Team Wickets vs ER Power Play plot all Matches
7. Team Wickets vs ER Middle overs plot all Matches
8. Team Wickets vs ER Death overs plot all Matches

C. Team Performance tab

The below functions are based on a team’s performance against all other teams

1. Team Runs vs SR Plot overall
2. Team Wickets vs ER Plot overall
3. Team Runs vs SR Power play plot overall
4. Team Runs vs SR Middle overs plot overall
5. Team Runs vs SR Death overs plot overall
6. Team Wickets vs ER Power Play overall
7. Team Wickets vs ER Middle overs overall
8. Team Wickets vs ER Death overs overall

D. T20 format Batting Analysis

This analysis is at T20 format level (IPL, Intl. T20(men), Intl. T20 (women), PSL, CPL etc.)

1. Overall Runs vs SR plot
2. Overall Runs vs SR Power play plot
3. Overall Runs vs SR Middle overs plot
4. Overall Runs vs SR Death overs plot

E. T20 Bowling Analysis

This analysis is at T20 format level (IPL, Intl. T20(men), Intl. T20 (women), PSL, CPL etc.)

1. Overall Wickets vs ER plot
2. Team Wickets vs ER Power Play
3. Team Wickets vs ER Middle overs
4. Team Wickets vs ER Death overs

These 32 functions have been added to my yorkr package and so all these functions become plug-n-play in my Shiny app GooglyPlusPlus2021 which means that the 32 functions apply across all the nine T20 formats that the app supports i.e. IPL, Intl. T20 (men), Intl. T20 (women), BBL, NTB, PSL, CPL, SSM, WBB.

Hence the multiplicative factor of the new addition is 32 x 9 = 288 additional ways of exploring match, team and player data

The data for GooglyPlusPlus is taken from Cricsheet. My shiny app GooglyPlusPlus2021 is based on my R package yorkr.

You can clone/fork GooglyPlusPlus from Github at gpp2021-10

Check out my app GooglyPlusPlus2021 and analyze batsmen, bowlers, teams, overall performance. The data for all the nine T20 formats have been updated to include the latest data.

Hence, the app is just in time for the IPL mega auction. You should be able to analyse players in IPL, Intl. T20 or in any of the other formats from where they could be drawn and check out their relative standings

I am including some random plots to demonstrate the newly minted functions

Note 1: All plots are interactive. The controls are on the top right. You can hover over data, zoom-in, zoom-out, compare data etc by choosing the appropriate control. To know more about how to use the interactive charts see GooglyPlusPlus2021 is now fully interactive!!!

You can also check my short video on how to navigate interactive charts

Note 2: To know about Powerplay, Middle overs and Death over analysis see my post GooglyPlusPlus2021 now with power play, middle and death over analysis

Note 3: All tabs(except Match tab) now include Date range pickers to focus on the period of interest. See my post GooglyPlusPlus2021 enhanced with drill-down batsman, bowler analytics

I) Match tab

New Zealand vs Australia (2021-11-14)

New Zealand batting, except K Williamson, the rest did not fire as much

For Australia, Warner, Maxwell and Marsh played good knocks to wrest control

a) Wickets vs ER during Power play of Mumbai Indians in all matches against Chennai Super Kings (IPL)

b) Karachi Kings Runs vs SR during middle overs against Multan Sultans (PSL)

c) Wickets vs ER during death overs of Barbados Tridents in all matches against Jamaica Tallawahs (CPL)

III) Teams overall batting performance

India’s best T20 performers in Power play since 2018 (Intl. T20)

e) Australia’s best performers in Death overs since Mar 2017 (Intl. T20)

f) India’s Intl. T20 (women) best Runs vs SR since 2018

g) England’s Intl. T20 (women) best bowlers in Death overs

IV) Overall Batting Performance across T20

This tab gives the batsmen’s rank and overall batting performance across the T20 format.

a) Why was Hardik Pandya chosen, and why this was in error?

Of course, it provides an insight into why Hardik Pandya was chosen in India’s World cup team despite poor performances recently. Here are the best Intl. T20 death over batsmen

Of course, we can zoom in to get a better look

This is further substantiated when we performances in IPL

However, if you move the needle forward a year at a time, you see Hardik Pandya’s performance drops significantly

and further down

Rather, Dinesh Karthik, Sanju Samson or Ruturaj Gaikwad would have been better options

b) Best batsmen Intl. T20 (women) in Power play since 2018

V) Overall bowling performance

This tab gives the bowler’s rank and overall bowling performance in Power play, middle and death overs across all T20 formats

a) Intl. T20 (men) best bowlers in Power Play from 2019 (zoomed in)

b) Intl. T20(men) best bowlers in Death overs since 2019

c) Was B. Kumar a good choice for India team in World cup?

Bhuvi was one of India’s best bowler in Power play only if we go back to the beginning of time

i) From 2008

But if we move forward to 2020 onwards we see Arshdeep Singh or D Chahar would have been a better choice

ii) From 2020 onwards

iii) 2021 onwards

Hence D Chahar & Arshdeep Singh are the natural choice moving forwards for India

iv) T20 Best batsman

If we look at Intl. T20 performances since 2017, Babar Azam leads the pack, however his Strike rate needs to move up.

v) T20 best bowlers

As mentioned above go ahead and give GooglyPlusPlus2021 a spin!!!

You can download/fork the code for the Shiny app from Github at gpp2021-10

Also see

To see all posts click Index of posts

# GooglyPlusPlus2021:ICC WC T20:Pavilion-view analytics as-it-happens!

This year 2021, we are witnessing a rare spectacle in the cricketing universe, where IPL playoffs are immediately followed by ICC World Cup T20. Cricket pundits have claimed such a phenomenon occurs once in 127 years! Jokes apart, the World cup T20 is underway and as usual GooglyPlusPlus is ready for the action.

GooglyPlusPlus will provide near-real time analytics, by automatically downloading the latest match data daily, processing and organising the match data into appropriate folders so that my R package yorkr can slice and dice the data to provide the pavilion-view analytics.

The charts capture all the breathless, heart-pounding, and nail-biting action in great details in the many tables and plots. Every table and chart tell a story. You just have to ‘read between the lines!’

GooglyPlusPlus2021 will update itself automatically every day, so the data will be current and you can analyse all matches upto the previous day, along with the historical performances of the teams. So make sure you check it everyday.

Note:

1. All charts are interactive. To know how to use the interactive charts see my post GooglyPlusPlus2021 is now fully interactive!!!
2. The are 5 tabs for each of the formats supported by GooglyPlusPlus2021 which now supports IPL, Intl. T20(men), Intl. T20(women), BBL, NTB, PSL, CPL, SSM, WBB. Besides, it also supports ODI (men) and ODI (women)
3. Each of the formats have 5 tabs – Batsman, Bowler, Match, Head-to-head and Overall Performace.
4. All T20 formats also include a ranking functionality for the batsmen and bowlers
5. You can now perform drill-down analytics for batsmen, bowlers, head-to-head and overall performance based on date-range selector functionality. The ranking tabs also include date range selector granular analysis. For more details see GooglyPlusPlus2021 enhanced with drill-down batsman, bowler analytics

Try out GooglyPlusPlus2021 here gpp2021-8!!

You can clone fork the code from Github gpp2021-8

I am including some random screenshots of things that can be done with GooglyPlusPlus2021

A. Papua New Guinea vs Oman (2021-10-17)

a. Batting partnership

B. Match worm chart (New Papua Guinea v Oman)

This was a no contest as Oman cruised to victory

b. March worm chart (Scotland vs Bangladesh)

Fortunes see-sawed one way, then another, as can be seen in the match worm chart

C. Netherlands vs Ireland (2021-10-18)

a. Batman vs Bowler

a. Sri Lanka vs West Indies (2019-2021) – Batting partnerships

b. India vs England (2018 – 2021) – Bowling scorecard

c) Australia vs South Africa – Team wicket opposition

E) Overall performance

a. Pakistan batting scorecard since 2019

a. Win loss of Australia since 2019

F) Batsman Performance

a. PR Stirling’s runs against opposition since 2019

b. KJ Brien’s cumulative average runs since 2019

G. Bowler performance

a. PWH De Silva’s wicket prediction since 2019

b. T Shamsi’s cumulative average wickets since 2019

H. Ranking Intl. T20 batsman since 2019

a. Runs over Strike rate

b. Strike rate over runs

I. Ranking bowlers since 2019

a. Wickets over Economy rate

b. Economy rate over wickets

As mentioned above GooglyPlusPlus2021 will be updated daily automatically, so you won’t miss any analytic action.

Do give GooglyPlusPlus2021 a spin!

Clone/fork the code for the Shiny app from Github gpp2021-8

You may also like

To see all post click Index of posts

# GooglyPlusPlus2021 enhanced with drill-down batsman, bowler analytics

This latest update to GooglyPlusPlus2021 includes the following changes

a) All the functions in the ‘Batsman’ and ‘Bowler ‘tabs now include a date range, which allows you specify a period of interest.

b) The ‘Rank Batsman’ and ‘Rank Bowler’ tabs also include a date range selector, against the earlier version which had a ‘Since year’ slider see GooglyPlusPlus2021 bubbles up top T20 players in all formats!. The earlier ‘Since year’ slider option could only rank for the latest year or for all years up to the current year. Now with the new ‘date range’ picker we can check the batsman and bowler ranks in any IPL season or (any T20 format) or for a range of years.

There are 10 batsman functions and 9 bowler function that have changed for the following T20 and ODI formats and Rank batsman and bowler includes the ‘date range’ and has changed for all T20 formats.

GooglyPlusPlus2021 supports all the following T20 formats

i) IPL ii) Intl T20(men) iii) Intl T20(women) iv) BBL v) NTB vi) PSL vii) WBB viii) CPL ix) SSM T20 formats – ( 9 T20 panels)

i) ODI (men) ii) ODI (women) – 2 ODI panels

i.e. the changes impact (10 + 9) x 11 + (1 + 1 ) x 9 = 227 tabs which have been changed

The addition of date range enables a fine-grained analysis of players as the players progress through the years.

Note: All charts are interactive. To see how to use interactive charts of GooglyPlusPlus2021 see

GooglyPlusPlus2021 is now fully interactive!!!

GooglyPlusPlus2021 is based on my R package yorkr. The data is take from Cricsheet

You can clone/fork this latest version of GooglyPlusPlus2021 from Github at gpp2021-7

Check out the Shiny app here GooglyPlusPlus2021!!!

I have included some random screen shots of some of using these tabs and options in GooglyPlusPlus2021.

A) KL Rahul’s Cumulative average in IPL 2021 vs IPL 2020

a) KL Rahul in IPL 2021

b) KL Rahul in IPL 2020

B) Performance of Babar Azam in Intl. T20 (men)

a) Babar Azam’s cumulative average from 2019

b) Babar Azam’s Runs against opposition since 2019

Note: Intl. T20 (women) data available upto Mar 2020 from Cricsheet

a) A J Healy performance between 2010 – 2015

b) A J Healy performance between 2015 – 2020

D) M S Dhoni’s performance with the bat pre-2020 and post 2020

There has been a significant decline in Dhoni’s performance in the last couple of years

I) Dhoni’s performance from Jan 2010 to Dec 2019

a) Moving average at 25+ (Dhoni before)

The moving average actually moves up…

b) Cumulative average at 25+ (Dhoni before)

c) Cumulative Strike rate 140+ (Dhoni before)

d) Dhoni’s moving average is ~10-12 (post 2020)

e) Dhoni’s cumulative average (post 2020)

f) Dhoni’s strike rate ~80 (post 2020)

E) Bumrah’s performance in IPL

a) Bumrah’s performance in IPL 2020

b) Bumrah’s performance in IPL 2021

F) Moving average wickets for A. Shrubsole in ODI (women)

G) Chris Jordan’s cumulative economy rate

We can see that Jordan has become more expensive over the years

G) Ranking players

In this latest version the ‘Since year slider’ has been replaced with a Date Range selector. With this we can identify the player ranks in any IPL, CPL, PSL or BBL season. We can also check the performance over the last couple of years. Note: The matches played and Runs over Strike rate or Strike rate over runs can be computed. Similarly for bowlers we have Wickets over Economy rate and Economy rate over wickets options.

a) Ranking IPL batsman in IPL season 2020

b) Ranking Intl. T20 (batsmen) from Jan 2019 to Jul 2021

c) Ranking Intl. T20 bowlers (women) from Jan 2019 – Jul 2021

d) Best IPL bowlers over the last 3 seasons (Wickets over Economy rate)

e) Best IPL bowlers over the last 3 seasons (Economy rate over wickets)

Take GooglyPlusPlus2021 for a spin!!

Hope you have fun checking out the different tabs with the different options!!

Also see

# GooglyPlusPlus2021: Restarting IPL 2021 as-it-happens!!!

The IPL 2021 extravaganza has restarted again, now in Dubai, and it was time for me to crank up good ol’ GooglyPlusPlus2021. As in my earlier post, GooglyPlus2021 with IPL 2021 as it happens, during the initial set of IPL 2021 games,, a command script will execute automatically every day, download the latest data files, unzip, sort, process and put them in appropriate directories so that GooglyPlusPlus can work its magic on the data, with my R package yorkr. You can do analysis of IPL 2021 matches, batsmen, bowlers, historical performance analysis of head-to-head clashes and performances of teams.

Note: Since the earlier instalment of IPL 2021, there are 2 key changes that have taken place in GooglyPlusPlus.

Now,

a) All charts are interactive. You can hover over charts, click, double-click to get more details. To see more details on how to use the interactive charts, see my post GooglyPlusPlus2021 is now fully interactive!

b) You can now analyse historical performances, compute team batting and bowling scorecards for specified periods. To know details see GooglyPlusPlus2021 adds new bells and whistles!

You can try out my app GooglyPlusPlus2021 by clicking GooglyPlusPlus2021

The code for my R package yorkr is available at Github at yorkr

You can clone/fork GooglyPlusPlus2021 from github at gpp2021-6

Some key analysis and highlights of the 2 recently concluded IPL matches

• CSK vs MI
• KKR vs RCB

a) CSK vs MI (19 Sep 2021) – Batting Partnerships (CSK)

b) CSK vs MI (19 Sep 2021) – Bowling scorecard (MI)

c) CSK vs MI (19 Sep 2021) – Match worm chart

Even though MI had a much better start and were cruising along to a victory, they lost the plot around the 18.1 th over as seen below (hover on the chart)

d) KKR vs RCB ( 20 Sep 2021) – Bowling wicket match

This chart gives the wickets taken by the bowler and the total runs conceded

e) KKR vs RCB ( 20 Sep 2021) – Match worm chart

This was a no contest. RCB batting was pathetic and KKR blasted their way to victory as seen in this worm chart

Note: You can also do historical analysis of teams with GooglyPlusPlus2021

For the match to occur today PBKS vs RR (21 Sep 2021) we can perform head-to-head historical analysis. Here Kings XI Punjab has been chosen instead of Punjab Kings as that was its name.

For the Rajasthan Royals Sanjy Samson and Jos Buttler have the best performance from 2018 -2021 as seen below

For Punjab Kings KL Rahul and Chris Gayle are the leading scorers for the period 2018-2021

g) Current ranking of batsmen IPL 2021

h) Current ranking of bowlers IPL 2021

Also you analyse individual batsman and bowlers

i) Batsman analysis

To see Rituraj Gaikwad performance checkout the batsman tab

j) Bowler analysis

Performance of Varun Chakaravarty

Remember to check out GooglyPlusPlus2021 for your daily analysis of matches, teams, batsmen and bowlers. Your ride will be waiting for you!!!

You can clone/fork GooglyPlusPlus2021 from github at gpp2021-6

GooglyPlusPlus2021 has been updated with all completed 31 matches

Also see

To see all posts click Index of posts

# Benford’s law meets IPL, Intl. T20 and ODI cricket

“To grasp how different a million is from a billion, think about it like this: A million seconds is a little under two weeks; a billion seconds is about thirty-two years.”

“One of the pleasures of looking at the world through mathematical eyes is that you can see certain patterns that would otherwise be hidden.”

               Steven Strogatz, Prof at Cornell University

## Introduction

Within the last two weeks, I was introduced to Benford’s Law by 2 of my friends. Initially, I looked it up and Google and was quite intrigued by the law. Subsequently another friends asked me to check the ‘Digits’ episode, from the “Connected” series on Netflix by Latif Nasser, which I strongly recommend you watch.

Benford’s Law also called the Newcomb–Benford law, the law of anomalous numbers, or the First Digit Law states that, when dealing with quantities obtained from Nature, the frequency of appearance of each digit in the first significant place is logarithmic. For example, in sets that obey the law, the number 1 appears as the leading significant digit about 30.1% of the time, the number 2 about 17.6%, number 3 about 12.5% all the way to the number 9 at 4.6%. This interesting logarithmic pattern is observed in most natural datasets from population densities, river lengths, heights of skyscrapers, tax returns etc. What is really curious about this law, is that when we measure the lengths of rivers, the law holds perfectly regardless of the units used to measure. So the length of the rivers would obey the law whether we measure in meters, feet, miles etc. There is something almost mystical about this law.

The law has also been used widely to detect financial fraud, manipulations in tax statements, bots in twitter, fake accounts in social networks, image manipulation etc. In this age of deep fakes, the ability to detect fake images will assume paramount importance. While deviations from Benford Law do not always signify fraud, to large extent they point to an aberration. Prof Nigrini, of Cape Town used this law to identify financial discrepancies in Enron’s financial statement resulting in the infamous scandal. Also the 2009 Iranian election was found to be fradulent as the first digit percentages did not conform to those specified by Benford’s Law.

While it cannot be said with absolute certainty, marked deviations from Benford’s law could possibly indicate that there has been manipulation of natural processes. Possibly Benford’s law could be used to detect large scale match-fixing in cricket tournaments. However, we cannot look at this in isolation and the other statistical and forensic methods may be required to determine if there is fraud. Here is an interesting paper Promises and perils of Benford’s law

A set of numbers is said to satisfy Benford’s law if the leading digit d (d ∈ {1, …, 9}) occurs with probability

$P(d)=log_{10}(1+1/d)$

This law also works for number in other bases, in base b >=2

$P(d)=log_{b}(1+1/d)$

Interestingly, this law also applies to sports on the number of point scored in basketball etc. I was curious to see if this applied to cricket. Previously, using my R package yorkr, I had already converted all T20 data and ODI data from Cricsheet which is available at yorkrData2020, I wanted to check if Benford’s Law worked on the runs scored, or deliveries faced by batsmen at team level or at a tournament level (IPL, Intl. T20 or ODI).

Thankfully, R has a package benford.analysis to check for data behaviour in accordance to Benford’s Law, and I have used this package in my post

This post is also available in RPubs as Benford’s Law meets IPL, Intl. T20 and ODI

library(data.table)
library(reshape2)
library(dplyr)
library(benford.analysis)
library(yorkr)

In this post, I have randomly check data with Benford’s law. The fully converted dataset is available in yorkrData2020 which I have included above. You can try on any dataset including ODI (men,women),Intl T20(men,women),IPL,BBL,PSL,NTB and WBB.

## 1. Check the runs distribution by Royal Challengers Bangalore

We can see the behaviour is as expected with Benford’s law, with minor deviations

load("/Users/tvganesh/backup/software/cricket-package/yorkr-cricsheet/yorkrData2020/ipl/iplBattingBowlingDetails/Royal Challengers Bangalore-BattingDetails.RData")
rcbRunsTrends = benford(battingDetails$runs, number.of.digits = 1, discrete = T, sign = "positive") rcbRunsTrends ## ## Benford object: ## ## Data: battingDetails$runs
## Number of observations used = 1205
## Number of obs. for second order = 99
## First digits analysed = 1
##
## Mantissa:
##
##    Statistic  Value
##         Mean  0.458
##          Var  0.091
##  Ex.Kurtosis -1.213
##     Skewness -0.025
##
##
## The 5 largest deviations:
##
##   digits absolute.diff
## 1      1         14.26
## 2      7         13.88
## 3      9          8.14
## 4      6          5.33
## 5      4          4.78
##
## Stats:
##
##  Pearson's Chi-squared test
##
## data:  battingDetails$runs ## X-squared = 5.2091, df = 8, p-value = 0.735 ## ## ## Mantissa Arc Test ## ## data: battingDetails$runs
## L2 = 0.0022852, df = 2, p-value = 0.06369
##
## Mean Absolute Deviation (MAD): 0.004941381
## MAD Conformity - Nigrini (2012): Close conformity
## Distortion Factor: -18.8725
##
## Remember: Real data will never conform perfectly to Benford's Law. You should not focus on p-values!

## 2. Check the ‘balls played’ distribution by Royal Challengers Bangalore

load("/Users/tvganesh/backup/software/cricket-package/yorkr-cricsheet/yorkrData2020/ipl/iplBattingBowlingDetails/Royal Challengers Bangalore-BattingDetails.RData")
rcbBallsPlayedTrends = benford(battingDetails$ballsPlayed, number.of.digits = 1, discrete = T, sign = "positive") plot(rcbBallsPlayedTrends) ## 3. Check the runs distribution by Chennai Super Kings The trend seems to deviate from the expected behavior to some extent in the number of digits for 5 & 7. load("/Users/tvganesh/backup/software/cricket-package/yorkr-cricsheet/yorkrData2020/ipl/iplBattingBowlingDetails/Chennai Super Kings-BattingDetails.RData") cskRunsTrends = benford(battingDetails$runs, number.of.digits = 1, discrete = T, sign = "positive")
cskRunsTrends
##
## Benford object:
##
## Data: battingDetails$runs ## Number of observations used = 1054 ## Number of obs. for second order = 94 ## First digits analysed = 1 ## ## Mantissa: ## ## Statistic Value ## Mean 0.466 ## Var 0.081 ## Ex.Kurtosis -1.100 ## Skewness -0.054 ## ## ## The 5 largest deviations: ## ## digits absolute.diff ## 1 5 27.54 ## 2 2 18.40 ## 3 1 17.29 ## 4 9 14.23 ## 5 7 14.12 ## ## Stats: ## ## Pearson's Chi-squared test ## ## data: battingDetails$runs
## X-squared = 22.862, df = 8, p-value = 0.003545
##
##
##  Mantissa Arc Test
##
## data:  battingDetails$runs ## L2 = 0.002376, df = 2, p-value = 0.08173 ## ## Mean Absolute Deviation (MAD): 0.01309597 ## MAD Conformity - Nigrini (2012): Marginally acceptable conformity ## Distortion Factor: -17.90664 ## ## Remember: Real data will never conform perfectly to Benford's Law. You should not focus on p-values! ## 4. Check runs distribution in all of Indian Premier League (IPL) battingDF <- NULL teams <-c("Chennai Super Kings","Deccan Chargers","Delhi Daredevils", "Kings XI Punjab", 'Kochi Tuskers Kerala',"Kolkata Knight Riders", "Mumbai Indians", "Pune Warriors","Rajasthan Royals", "Royal Challengers Bangalore","Sunrisers Hyderabad","Gujarat Lions", "Rising Pune Supergiants") setwd("/Users/tvganesh/backup/software/cricket-package/yorkr-cricsheet/yorkrData2020/ipl/iplBattingBowlingDetails") for(team in teams){ battingDetails <- NULL val <- paste(team,"-BattingDetails.RData",sep="") print(val) tryCatch(load(val), error = function(e) { print("No data1") setNext=TRUE } ) details <- battingDetails battingDF <- rbind(battingDF,details) } ## [1] "Chennai Super Kings-BattingDetails.RData" ## [1] "Deccan Chargers-BattingDetails.RData" ## [1] "Delhi Daredevils-BattingDetails.RData" ## [1] "Kings XI Punjab-BattingDetails.RData" ## [1] "Kochi Tuskers Kerala-BattingDetails.RData" ## [1] "Kolkata Knight Riders-BattingDetails.RData" ## [1] "Mumbai Indians-BattingDetails.RData" ## [1] "Pune Warriors-BattingDetails.RData" ## [1] "Rajasthan Royals-BattingDetails.RData" ## [1] "Royal Challengers Bangalore-BattingDetails.RData" ## [1] "Sunrisers Hyderabad-BattingDetails.RData" ## [1] "Gujarat Lions-BattingDetails.RData" ## [1] "Rising Pune Supergiants-BattingDetails.RData" trends = benford(battingDF$runs, number.of.digits = 1, discrete = T, sign = "positive")
trends
##
## Benford object:
##
## Data: battingDF$runs ## Number of observations used = 10129 ## Number of obs. for second order = 123 ## First digits analysed = 1 ## ## Mantissa: ## ## Statistic Value ## Mean 0.4521 ## Var 0.0856 ## Ex.Kurtosis -1.1570 ## Skewness -0.0033 ## ## ## The 5 largest deviations: ## ## digits absolute.diff ## 1 2 159.37 ## 2 9 121.48 ## 3 7 93.40 ## 4 8 83.12 ## 5 1 61.87 ## ## Stats: ## ## Pearson's Chi-squared test ## ## data: battingDF$runs
## X-squared = 78.166, df = 8, p-value = 1.143e-13
##
##
##  Mantissa Arc Test
##
## data:  battingDF$runs ## L2 = 5.8237e-05, df = 2, p-value = 0.5544 ## ## Mean Absolute Deviation (MAD): 0.006627966 ## MAD Conformity - Nigrini (2012): Acceptable conformity ## Distortion Factor: -20.90333 ## ## Remember: Real data will never conform perfectly to Benford's Law. You should not focus on p-values! ## 5. Check Benford’s law in India matches setwd("/Users/tvganesh/backup/software/cricket-package/yorkr-cricsheet/yorkrData2020/t20/t20BattingBowlingDetails") load("India-BattingDetails.RData") indiaTrends = benford(battingDetails$runs, number.of.digits = 1, discrete = T, sign = "positive")
plot(indiaTrends)

## 6. Check Benford’s law in all of Intl. T20

setwd("/Users/tvganesh/backup/software/cricket-package/yorkr-cricsheet/yorkrData2020/t20/t20BattingBowlingDetails")
teams <-c("Australia","India","Pakistan","West Indies", 'Sri Lanka',
"Bermuda","Kenya","Hong Kong","Nepal","Oman","Papua New Guinea",
"United Arab Emirates","Namibia","Cayman Islands","Singapore",
"United States of America","Bhutan","Maldives","Botswana","Nigeria",
"Denmark","Germany","Jersey","Norway","Qatar","Malaysia","Vanuatu",
"Thailand")

for(team in teams){
battingDetails <- NULL
val <- paste(team,"-BattingDetails.RData",sep="")
print(val)
error = function(e) {
print("No data1")
setNext=TRUE
}

)
details <- battingDetails
battingDF <- rbind(battingDF,details)

}
intlT20Trends = benford(battingDF$runs, number.of.digits = 1, discrete = T, sign = "positive") intlT20Trends ## ## Benford object: ## ## Data: battingDF$runs
## Number of observations used = 21833
## Number of obs. for second order = 131
## First digits analysed = 1
##
## Mantissa:
##
##    Statistic  Value
##         Mean  0.447
##          Var  0.085
##  Ex.Kurtosis -1.158
##     Skewness  0.018
##
##
## The 5 largest deviations:
##
##   digits absolute.diff
## 1      2        361.40
## 2      9        276.02
## 3      1        264.61
## 4      7        210.14
## 5      8        198.81
##
## Stats:
##
##  Pearson's Chi-squared test
##
## data:  battingDF$runs ## X-squared = 202.29, df = 8, p-value < 2.2e-16 ## ## ## Mantissa Arc Test ## ## data: battingDF$runs
## L2 = 5.3983e-06, df = 2, p-value = 0.8888
##
## Mean Absolute Deviation (MAD): 0.007821098
## MAD Conformity - Nigrini (2012): Acceptable conformity
## Distortion Factor: -24.11086
##
## Remember: Real data will never conform perfectly to Benford's Law. You should not focus on p-values!

## Conclusion

Maths rules our lives, more than we are aware, more that we like to admit. It is there in all of nature. Whether it is the recursive patterns of Mandelbrot sets, the intrinsic notion of beauty through the golden ratio, the murmuration of swallows, the synchronous blinking of fireflies or in the almost univerality of Benford’s law on natural datasets, mathematics govern us.

Isn’t it strange that while we humans pride ourselves of freewill, the runs scored by batsmen in particular formats conform to Benford’s rule for the first digits. It almost looks like, the runs that will be scored is almost to extent predetermined to fall within specified ranges obeying Benford’s law. So much for choice.

Something to be pondered over!

Also see

# Introducing GooglyPlusPlus!!!

“We can lift ourselves out of ignorance, we can find ourselves as creatures of excellence and intelligence and skill.”
“Heaven is not a place, and it is not a time. Heaven is being perfect.”
“Your whole body, from wingtip to wingtip, is nothing more than your thought itself, in a form you can see. Break the chains of your thought, and you break the chains of your body, too.”

From Jonathan Livingstone Seagull, by Richard Bach

## Introduction

The metamorphosis is complete, from eggs to the butterfly! My R package yorkr, went on to become Googly,  and then to GooglyPlus and  now finally GooglyPlusPlus. My latest R Shiny app now provides interactive visualisation of almost all data in Cricsheet. GooglyPlusPlus visualizes the following matches

1. ODI (men)
2. ODI (women)
3. Intl. T20 (men)
4. Intl T20 (women)
5. IPL (Indian Premier League)
6. BBL (Big Bash League)
7. NTB (Natwest T20)
8. PSL (Pakistan Super League)
9. WBBL – Women’s BBL

GooglyPlusPlus is entirely based on my R package yorkr. To know more about yorkr see ‘Revitalizing R package yorkr‘ and the roughly 25+ posts on yorkr in Index of posts

This Shiny app was quite involved, and it took a lot of work to keep things organised and separate for the different forms of cricket. Anyway it is done and I am happy with the outcome.

Before you use the app, I would suggest that you take a look at the video “How to use GooglyPlusPlus?“. In this video, I show the different features of GooglyPlusPlus and how to navigate through them.

Check out GooglyPlusPlus Shiny at GooglyPlusPlus

You can clone/fork and play around with the code of GooglyPlusPlus here at Github

## A. Highlights of GooglyPlusPlus.

The R Shiny app GooglyPlusPlus has the following main pages for the 9 different cricket formats. See below

Important note: Below I will be including some random output from the GooglyPlusPlus app for different match formats, however there is a lot more features in GooglyPlusPlus

## Conclusion

There you have it. I have randomly shown  2 functions for each cricket format. There are many functions in each tab for the for the different match formats – namely IPL, BBL, Intl T20 (men,women), PSL etc.  Go ahead and give GooglyPlusPlus a spin!

To try out GooglyPlusPlus click GooglyPlusPlus. Don’t forget to check out the video How to use GooglyPlusPlus?

You can clone/fork the code from Github at GooglyPlusPlus

Hope you have fun with GooglyPlusPlus!!

You may also like

To see all posts click Index of posts

# Big Data 7: yorkr waltzes with Apache NiFi

In this post, I construct an end-to-end Apache NiFi pipeline with my R package yorkr. This post is a mirror of my earlier post Big Data-5: kNiFing through cricket data with yorkpy based on my Python package yorkpy. The  Apache NiFi Data Pipeilne  flows all the way from the source, where the data is obtained, all the way  to target analytics output. Apache NiFi was created to automate the flow of data between systems.  NiFi dataflows enable the automated and managed flow of information between systems. This post automates the flow of data from Cricsheet, from where the zip file it is downloaded, unpacked, processed, transformed and finally T20 players are ranked.

This post uses the functions of my R package yorkr to rank IPL players. This is a example flow, of a typical Big Data pipeline where the data is ingested from many diverse source systems, transformed and then finally insights are generated. While I execute this NiFi example with my R package yorkr, in a typical Big Data pipeline where the data is huge, of the order of 100s of GB, we would be using the Hadoop ecosystem with Hive, HDFS Spark and so on. Since the data is taken from Cricsheet, which are few Megabytes, this approach would suffice. However if we hypothetically assume that there are several batches of cricket data that are being uploaded to the source, of different cricket matches happening all over the world, and the historical data exceeds several GBs, then we could use a similar Apache NiFi pattern to process the data and generate insights. If the data is was large and distributed across the Hadoop cluster , then we would need to use SparkR or SparklyR to process the data.

This is shown below pictorially

While this post displays the ranks of IPL batsmen, it is possible to create a cool dashboard using UI/UX technologies like AngularJS/ReactJS.  Take a look at my post Big Data 6: The T20 Dance of Apache NiFi and yorkpy where I create a simple dashboard of multiple analytics

My R package yorkr can handle both men’s and women’s ODI, and all formats of T20 in Cricsheet namely Intl. T20 (men’s, women’s), IPL, BBL, Natwest T20, PSL, Women’s BBL etc. To know more details about yorkr see Revitalizing R package yorkr

The code can be forked from Github at yorkrWithApacheNiFi

You can take a look at the live demo of the NiFi pipeline at yorkr waltzes with Apache NiFi

Basic Flow

## 1. Overall flow

The overall NiFi flow contains 2 Process Groups a) DownloadAnd Unpack. b) Convert and Rank IPL batsmen. While it appears that the Process Groups are disconnected, they are not. The first process group downloads the T20 zip file, unpacks the. zip file and saves the YAML files in a specific folder. The second process group monitors this folder and starts processing as soon the YAML files are available. It processes the YAML converting it into dataframes before storing it as CSV file. The next  processor then does the actual ranking of the batsmen before writing the output into IPLrank.txt

This process group is shown below

#### 1.1.1 GetT20Data

The \${T20data} variable points to the specific T20 format that needs to be downloaded. I have set this to https://cricsheet.org/downloads/ipl.zip. This could be set any other data set. In fact we could have parallel data flows for different T20/ Sports data sets and generate

#### 1.1.2 SaveUnpackedData

This processor stores the YAML files in a predetermined folder, so that the data can be picked up  by the 2nd Process Group for processing

### 1.2 ProcessAndRankT20Players Process Group

This is the second process group which converts the YAML files to pandas dataframes before storing them as. CSV files. The RankIPLPlayers will then read all the CSV files, stack them and then proceed to rank the IPL players. The Process Group is shown below

#### 1.2.1 ListFile and FetchFile Processors

The left 2 Processors ListFile and FetchFile get all the YAML files from the folder and pass it to the next processor

#### 1.2.2 convertYaml2DataFrame Processor

The convertYaml2DataFrame Processor uses the ExecuteStreamCommand which call Rscript. The Rscript invoked the yorkr function convertYaml2DataframeT20() as shown below

I also use a 16 concurrent tasks to convert 16 different flowfiles at once

library(yorkr)
args<-commandArgs(TRUE)
convertYaml2RDataframeT20(args[1], args[2], args[3])


#### 1.2.3 MergeContent Processor

This processor’s only job is to trigger the rankIPLPlayers when all the FlowFiles have merged into 1 file.

#### 1.2.4 RankT20Players

This processor is an ExecuteStreamCommand Processor that executes a Rscript which invokes a yorrkr function rankIPLT20Batsmen()

library(yorkr)
args<-commandArgs(TRUE)

rankIPLBatsmen(args[1],args[2],args[3])


#### 1.2.5 OutputRankofT20Player Processor

This processor writes the generated rank to an output file.

### 1.3 Final Ranking of IPL T20 players

The Nodejs based web server picks up this file and displays on the web page the final ranks (the code is based on a good youtube for reading from file)

[1] "Chennai Super Kings"
[1] "Deccan Chargers"
[1] "Delhi Daredevils"
[1] "Kings XI Punjab"
[1] "Kochi Tuskers Kerala"
[1] "Kolkata Knight Riders"
[1] "Mumbai Indians"
[1] "Pune Warriors"
[1] "Rajasthan Royals"
[1] "Royal Challengers Bangalore"
[1] "Gujarat Lions"
[1] "Rising Pune Supergiants"
[1] "Chennai Super Kings-BattingDetails.RData"
[1] "Deccan Chargers-BattingDetails.RData"
[1] "Delhi Daredevils-BattingDetails.RData"
[1] "Kings XI Punjab-BattingDetails.RData"
[1] "Kochi Tuskers Kerala-BattingDetails.RData"
[1] "Kolkata Knight Riders-BattingDetails.RData"
[1] "Mumbai Indians-BattingDetails.RData"
[1] "Pune Warriors-BattingDetails.RData"
[1] "Rajasthan Royals-BattingDetails.RData"
[1] "Royal Challengers Bangalore-BattingDetails.RData"
[1] "Gujarat Lions-BattingDetails.RData"
[1] "Rising Pune Supergiants-BattingDetails.RData"
# A tibble: 429 x 4
batsman     matches meanRuns meanSR
<chr>         <int>    <dbl>  <dbl>
1 DA Warner       130     37.9   128.
2 LMP Simmons      29     37.2   106.
3 CH Gayle        125     36.2   134.
4 HM Amla          16     36.1   108.
5 ML Hayden        30     35.9   129.
6 SE Marsh         67     35.9   120.
7 RR Pant          39     35.3   135.
8 MEK Hussey       59     33.8   105.
9 KL Rahul         59     33.5   128.
10 MN van Wyk        5     33.4   112.
# â€¦ with 419 more rows

## Conclusion

This post demonstrated an end-to-end pipeline with Apache NiFi and R package yorkr. You can this pipeline and generated different analytics using the various functions of yorkr and display them on a dashboard.

Hope you enjoyed with post!

To see posts click Index of posts