
BeginPackage["UVW`DataRep`"]


Histogram::usage = "
Histogram[listofdata,listofbounds] represents the histogram of the data 
contained in listofdata. The bounds of the classes are read in 
listofbounds."


RegularHisto::usage = "
RegularHisto[listofdata,xmin,xmax,nx] represents the histogram of the data 
contained in list of data. There are nx regular classes between xmin 
and xmax."


SamplePlot2D::usage = "
SamplePlot2D[listof2Ddata] plots in the plane the points whose coordinates 
are read in listof2Ddata."


Histogram2D::usage = "
Histogram2D[listof2Ddata,xmin,xmax,nx,ymin,ymax,ny] represents a histogram 
in 3 dimensions for the two dimensional data contained in listof2Ddata. 
The classes are regular. There are nx classes in abscissa between xmin 
and xmax, and ny classes in ordinate between ymin and ymax."


LargeNumbers::usage = "
LargeNumbers[listofdata] plots the partial means of the data contained in 
listofdata."


CentralLimit::usage = "
CentralLimit[listofdata, mu, sigma, n] takes consecutive groups of n data in 
listofdata. The sum of each group is centered by n*mu then divided 
by Sqrt[n]*sigma. The results are represented on a regular histogram 
with 20 classes."



Begin["`Private`"]


Histogram[listofdata_List,listofbounds_List,opts___]:=

    Block[{list,nbclasses,cardinal,i,ordinates,abscissas,g},

       list = Cases[listofdata,x_/;First[listofbounds]<x<Last[listofbounds]];
       nbclasses = Length[listofbounds]-1;

       cardinal = Table[Length[Select[list,
				listofbounds[[i]]<#<=listofbounds[[i+1]] &]],
			  {i,1,nbclasses}];

       cardinal = cardinal / (Drop[listofbounds,1]
				  - Drop[listofbounds,-1]);
       cardinal = cardinal / Length[list];

       ordinates = Flatten[Transpose[{cardinal,cardinal,
				      Table[0,{nbclasses}]}]];
       
       
       abscissas = Flatten[Transpose[{Drop[listofbounds,-1],
					  Drop[listofbounds,1],
					      Drop[listofbounds,1]}]];
       
       g = Transpose[{abscissas,ordinates}];
       g = Prepend[g,{First[listofbounds],0}];

       ListPlot[g,
		 opts,
		 PlotRange  -> All,
		 PlotJoined -> True]
    ]



RegularHisto[listofdata_List,xmin_,xmax_,nx_,opts___]:=

  Block[{cardinal,list,step,i,abscissas,ordinates,g},

	step  = N[(xmax-xmin)/nx];

	list = Cases[listofdata,x_/;xmin<x<xmax];
	list = list - xmin;
	list = list/step;
	list = Ceiling[list];

	cardinal=Table[Count[list,i],{i,1,nx}];
	cardinal=cardinal / (step*Length[list]);

	ordinates = Flatten[Transpose[{cardinal,cardinal,Table[0,{nx}]}]];
	abscissas = Flatten[Transpose[{Range[xmin,xmax-step,step],
					    Range[xmin+step,xmax,step],
						 Range[xmin+step,xmax,step]}]]; 
						 
	g = Transpose[{abscissas,ordinates}];                                         
	g = Prepend[g,{xmin,0}];
     

	ListPlot[g, 
		    opts,
		    PlotJoined -> True,
		    PlotRange  -> All ]

	]




Histogram2D[list2Dofdata_List,xmin_,xmax_,nx_,ymin_,ymax_,ny_,opts___]:=

Block[{cardinal,xmesh,ymesh,list,stepx,stepy,g,i,j},

      stepx   = N[(xmax-xmin)/nx];
      stepy   = N[(ymax-ymin)/ny];

      list   = Cases[list2Dofdata,x_List/;
		      xmin<First[x]<xmax && ymin<Last[x]<ymax];
      list   = Transpose[ {(Transpose[list][[1]] - xmin)/stepx ,
			   (Transpose[list][[2]] - ymin)/stepy } ]; 
      list   = Ceiling[list];

      cardinal = Table[Count[list,{i,j}],{i,1,nx},{j,1,ny}];
      cardinal = cardinal (Max[N[xmax-xmin],N[ymax-ymin]]/Max[cardinal]);
      xmesh    = Range[xmin,xmax,stepx];
      ymesh    = Range[ymin,ymax,stepy];
   
	g=Table[{Polygon[{
		 {xmesh[[i]]    , ymesh[[j]]   ,0},
		 {xmesh[[i]]    , ymesh[[j]]   ,cardinal[[i,j]]},
		 {xmesh[[i+1]]  , ymesh[[j]]   ,cardinal[[i,j]]},
		 {xmesh[[i+1]]  , ymesh[[j]]   ,0}}],
		 Polygon[{
		 {xmesh[[i]]    , ymesh[[j]]   ,cardinal[[i,j]]},
		 {xmesh[[i+1]]  , ymesh[[j]]   ,cardinal[[i,j]]},
		 {xmesh[[i+1]]  , ymesh[[j+1]] ,cardinal[[i,j]]},
		 {xmesh[[i]]    , ymesh[[j+1]] ,cardinal[[i,j]]}}],
		 Polygon[{
		 {xmesh[[i+1]]  , ymesh[[j]]   ,0},
		 {xmesh[[i+1]]  , ymesh[[j]]   ,cardinal[[i,j]]},
		 {xmesh[[i+1]]  , ymesh[[j+1]] ,cardinal[[i,j]]},
		 {xmesh[[i+1]]  , ymesh[[j+1]] ,0}}]
		 },{i,1,nx},{j,1,ny}];

	 Show[Graphics3D[g],
	     opts,
	     PlotRange -> All,
	     AspectRatio -> Automatic,
	     Boxed -> True,
	     Axes -> Automatic,
	     Ticks ->Automatic,
	     AxesLabel ->{"x","y","f[x,y]"}
	     ]
       ]






SamplePlot2D[listof2Ddata_,opts___]:=

	     ListPlot[listof2Ddata,
		      opts,
		      PlotRange -> All,
		      AxesLabel -> {"x","y"}];




LargeNumbers[listofdata_List,opts___] :=
  Block[
    {listofmeans,len},

    listofmeans = Rest[FoldList[Plus,0.,listofdata]];
    len=Length[listofdata];
    listofmeans = listofmeans / Range[len];

    ListPlot[listofmeans , 

	     PlotRange -> All,
	     Prolog    -> PointSize[0.001],
	     opts
	    ]
       ]


CentralLimit[ listofdata_List,mu_,sigma_,n_,opts___] :=
  Block[
    {listpart, max,min,g1,g2},
    

    listpart = Transpose[Partition[listofdata,n]];
    listpart = Apply[Plus,listpart];
    listpart = listpart - (N[n] mu);
    listpart = listpart/(Sqrt[N[n]] sigma);
    
    min = Min[listpart];
    max = Max[listpart];
   
    g1 := RegularHisto[listpart,min,max,20,
		   DisplayFunction -> Identity];

    g2 := Plot[Exp[-(x^2)/2] / Sqrt[2 Pi], {x,min,max},
		   DisplayFunction -> Identity];

	      
    Show[g1,g2,DisplayFunction->$DisplayFunction]


    ]



End[ ]


EndPackage[ ]

