%%% R code to generate the input data files from corresponding simulation logs
% R> setwd("~/Work/tensorPredictors")
% R>
% R> for (sim.name in c("2a")) {
% 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{amssymb, bm}

\definecolor{exact}{RGB}{230,0,0}
\definecolor{MC}{RGB}{30,180,30}
\definecolor{MCthrd}{RGB}{0,0,230}

\pgfplotsset{
    compat=newest,
    grid=both,
    grid style={gray!15}
}
\tikzset{
    legend entry/.style={
        mark = *,
        mark size = 1pt,
        mark indices = {2},
        line width=0.8pt
    }
}

\begin{document}
\begin{tikzpicture}[>=latex]

    \begin{axis}[
        name=perft,
        xmode = normal, ymode = log,
        xtick={10, 30, 50, 70, 90, 110, 130},
        mark = *,
        mark size = 1pt,
        ytick={1,1e1,1e2,1e3,1e4,1e5,1e6,1e7},
        yticklabels={$1\mu s$,$10\mu s$,$100\mu s$,$1 ms$,$10 ms$,$100 ms$,$1 s$,$10 s$},
        xlabel = {Dimension $p$},
        ylabel = {Time}
    ]
        \addplot[
            only marks,
            color = exact
        ] table[x = dim, y = exact] {aggr-ising-perft-m2.csv};
        \addplot[
            only marks,
            color = MC
        ] table[x = dim, y = MC] {aggr-ising-perft-m2.csv};
        \addplot[
            only marks,
            color = MCthrd
        ] table[x = dim, y = MCthrd] {aggr-ising-perft-m2.csv};

        \addplot[smooth, domain = 8:25,  color = exact,  samples = 64] { 2^(x - 3) };
        \addplot[smooth, domain = 1:130, color = MC,     samples = 64] {
            -18340.8 + 7557.0 * x + 200.3 * x^2
        };
        \addplot[smooth, domain = 1:130, color = MCthrd, samples = 64] {
            8413.21 + 3134.98 * x + 41.87 * x^2
        };
    \end{axis}

    \matrix[anchor = west] at (perft.east) {
        \draw[color=exact,  only marks, mark = *, mark size = 1pt, mark indices = {2}] plot coordinates {(0, 0) (.3, 0) (.6, 0)};
        & \node[anchor=west] {exact}; \\
        \draw[color=exact,  line width = 0.8pt] plot coordinates {(0, 0) (.4, 0)};
        & \node[anchor=west] {$\mathcal{O}(2^p)$}; \\
        \draw[color=MC,  only marks, mark = *, mark size = 1pt, mark indices = {2}] plot coordinates {(0, 0) (.3, 0) (.6, 0)};
        & \node[anchor=west] {MC}; \\
        \draw[color=MC,  line width = 0.8pt] plot coordinates {(0, 0) (.4, 0)};
        & \node[anchor=west] {$\mathcal{O}(p^2)$}; \\
        \draw[color=MCthrd,  only marks, mark = *, mark size = 1pt, mark indices = {2}] plot coordinates {(0, 0) (.3, 0) (.6, 0)};
        & \node[anchor=west] {MC (8 thrd)}; \\
        \draw[color=MCthrd,  line width = 0.8pt] plot coordinates {(0, 0) (.4, 0)};
        & \node[anchor=west] {$\mathcal{O}(p^2)$}; \\
    };

    \node[anchor = south] at (current bounding box.north) {Ising Second Moment Performance Test};

\end{tikzpicture}
\end{document}
