6

Can you help me out with drawing a simple cumulative distribution function of a discrete variable, which has the following values:

x=1, f(x)=1/15;
x=2, f(x)=2/15;
x=3, f(x)=1/5;
x=4, f(x)=4/15;
x=5, f(x)=1/3

Most resources show how to do it for continuous variables. The question is very trivial because I am a newbie. Thank you.

EDIT:

\documentclass[paper=a4, fontsize=11pt]{scrartcl} % A4 paper and 11pt font size

\usepackage[T1]{fontenc} % Use 8-bit encoding that has 256 glyphs
\usepackage{fourier} % Use the Adobe Utopia font for the document - comment this line to return to the LaTeX default
\usepackage[english]{babel} % English language/hyphenation
\usepackage{amsmath,amsfonts,amsthm} % Math packages

\usepackage{lipsum} % Used for inserting dummy 'Lorem ipsum' text into the template

\usepackage{sectsty} % Allows customizing section commands
\allsectionsfont{\centering \normalfont\scshape} % Make all sections centered, the default font and small caps

\usepackage{fancyhdr} % Custom headers and footers
\usepackage{xfrac}
\pagestyle{fancyplain} % Makes all pages in the document conform to the custom headers and footers
\fancyhead{} % No page header - if you want one, create it in the same way as the footers below
\fancyfoot[L]{} % Empty left footer
\fancyfoot[C]{} % Empty center footer
\fancyfoot[R]{\thepage} % Page numbering for right footer
\renewcommand{\headrulewidth}{0pt} % Remove header underlines
\renewcommand{\footrulewidth}{0pt} % Remove footer underlines
\setlength{\headheight}{13.6pt} % Customize the height of the header

\numberwithin{equation}{section} % Number equations within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)
\numberwithin{figure}{section} % Number figures within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)
\numberwithin{table}{section} % Number tables within sections (i.e. 1.1, 1.2, 2.1, 2.2 instead of 1, 2, 3, 4)

\setlength\parindent{0pt} % Removes all indentation from paragraphs - comment this line for an assignment with lots of text

%----------------------------------------------------------------------------------------
%   TITLE SECTION
%----------------------------------------------------------------------------------------

