(* Plot MLC++ LearnCurve output (when MATH_DISPLAY is yes).
   By Ronny Kohavi

   Given data in matrix form, graph it
   Every line in the matrix should start with the number of item,
   then the actual data.
   e.g.,

   data={{10, .93,.95,.92, .97, .92},{20,.95,.98,.97,.95}, {30, .99,.99,1,.97,.98,.99,.99,.99}}
   graphTitle="foo"

*)

<< ../.math.init
Needs["Statistics`DescriptiveStatistics`"]
Needs["Graphics`Graphics`"]
MyErrorListPlot[l3:{{_, _, _}..},opts___] :=
        Show[ Graphics[ { PointSize[0.015], Thickness[0.002],
                Module[ {i, x, y, dy} , 
                Table[ 
                        {x, y, dy} = l3[[i]] ;
                        { Line[ {{x, y-dy}, {x, y+dy}} ],
	                  Line[ {{x-1, y-dy}, {x+1, y-dy}} ],
	                  Line[ {{x-1, y+dy}, {x+1, y+dy}} ],
                        Point[ {x, y} ] } ,
                        {i, Length[l3]}
                ] ] } ], opts,Sequence @@ Options[ErrorListPlot] ]


(* Print["Data is ", MatrixForm[data]]
   Print[""]  *)
numrows = Length[data]
dispersionData = {}
sizeArray={}
numDataArray = {}
meanArray = {}
meanPlotData = {}
meanStdPlotData = {}
stdArray = {}
min = 1
For[i=1, i <= numrows, i++, 
   dataRow = data[[i]];
   dataSetSize = dataRow[[1]];
   sizeArray = Append[sizeArray, dataSetSize];
   dataRow = Drop[dataRow,1];
   numPoints = Length[dataRow];
   numDataArray = Append[numDataArray, numPoints];
   meanNum = Mean[dataRow];
   meanArray   = Append[meanArray, meanNum];
   min = Min[{min, meanNum}];
   meanPlotData = Append[meanPlotData,{dataSetSize, Mean[dataRow]}];
   stdErr = StandardErrorOfSampleMean[dataRow];
   (* stdErr = StandardDeviation[dataRow].  for population std-dev *)
   meanStdPlotData = Append[meanStdPlotData,{dataSetSize,
	  Mean[dataRow], 2*stdErr}];
   stdArray   = Append[stdArray, stdErr];
   For[j=1, j <= numPoints, j++,
     dispersionData = Append[dispersionData, {dataSetSize, dataRow[[j]]}]]
]
(* mycol generates padded form, avoiding exponential notation.  *)
mycol[name_, mylist_,t_] := PaddedForm[
     ColumnForm[Prepend[mylist, name],Center],
     {t,3}, ExponentFunction -> (Null & )]
Print[TableForm[{ColumnForm[Prepend[sizeArray, "num instances"], Center],
                 ColumnForm[Prepend[numDataArray, "num runs"], Center],
                 mycol["Mean", meanArray,4], 
                 mycol["Std.Dev of mean",stdArray,4]}, 
                 TableDirections->Row]]
minY = 0.95*min
startY = If [min < 0.1, 0, minY]
scaterPlot = ListPlot[dispersionData, AxesLabel->{"TS size", "Accuracy"},
         Prolog->AbsolutePointSize[4], PlotRange->{startY,1},
         AxesOrigin->{0,startY}, GridLines->{None, Automatic}]

meanPlot = Show[MyErrorListPlot[meanStdPlotData, DisplayFunction->Identity], 
	        ListPlot[meanPlotData, PlotJoined->True, 
	        DisplayFunction->Identity],
	 AxesLabel->{"TS size", "Accuracy"},
         Prolog->AbsolutePointSize[4], PlotRange->{startY,1},
         AxesOrigin->{0,startY}, GridLines->{None, Automatic},
	 PlotLabel->graphTitle,
         DisplayFunction->$DisplayFunction]

PSSave[pic_,file_] := (
     Display[StringJoin["!psfix -lmarg 0 -rmarg 0 -tmarg 0 -bmarg 0 -width ",
     ToString[graphWidth], " -height ", ToString[graphWidth*ar],
     " | sed -e '1s/.*/%!/' >", file], pic];)

PSSave[meanPlot, "lc.ps"]

