绘制离散变量的累积分布函数

绘制离散变量的累积分布函数

你能帮我画一个离散变量的简单累积分布函数吗?该函数具有以下值:

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

大多数资源都展示了如何对连续变量进行此操作。这个问题很简单,因为我是新手。谢谢。

编辑:

\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}

答案1

您可以使用以下方法均匀分布的概率密度函数

实际情节是使用此代码定义的:

\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}

这是完整的示例文档。

\makeatletter和之间的大段代码\makeatother提供了discontinous情节样式。代码块可以放在你的序言中,如果你需要几个这样的情节,就不需要重复。

\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}

要使图像居中,您可以将\begin{tikzpicture} ... \end{tikzpicture}块包裹在

{\centering
...
\par}

相关内容