• Nenhum resultado encontrado

Histograms ( mrv_histogram.h )

No documento A user – friendly M inimum S panning T ree (páginas 175-182)

APPENDIX A. THE MORAVA PACK alter the input vector. If the vector is to remain unaltered, use a copy before calling the functions.

Median computes the median of the elements of a real vector by using the faster method supplied in MoravaPack. For the current version, this is

MedianWithQSelect. Thus, if the vector is not sorted, then partial reorder- ing of the elements will take place.

MedianWithQSelectuses the QuickSelect

If the second argument is bypassed, the default valuefalseis used.

MedianWithSort returns the median of a real vector

. The second argument (if not given,falseis implied) defines sorts the real vector usingstd::sortof C++STL and returns the median.

MedianWithQSelect

A.17. Histograms (mrv_histogram.h)

14 uInt Bins() const;

15 Real BinWidth() const;

16

17 uInt Count(uInt i) const;

18 uInt MaxCount() const;

19 uInt TotalCount() const;

20 Real RelativeFrequency(uInt) const;

21 Real ProbabilityDensity(uInt) const;

22 Real Height(uInt, Type) const;

23 Real MaxHeight(Type) const;

24

25 Real LowerBound() const;

26 Real LowerBound(uInt) const;

27 Real Midpoint() const;

28 Real Midpoint(uInt) const;

29 Real UpperBound() const;

30 Real UpperBound(uInt) const;

31

32 uIntVector Counts() const;

33 RealVector RealCounts() const;

34 RealVector Midpoints() const;

35 RealVector Bounds() const;

36 RealVector RelativeFrequencies() const;

37 RealVector ProbabilityDensities() const;

38 };

The enumeration type (enum)Typedenotes the three types of histograms that are directly implemented in the class. It can useful to programs using the library in order to ask users what is the type of their choice and to distinguish which method of the class are to be called to extract information:

Frequency denotes standard histogram. Each bin is associated with counts (natural numbers.)

Relative refers to the relative frequency of each bin with respect to the number of observations. E.g. if 10 out of the 100 observations where binned together, the relative frequency of the bin is0.1.

Density divides each frequency to the total area so that each bin is associated with a rough estimate of the probability density in that range.

The enumtype isin the scopeof the class. Thus, using it requires as prefix the

APPENDIX A. THE MORAVA PACK name of the class followed by the scope resolution operator (Histogram::) as shown in ListingA.27.

Hopefully, the large number of methods will cover most ways a user would interact with a histogram:

Histogram() , the default constructor, sets the values that describe the state of the histogram to0orNaNaccordingly (counters, reals) so that any requests are properly answered.

Histogram(V, M) creates the histogram of the data in theRealVectorV with M bins. IfM is not given or is equal to0, then theTerrell–Scott binning rule(Terrell & Scott,1985) is used:

M =d2N1/3e (A.9)

whereN is the number of observations inV. All bins are equal in width.

Bins,BinWidth return the number of binsM and their widthw

Count(i) returns the counts in thei–th bin (zero–based)Bi

MaxCount return the counts of the most crowded binBmax Bmax= max{B0, B1,· · ·BM−1}

TotalCount return the total number of counts (number of observations) N

M−1

X

i=0

Bi

RelativeFrequency(i),ProbabilityDensity(i) return thei–th bin’s relative frequency (Ri) and probability density estimate on the center of the binning interval:

Ri = Bi N Fi = Bi

(area) = Bi

wN = M Bi

N(max(V)−min(V))

A.17. Histograms (mrv_histogram.h)

Height(i, type), MaxHeight(type) returns the height of thei–th bin and the height of the most crowded one respectively, where the height depends on the histogram type.

LowerBound(i), Midpoint(i), UpperBound(i) return the lower, middle and higher value in the range of the i–th bin. If i is not given, total range endpoints and middle point are returned.

Counts returns all counts of the bins in a vector. Use RealCounts to get the floating–point equivalent (useful for statistical functions expecting

RealVectors.)

Midpoints, Bounds return theM midpoints and theM+ 1boundary values of the bins respectively.

RelativeFrequencies,ProbabilityDensities return vectors holding the rela- tive frequencies and the probability density estimates of the bins respec- tively.

Example

The following code demonstrates how to use histograms, aided by functions that are not part of C++or Morava library:

get_data, could ask user for input, or read from a file,

plot_xy(X,Y)would draw a polylinedefined by its points withxand ycoordinates grouped together in the vectors to be passed as arguments

plot_rect(x1, y1, x2, y2)would draw a rectangle with sides parallel to coordinate system axes, with corners at points(x1, y1)and(x2, y2).

Listing A.27 Using histograms

1 // get input and create the histogram with 20 bins

2 RealVector V = get_data();

3 Histogram H(V, 20);

4

5 // ask user what kind of histogram to plot

6 Histogram::Type HT;

7 char choice;

8 cout << "Choose a type for the histogram (f=freq|r=rel.freq.|d=density): ";

sequence of connected line segments

APPENDIX A. THE MORAVA PACK

9 cin >> choice;

10

11 // the choice defines the y-values

12 RealVector Y;

13 if (choice == ’f’ || choice == ’F’)

14 HT = Histogram::Frequency;

15 else if (choice == ’r’ || choice == ’R’)

16 HT = Histogram::Relative;

17 else if (choice == ’d’ || choice == ’D’)

18 HT = Histogram::Density;

19 else

20 {

21 cout << "Unknown histogram type";

22 return;

23 }

24

25 // if density, plot it like a function, else, make a bar chart

26 if (HT == Histogram::Density)

27 plot_xy(H.Midpoints(), H.Height(HT);

28 else

29 for(int i = 0; i < H.Bins(); ++i)

30 draw_rect(H.LowerBound(i), 0, H.UpperBound(i), H.Height(HT));

This page intentionally left blank

B

Cosmology–oriented code

B.1 Purpose and namespace

The MoravaPack library can be used for geometrical graphs independently of the context. Using it for cosmological data required more code: a distance calculator and the representation of DEUSS halo catalogs (Raseraet al.,2010) in a special type we callHaloGraph.

For better organization, readability and extensibility of the code, we enclosed the cosmology–related identifiers in thecsmnamespace.

For example, code without theusingdirectives:

1 using namespace mrv;

2 using namespace csm;

look like this:

Listing B.1 Two namespaces

1 csm::DistanceCalc D(h_param, omega_l, max_redshift, tolerance);

2 mrv::Real redshift = 2.3;

No documento A user – friendly M inimum S panning T ree (páginas 175-182)