\newcommand{\horrule}[1]{\rule{\linewidth}{#1}} % Create horizontal rule command with 1 argument of height

\title{ 
\normalfont \normalsize 
\textsc{\LARGE XXXX} \\ [20pt]
\textsc{ XXX}  \\ % Your university, school and/or department name(s)
\textsc{ XXX}  \\ [20pt]
\horrule{0.5pt} \\[0.4cm] % Thin top horizontal rule
\huge Problem Set 1 \\ % The assignment title
\horrule{2pt} \\[0.5cm] % Thick bottom horizontal rule
}

\author{XXX  \textsc{XXX}} % Your name

\date{\normalsize\today} % Today's date or a custom date

\begin{document}

\maketitle % Print the title

%----------------------------------------------------------------------------------------
%   PROBLEM 1
%----------------------------------------------------------------------------------------


1. Let X be a discrete random variable with density function


\[ P(x) = \left\{ 
  \begin{array}{l l}
    cx & \quad \text{for $x$ =1, 2, 3, 4, 5}\\
    0 & \quad \text{otherwise}
  \end{array} \right.\]
 \\ The discrete probability density function (PDF) of a discrete random variable $X$ provides the probabilities P($X=x$) for all possible values of $x$. In our case, \\

P($x$=1)=$1\times c$,\\
P($x$=2)=$2\times c$,\\
P($x$=3)=$3\times c$,\\
P($x$=4)=$4\times c$,\\
P($x$=5)=$5\times c$, $0$ otherwise\\

$1\times c +   2\times c +3\times c +4\times c +5\times c=1 \rightarrow c=\dfrac{1}{15}$

  The probability density function for $X$ is:\\

\begin{tabular*}{1\textwidth}{@{\extracolsep{\fill} } | c | c | c | c | c | c |} 
  \hline
   x & 1 & 2 & 3 & 4& 5 \\ [1ex]
  \hline
  \raisebox{2ex}f(x)& $1\times \dfrac{1}{15}=\dfrac{1}{15}$  & $2\times \dfrac{1}{15}=\dfrac{2}{15}$   & $3\times\dfrac{1}{15} =\dfrac{1}{5}$  & $4\times \dfrac{1}{15}=\dfrac{4}{15}$  & $5\times \dfrac{1}{15} =\dfrac{1}{3}$ \\ [1.5ex]
  \hline
\end{tabular*}\\\\

 A. What is $c$? \\\\
 $c=\dfrac{1}{15}$ \\\\

 B. Find P($X$ is odd).\\\\
 P($X$ is odd)$= P($X$ =1)+ P($X$ =3)+ P($X$ =5)=\dfrac{1}{15}+\dfrac{1}{5}+\dfrac{1}{3}=\dfrac{9}{15}$ \\\\


 C. What is the cumulative distribution function for X? Plot the function.\\\\

 $P(X\leq1)=\dfrac{1}{15}$\\\\
 $P(X\leq2)=\dfrac{1}{15}+\dfrac{2}{15}=\dfrac{1}{5}$\\\\
 $P(X\leq3)=\dfrac{1}{15}+\dfrac{2}{15}+\dfrac{1}{5}=\dfrac{6}{15}$\\\\
 $P(X\leq4)=\dfrac{1}{15}+\dfrac{2}{15}+\dfrac{1}{5}+\dfrac{4}{15}=\dfrac{2}{3}$\\\\
 $P(X\leq5)=\dfrac{1}{15}+\dfrac{2}{15}+\dfrac{1}{5}+\dfrac{4}{15}+\dfrac{1}{3}=1$\\\\
\\\\
\\\\
\\\\
\\\\ 
\\\\
\\\\

\end{document}
8
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}.
    – user31729
    Aug 28 '14 at 15:44
  • It is not clear, by which tool you want to get a graph? LaTeX/pstricks/tikz, including external images.
    – user31729
    Aug 28 '14 at 15:45
  • Thanks! Here it is. Please suggest a simple package that can be used for drawing graphs like that.
    – Olga
    Aug 28 '14 at 16:00
  • A tip: If you indent lines by 4 spaces or enclose words in backticks `, they'll be marked as code, as can be seen in my edit. You can also highlight the code and click the "code" button (with "{}" on it).
    – user31729
    Aug 28 '14 at 16:02
  • Additional comment: Your example is by far not minimal ;-)
    – user31729
    Aug 28 '14 at 16:02
11

You can use the approach from Probability density function of Uniform Distribution.

The actual plot is defined using this code:

\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]{
x f(x)
0 0
1 1/15
2 2/15
3 1/5
4 4/15
5 1/3
6 0
};
\end{axis}

Here's the complete example document.

The big chunk of code between \makeatletter and \makeatother provides the discontinous plot style. The code chunk can be put in your preamble, it doesn't need to be repeated if you need several of these plots.

\documentclass{article}
\usepackage{pgfplots, pgfplotstable}
\usepackage{amsmath}

\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] (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}
C. What is the cumulative distribution function for X? Plot the function.

\begin{align*}
P(X\leq1) &= \dfrac{1}{15}\\
P(X\leq2) &= \dfrac{1}{15}+\dfrac{2}{15}=\dfrac{1}{5}\\
P(X\leq3) &= \dfrac{1}{15}+\dfrac{2}{15}+\dfrac{1}{5}=\dfrac{6}{15}\\
P(X\leq4) &= \dfrac{1}{15}+\dfrac{2}{15}+\dfrac{1}{5}+\dfrac{4}{15}=\dfrac{2}{3}\\
P(X\leq5) &= \dfrac{1}{15}+\dfrac{2}{15}+\dfrac{1}{5}+\dfrac{4}{15}+\dfrac{1}{3}=1
\end{align*}

{\centering
\begin{tikzpicture}
\begin{axis}[
    clip=false,
    jump mark left,
    ymin=0,ymax=1,
    xmin=0, xmax=5,
    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]{
x f(x)
0 1/15
1 2/15
2 1/5
3 4/15
4 1/3
5 0
};
\end{axis}
\end{tikzpicture}
\par}
\end{document}

To center the image, you can wrap the \begin{tikzpicture} ... \end{tikzpicture} block in

{\centering
...
\par}

5
  • This looks perfect. Greatly appreciate your input.
    – Olga
    Aug 28 '14 at 16:47
  • 1
    @Olga: I would recommend you wait a bit (a day or so) before accepting an answer, it's very likely that someone will come up with a completely different, quite possibly much better approach.
    – Jake
    Aug 28 '14 at 16:50
  • How do you place this picture in the center? Right now, it's located to the left of my document. \centering doesn't work for me.
    – Olga
    Aug 28 '14 at 16:53
  • 1
    @Olga: I've edited my answer to show how to center the picture. I assume that you've either tried to put the \centering within the tikzpicture environment, or that you forgot to put a \par after the tikzpicture
    – Jake
    Aug 28 '14 at 17:01
  • @Jake Any chance that you know why am I getting these errors when copying your code? Unexpected close group } after \begin{scope} Unclosed open group { found at \end{scope} Unexpected \end{scope} Unexpected close group } Unexpected close group } Unexpected close group } These errors concern the last 4 lines before \makeatother
    – user148199
    May 12 '19 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.