3

The overall achievement vector of a person/household is designated by x=(x_1 \cdots x_n). To demonstrate, imagine that there are five persons in a society with over all income $25, $18, $15, $33, $31. In this case x=(25, 18, 15, 33, 31) is a vector indicating the income of the society. As can be seen, ordering/ranking the overall achievement vector; in other words, ranking/ordering individuals or households by their achievements yields (15, 18, 25, 31, 33). The aim here is to plot the cumulative distribution function for X? It is an excerpt my research Thesis - a Welfare Analysis approach in the Multidimensional Poverty Analysis domains!

\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=1,
    xmin=0, xmax=6,
    every axis plot/.style={very thick},
    discontinuous,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
\addplot [red] table [y=cumulative distribution]{
P(x) f(x)
<15  0
15  1/5
18  2/5
25 3/5
31 4/5
33 1
};
\end{axis}
gernot
  • 49,614

1 Answers1

7

I have adjusted the following things to obtain the result below:

  • The code for the key discontinuous is copied from here.

  • The x and y ranges didn't fit; I adjusted them to

    ymin=0,ymax=3.5, xmin=14,xmax=35,
    
  • Currently the first column is taken as the x values, hence "<15" doesn't work. I replaced it by 14, the smallest value of the range.

  • I added the upper bound of the x range (35) to the table, such that the last accumulated value is also drawn.

enter image description here

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\makeatletter
\long\def\ifnodedefined#1#2#3{%
    \@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}

\pgfplotsset{
    discontinuous/.style={
    scatter,
    scatter/@pre marker code/.code={
        \ifnodedefined{marker}{
            \pgfpointdiff{\pgfpointanchor{marker}{center}}%
             {\pgfpoint{0}{0}}%
             \ifdim\pgf@y>0pt
                \tikzset{options/.style={mark=*, fill=white}}
                \draw [densely dashed,blue] (marker-|0,0) -- (0,0);
                \draw plot [mark=*] coordinates {(marker-|0,0)};
             \else
                \tikzset{options/.style={mark=none}}
             \fi
        }{
            \tikzset{options/.style={mark=none}}        
        }
        \coordinate (marker) at (0,0);
        \begin{scope}[options]
    },
    scatter/@post marker code/.code={\end{scope}}
    }
}

\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=3.5,
    xmin=14,xmax=35,
    xlabel={income},
    ylabel={cumulative distribution},
    every axis plot/.style={very thick},
    discontinuous,
    table/create on use/cumulative distribution/.style={
        create col/expr={\pgfmathaccuma + \thisrow{f(x)}}   
    }
]
\addplot [red] table [y=cumulative distribution]{
P(x) f(x)
14   0
15  1/5
18  2/5
25 3/5
31 4/5
33 1
35 0
};
\end{axis}
\end{tikzpicture}
\end{document}
gernot
  • 49,614
  • See http://tex.stackexchange.com/a/198397/586 for the likely definition of discontinous. – Torbjørn T. Jan 13 '17 at 14:36
  • @TorbjørnT. Thanks, I've adapted the answer. – gernot Jan 13 '17 at 14:41
  • @ Torbjørn T. Hello, Sir, I thank you so much for your kind effective assistance. It perfectly serves the purpose and my challenges are simplified. Big apologies for bothering you, yet, I have some more questions.
    1. While the min. and max. values of the CDF and hence the "y"-value are supposed to be (0) and (1) respectively, how do we justify values greater than 1 (like 2, 3).
    2. Can we have alternative color for the CDF step functions? horizontal "red" and the vertical (jump) "blue".

    I thank you!

    – Hailai Weldeslassie Jan 13 '17 at 16:57
  • Plus, How do I write(include) income in the horizontal (x-axis) and Cumulative Distribution in the vertical (Y-axis)? – Hailai Weldeslassie Jan 13 '17 at 17:12
  • @HailaiWeldeslassie I have added labels to the axes and coloured the vertical lines in blue. What do you mean by justifying y-values bigger than 1? If you accumulate the values, the y-values may become arbitrarily large. Calculate the sums to check. – gernot Jan 13 '17 at 21:47
  • @gernot Thank so much for your kind assistance. Big apologies for not responding swiftly owing to problems beyond my control. To explicate my last question on the 'y'-value, it's a probability of CDF and the maximum value must add up to 1. Thus, could it be possible to make some adjustment taking this into account.

    Other issues were addressed sufficiently and fit the purpose perfectly. I am very appreciative in advance for your usual priceless assistance. I look forward to hearing from you shortly as I am still stuck with the above mentioned problem.

    Thank you!

    – Hailai Weldeslassie Jan 25 '17 at 08:58
  • @HailaiWeldeslassie I still don't understand the problem. The data is given, and you want the graph to show the sum of the values seen so far. This is what the plot currently shows. You can rescale your data by dividing it by the sum of the y-values (before plotting it). Or do you just want to relabel the y-axis to run from 0 to 1 instead of from 0 to sum-of-values? – gernot Jan 25 '17 at 09:46
  • @gernot Dear and Esteemed Gernot, the precise answer is I want to relabel the y-axis to run from 0 to 1. To succinctly explicate my specific interest, in the wealth analysis (in my research), I am going to identify household (individuals) as poor denoted by (1) and non-poor by zero. For this reason, it will not be logical (in my specific case) to have values greater than one in the y-axis. Thank you very much! – Hailai Weldeslassie Jan 25 '17 at 10:23
  • @HailaiWeldeslassie I have opened a new question for this remaining problem, see http://tex.stackexchange.com/questions/350364/dynamic-re-labeling-of-y-axis – gernot Jan 25 '17 at 11:34
  • @HailaiWeldeslassie See this answer regarding the labeling of the y-axis. If I understood you correctly, this solves your problem. – gernot Jan 25 '17 at 13:52
  • @gernot YES! It solves my problem perfectly. Thanks so much dear! I am humbled and encouraged by your earthly and cordial assistance! GOD BLESS! – Hailai Weldeslassie Jan 26 '17 at 14:27