Predicting Clothing Size Ratios when different products have different size methodologies 
*

*For context, when a retailer buys in a clothing item they have to stock it in various sizes. The ratio of stock for each size is referred to as the "size ratio" of the product. E.g. S=0.2, M=0.5, L=0.3

*I am looking to build a model that predicts the size curve ratio of sales at the end of the season based on data available at the start of the season e.g. historic sales data, product attributes.

*The data is structured so that on each line I have a product, product size, and the actual size ratio that sales accounted for in that size.

*The issue is that the data uses different size methodology depending on the product e.g. jeans might be in numerical sizes, but tops might be in S,M,L etc.  So essentially the model is predicting the size ratio for each line.

*I was thinking that I could take each product, look at the range of sizes, count them, then align them in the centre?  For example, product 1 has sizes S,M, L and product 2 has sizes 1,2,3,4,5.  I could align the M and 3.  However, I am unsure of how to get this to work.


Questions: How can I standardise the sizes?
 A: You start out with a size ratio $r\in\mathbb{R}^n$, where the "source" product has $n$ different sizes, and the ratios sum to one, $\sum_{i=1}^nr_n=1$. The goal is to map this to a different dimensionality, $s=f(r)\in\mathbb{R}^m$ so that the target ratio again adds up to one, $\sum_{j=1}^ms_j=1$, and the mapping does not shift around mass "too much". (For instance, if $n=m$, then $f$ should be the identity mapping.)
The simplest mapping would be a linear one, using a matrix $M\in\mathbb{R}^{n\times m}$,
$$ f\colon r\mapsto s := M r.$$
Let $m_{ij}$ denote the entry of $M$ in the $i$-th row and the $j$-th column. The following will work:
$$ m_{ij} := n\times \max\bigg\{0,\min\Big\{\frac{j}{n},\frac{i}{m}\Big\}-\max\Big\{\frac{j-1}{n},\frac{i-1}{m}\Big\}\bigg\}. $$
This works for $n<m$ and $n>m$ and gives the identity matrix for $n=m$. A few examples:


*

*$n=3, m=4$:


\begin{pmatrix}{}
  0.75 & 0.00 & 0.00 \\ 
  0.25 & 0.50 & 0.00 \\ 
  0.00 & 0.50 & 0.25 \\ 
  0.00 & 0.00 & 0.75 \\ 
\end{pmatrix}


*

*$n=4, m=4$:


\begin{pmatrix}{}
  1.00 & 0.00 & 0.00 & 0.00 \\ 
  0.00 & 1.00 & 0.00 & 0.00 \\ 
  0.00 & 0.00 & 1.00 & 0.00 \\ 
  0.00 & 0.00 & 0.00 & 1.00 \\ 
\end{pmatrix}


*

*$n=5, m=4$:


\begin{pmatrix}{}
  1.00 & 0.67 & 0.00 & 0.00 & 0.00 \\ 
  0.00 & 0.33 & 1.00 & 0.33 & 0.00 \\ 
  0.00 & 0.00 & 0.00 & 0.67 & 1.00 \\ 
\end{pmatrix}
As an example, here is how a size ratio of length $n=10$ would be mapped to $m=7$:

Of course, this only works if you have an ordinally scaled attribute, like a single size. It won't work with pants or bras that have two different size attributes, nor with colors that have no natural ordering.
R code:
make_scale_matrix <- function( nsource, ndest ) {
    MM <- matrix(NA, ncol=nsource, nrow=ndest)
    for ( row in 1:ndest ) {
        for ( col in 1:nsource ) {
            MM[row,col] <- nsource *
              max(0, min(col/nsource,row/ndest)-max((col-1)/nsource,(row-1)/ndest))
        }
    }  
    MM
}

prettyprint <- function(MM) {
    # https://tex.stackexchange.com/a/55424/22201
    MM_xtable <- xtable(MM,align=rep("",ncol(MM)+1)) 
    print(MM_xtable,tabular.environment="pmatrix", hline.after=NULL, 
      include.rownames=FALSE, include.colnames=FALSE)
}

prettyprint(make_scale_matrix(3,4))
prettyprint(make_scale_matrix(4,4))
prettyprint(make_scale_matrix(5,3))

set.seed(1)
foo <- runif(10)
source_ratio <- foo/sum(foo)
target_ratio <- (make_scale_matrix(length(source_ratio),7)%*%source_ratio)[,1]
ylim <- c(0,max(c(source_ratio,target_ratio)))

opar <- par(mfrow=c(2,1))
    plot(source_ratio,type="o",main="Source",pch=19,ylim=ylim,xlab="Size",ylab="")
    plot(target_ratio,type="o",main="Target",pch=19,ylim=ylim,xlab="Size",ylab="")
par(opar)

