%%% R code to generate the input data files from corresponding simulation logs
% R> setwd("~/Work/tensorPredictors")
% R>
% R> for (sim.name in c("2a", "2b", "2c", "2d")) {
% R>     pattern <- paste0("sim\\_", sim.name, "\\_ising\\-[0-9T]*\\.csv")
% R>     log.file <- sort(
% R>         list.files(path = "sim/", pattern = pattern, full.names = TRUE),
% R>         decreasing = TRUE
% R>     )[[1]]
% R>
% R>     sim <- read.csv(log.file)
% R>
% R>     aggr <- aggregate(sim[, names(sim) != "sample.size"], list(sample.size = sim$sample.size), mean)
% R>
% R>     write.table(aggr, file = paste0("LaTeX/plots/aggr-", sim.name, "-ising.csv"), row.names = FALSE, quote = FALSE)
% R> }
\documentclass[border=0cm]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{bm}

\definecolor{gmlm}{RGB}{0,0,0}
\definecolor{mgcca}{RGB}{86,180,233}
\definecolor{tsir}{RGB}{0,158,115}
\definecolor{pca}{RGB}{240,228,66}
\definecolor{hopca}{RGB}{230,159,0}
\definecolor{lpca}{RGB}{127,127,127}
\definecolor{clpca}{RGB}{191,191,191}

\pgfplotsset{
    every axis/.style={
        xtick={100,200,300,500,750},
        ymin=-0.05, ymax=1.05,
        grid=both,
        grid style={gray, dotted}
    },
    every axis plot/.append style={
        mark = *,
        mark size = 1pt,
        line width=0.8pt
    }
}
\tikzset{
    legend entry/.style={
        mark = *,
        mark size = 1pt,
        mark indices = {2},
        line width=0.8pt
    }
}

\begin{document}
\begin{tikzpicture}[>=latex, scale = 0.58]

    \begin{axis}[
        name=sim-2a,
        xticklabel=\empty
    ]
        \addplot[color = gmlm, line width=1pt]  table[x = sample.size, y = dist.subspace.gmlm]  {aggr-1a-normal.csv};
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-2a-ising.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-2a-ising.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-2a-ising.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-2a-ising.csv};
        \addplot[color = lpca]  table[x = sample.size, y = dist.subspace.lpca]  {aggr-2a-ising.csv};
        \addplot[color = clpca] table[x = sample.size, y = dist.subspace.clpca] {aggr-2a-ising.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-2a.north west) {
        a: linear dependence
    };
    \begin{axis}[
        name=sim-2b,
        anchor=north west, at={(sim-2a.right of north east)}, xshift=0.1cm,
        xticklabel=\empty,
        ylabel near ticks, yticklabel pos=right
    ]
        \addplot[color = gmlm, line width=1pt]  table[x = sample.size, y = dist.subspace.gmlm]  {aggr-1a-normal.csv};
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-2b-ising.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-2b-ising.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-2b-ising.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-2b-ising.csv};
        \addplot[color = lpca]  table[x = sample.size, y = dist.subspace.lpca]  {aggr-2b-ising.csv};
        \addplot[color = clpca] table[x = sample.size, y = dist.subspace.clpca] {aggr-2b-ising.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-2b.north west) {
        b: quadratic dependence
    };
    \begin{axis}[
        name=sim-2c,
        anchor=north west, at={(sim-2a.below south west)}, yshift=-1.3em
    ]
        \addplot[color = gmlm, line width=1pt]  table[x = sample.size, y = dist.subspace.gmlm]  {aggr-1a-normal.csv};
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-2c-ising.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-2c-ising.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-2c-ising.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-2c-ising.csv};
        \addplot[color = lpca]  table[x = sample.size, y = dist.subspace.lpca]  {aggr-2c-ising.csv};
        \addplot[color = clpca] table[x = sample.size, y = dist.subspace.clpca] {aggr-2c-ising.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-2c.north west) {
        c: rank 1 $\boldsymbol{\beta}$'s
    };

    \begin{axis}[
        name=sim-2d,
        anchor=north west, at={(sim-2c.right of north east)}, xshift=0.1cm,
        ylabel near ticks, yticklabel pos=right
    ]
        \addplot[color = gmlm, line width=1pt]  table[x = sample.size, y = dist.subspace.gmlm]  {aggr-1a-normal.csv};
        \addplot[color = pca]   table[x = sample.size, y = dist.subspace.pca]   {aggr-2d-ising.csv};
        \addplot[color = hopca] table[x = sample.size, y = dist.subspace.hopca] {aggr-2d-ising.csv};
        \addplot[color = tsir]  table[x = sample.size, y = dist.subspace.tsir]  {aggr-2d-ising.csv};
        \addplot[color = mgcca] table[x = sample.size, y = dist.subspace.mgcca] {aggr-2d-ising.csv};
        \addplot[color = lpca]  table[x = sample.size, y = dist.subspace.lpca]  {aggr-2d-ising.csv};
        \addplot[color = clpca] table[x = sample.size, y = dist.subspace.clpca] {aggr-2d-ising.csv};
    \end{axis}
    \node[anchor = base west, yshift = 0.3em] at (sim-2d.north west) {
        d: interaction constraints% via $\boldsymbol{\Omega}$'s
    };

    \node[anchor = north] at (current bounding box.south) {Sample Size $n$};

    \matrix[anchor = west] at (current bounding box.east) {
        \draw[color=gmlm,  legend entry, line width=1pt] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {GMLM};  \\
        \draw[color=tsir,  legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {TSIR};  \\
        \draw[color=mgcca, legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {MGCCA}; \\
        \draw[color=hopca, legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {HOPCA}; \\
        \draw[color=pca,   legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {PCA};   \\
        \draw[color=lpca,  legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {LPCA};  \\
        \draw[color=clpca, legend entry] plot coordinates {(0, 0) (.3, 0) (.6, 0)}; & \node[anchor=west, font=\footnotesize] {CLPCA}; \\
    };

    \node[anchor = south, rotate = 90] at (current bounding box.west) {Subspace Distance $d(\boldsymbol{B}, \hat{\boldsymbol{B}})$};
    \node[anchor = south, rotate = 270] at (current bounding box.east) {\phantom{Subspace Distance $d(\boldsymbol{B}, \hat{\boldsymbol{B}})$}};

    \node[anchor = south, font=\large] at (current bounding box.north) {Ising Simulation};

\end{tikzpicture}
\end{document}
