diff --git a/4buttonboard.png b/4buttonboard.png new file mode 100644 index 0000000..89e4268 Binary files /dev/null and b/4buttonboard.png differ diff --git a/Lec01-NSD_Spring2018.ipynb b/Lec01-NSD_Spring2018.ipynb new file mode 100644 index 0000000..36e0cff --- /dev/null +++ b/Lec01-NSD_Spring2018.ipynb @@ -0,0 +1,684 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Stochastic calculus and its applications in quantitative finance\n", + "## National School of Development, Spring 2018 \n", + "\n", + "## Lecture 1: Probability space, random variable, and stochastic process\n", + "\n", + "\n", + "Tai-Ho Wang (王 太 和) \n", + " \n", + "

\n", + "and \n", + "
\n", + "

\n", + "\n", + "$$\n", + "\\newcommand{\\bea}{\\begin{eqnarray}}\n", + "\\newcommand{\\eea}{\\end{eqnarray}}\n", + "\\newcommand{\\supp}{\\mathrm{supp}}\n", + "\\newcommand{\\cB}{\\mathcal{B}}\n", + "\\newcommand{\\F}{\\mathcal{F} }\n", + "\\newcommand{\\G}{\\mathcal{G} }\n", + "\\newcommand{\\cG}{\\mathcal{G} }\n", + "\\newcommand{\\cF}{\\mathcal{F} }\n", + "\\newcommand{\\E}{\\mathbb{E} }\n", + "\\newcommand{\\Eof}[1]{\\mathbb{E}\\left[ #1 \\right]}\n", + "\\def\\Cov{{ \\mbox{Cov} }}\n", + "\\def\\Var{{ \\mbox{Var} }}\n", + "\\newcommand{\\1}{\\mathbf{1} }\n", + "\\newcommand{\\P}{\\mathbb{P} }\n", + "\\newcommand{\\Pof}[1]{\\mathbb{P}\\left[ #1 \\right]}\n", + "\\newcommand{\\QQ}{\\mathbb{Q} }\n", + "\\newcommand{\\R}{\\mathbb{R} }\n", + "\\newcommand{\\DD}{\\mathbb{D} }\n", + "\\newcommand{\\HH}{\\mathbb{H} }\n", + "\\newcommand{\\spn}{\\mathrm{span} }\n", + "\\newcommand{\\cov}{\\mathrm{cov} }\n", + "\\newcommand{\\HS}{\\mathcal{L}_{\\mathrm{HS}} }\n", + "\\newcommand{\\trace}{\\mathrm{trace} }\n", + "\\newcommand{\\LL}{\\mathcal{L} }\n", + "\\newcommand{\\s}{\\mathcal{S} }\n", + "\\newcommand{\\ee}{\\mathcal{E} }\n", + "\\newcommand{\\ff}{\\mathcal{F} }\n", + "\\newcommand{\\hh}{\\mathcal{H} }\n", + "\\newcommand{\\bb}{\\mathcal{B} }\n", + "\\newcommand{\\dd}{\\mathcal{D} }\n", + "\\newcommand{\\g}{\\mathcal{G} }\n", + "\\newcommand{\\half}{\\frac{1}{2} }\n", + "\\newcommand{\\T}{\\mathcal{T} }\n", + "\\newcommand{\\bit}{\\begin{itemize}}\n", + "\\newcommand{\\eit}{\\end{itemize}}\n", + "\\newcommand{\\beq}{\\begin{equation}}\n", + "\\newcommand{\\eeq}{\\end{equation}}\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Coordinates\n", + "\n", + "* Email: tai-ho.wang@baruch.cuny.edu\n", + "* Webpage: mfe.baruch.cuny.edu/tai-ho.wang\n", + "* Office: 朗潤園505\n", + "\n", + "\n", + "- Office hour: Wednesdays 10 ~ 11am or by appointment\n", + "- TA: 陳鑫泉, cxq.ccer@gmail.com\n", + "- Discussion forum: http://forum.nsd.edu.cn/showforum-466.aspx\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Important dates\n", + "\n", + "- **No classes** on Wed, Mar-14-2018 and Thu, Mar-15-2018\n", + "\n", + "- **Make up classes**: Thu, Mar-22-2018 and Thu, Apr-19-2018 \n", + "\n", + "- **Midterm**: Thu, Apr-26-2018, 3:10pm ~ 5pm\n", + "\n", + "- **Final**: Wed, Jun-27-2018, 2pm ~ 4pm" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Outline of Lecture 1\n", + "\n", + "* Probability space \n", + "\n", + "* Conditional expectation and conditional probability\n", + "\n", + "* Properties of conditional expectation\n", + "\n", + "* Independence\n", + "\n", + "* Random variable\n", + "\n", + "* Stochastic process in general\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## A little bit of set theory\n", + "Let $\\Omega$ be a set.\n", + "\n", + "* $A^c = \\Omega\\setminus A := \\{x\\in\\Omega: x \\not\\in A\\}$\n", + "\n", + "\n", + "* $A \\setminus B := \\{x\\in A: x \\not\\in B\\}$\n", + "\n", + "\n", + "* $A \\cap B := \\{x \\in \\Omega : x \\in A \\mbox{ and } x \\in B\\}$\n", + "\n", + "\n", + "* $A \\cup B := \\{x \\in \\Omega : x \\in A \\mbox{ or } x \\in B\\}$\n", + "\n", + "\n", + "* $\\displaystyle\\bigcup_{i=1}^n A_i := \\{x \\in \\Omega: x \\in A_i \\mbox{ for some } 1 \\leq i \\leq n \\}$\n", + "\n", + "\n", + "* $\\displaystyle\\bigcup_{i=1}^\\infty A_i := \\{x \\in \\Omega: x \\in A_i \\mbox{ for some } i \\}$\n", + "\n", + " Note: $\\displaystyle\\bigcup_{i=1}^n A_i \\nearrow \\displaystyle\\bigcup_{i=1}^\\infty A_i$ as $n\\to\\infty$\n", + "\n", + "\n", + "* $\\displaystyle\\bigcap_{i=1}^n A_i := \\{x \\in \\Omega: x \\in A_i \\mbox{ for all } i = 1,2,\\cdots,n \\}$\n", + "\n", + "\n", + "* $\\displaystyle\\bigcap_{i=1}^\\infty A_i := \\{x \\in \\Omega: x \\in A_i \\mbox{ for all } i \\}$\n", + "\n", + " Note: $\\displaystyle\\bigcap_{i=1}^n A_i \\searrow \\displaystyle\\bigcap_{i=1}^\\infty A_i$ as $n\\to\\infty$\n", + "\n", + "\n", + "* $2^\\Omega$ := the collection all subsets of $\\Omega$. $2^\\Omega$ is called the *power set* of $\\Omega$\n", + "\n", + "\n", + "* De Morgan's law\n", + " $$ \n", + " \\left(\\bigcup_{i=1}^\\infty A_i \\right)^c = \\bigcap_{i=1}^\\infty A_i^c\n", + " $$ \n", + " \n", + " $$\n", + " \\left(\\bigcap_{i=1}^\\infty A_i \\right)^c = \\bigcup_{i=1}^\\infty A_i^c\n", + " $$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## $\\sigma$-algebra\n", + "\n", + "Let $\\Omega$ be a set and $\\cF$ a collection of subsets of $\\Omega$. \n", + "\n", + "#### Definition\n", + "$\\cF$ is called a *$\\sigma$-algebra* if it satisfies\n", + "\n", + "* $\\emptyset, \\Omega \\in \\cF$\n", + "\n", + "\n", + "* If $A \\in \\cF$, then $A^c \\in \\cF$\n", + "\n", + "\n", + "* If a sequence of sets $\\{A_i\\}_{i=1}^\\infty$ satisfying $A_i \\in \\cF$ for all $i$, then $\\bigcup_{i=1}^\\infty A_i \\in \\cF$. \n", + "\n", + "#### Note\n", + "\n", + "* In probability theory, sets contained in $\\cF$ are usually referred to as *events*; while in measure theory they are called *$\\cF$-measurable sets* or simply *measurable sets* for short. \n", + "\n", + "\n", + "* Intuitively, we can think of the sets in $\\cF$ as the information in disposal and they are the sets that we can and will assign probabilities to. \n", + "\n", + "\n", + "* Typical examples are
\n", + " 1) the finest $\\sigma$-algebra $2^\\Omega$
\n", + " 2) the coarest $\\sigma$-algebra $\\{\\emptyset,\\Omega\\}$
\n", + " 3) the Borel sets in $\\R$: the smallest $\\sigma$-algebra that contains all the open intervals
" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Probability space\n", + "\n", + "A probability space, usually denoted by a triplet $(\\Omega,\\cF,\\P)$, consists of \n", + "\n", + "* a sample space $\\Omega$, which contains all possible outcomes of random experiments/scenarios under consideration\n", + "\n", + "\n", + "* a $\\sigma$-algebra containing all the subsets to which probabilities are to be assigned.\n", + "\n", + "\n", + "* a probability measure, which is a set function assigning probabilities to every event in $\\cF$ and satisfies \n", + " \n", + " * $\\Pof{\\Omega}=1$\n", + " \n", + " * $0 \\leq \\Pof{E} \\leq 1$ for every $E \\in \\cF$ \n", + "
\n", + " * (countable additivity) If $E_1,E_2,\\cdots \\in \\cF$ and $E_i\\cap E_j = \\emptyset$ for all $i \\neq j$, then \n", + " \n", + " $$\n", + " \\Pof{\\bigcup_{i=1}^\\infty E_i} = \\sum_{i=1}^\\infty \\Pof{E_i}\n", + " $$\n", + " \n", + "#### Note\n", + "In most of the elementary probability questions, the sample space $\\Omega$ is usually a finite set and each sample point is equally likely to occur. In mathematical terms, $\\Omega = \\{\\omega_1, \\cdots, \\omega_n\\}$ and $\\Pof{\\omega_i} = \\frac1n$, for $i = 1,\\cdots,n$. \n", + " \n", + "#### Remark\n", + "\n", + "* A sequence of events $\\{E_i\\}_{i \\geq 1}$ is called *mutually disjoint* or *mutually exclusive* if $E_i \\cap E_j = \\emptyset$ for all $i \\neq j$.\n", + "\n", + "\n", + "* A sequence of events $\\{E_i\\}_{i \\geq 1}$ is called a *partition* of the sample space $\\Omega$ if it is mutually disjoint and $\\bigcup_{i\\geq 1} E_i = \\Omega$." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Properties of probability measusre\n", + "\n", + "* (finite additivity) If $A_1, \\cdots, A_n$ are mutually disjoint, then \n", + "\n", + " $$\n", + " \\Pof{\\bigcup_{i=1}^n A_i} = \\sum_{i=1}^n \\Pof{A_i}.\n", + " $$\n", + " \n", + " In particular, if $A \\cap B = \\emptyset$, then $\\Pof{A \\cup B} = \\Pof{A} + \\Pof{B}$.\n", + "\n", + "\n", + "* If $A \\subset B$, then $\\Pof{A} \\leq \\Pof{B}$\n", + "\n", + "\n", + "* $\\Pof{A^c} = 1 - \\Pof{A}$\n", + "\n", + "\n", + "* $\\Pof{A \\cup B} = \\Pof{A} + \\Pof{B} - \\Pof{A \\cap B}$\n", + "\n", + "\n", + "* (Inclusion-exclusion formula)\n", + "\n", + " $$\n", + " \\begin{eqnarray*}\n", + " && \\Pof{\\bigcup_{i=1}^n E_i} = \\sum_{i=1}^n \\Pof{E_i} - \\sum_{i\\lt j} \\Pof{E_i \\cap E_j} + \\sum_{i\\lt j\\lt k} \\Pof{E_i\\cap E_j \\cap E_k} \\\\\n", + " && + \\cdots + (-1)^{n+1} \\Pof{E_1 \\cap \\cdots \\cap E_n}\n", + " \\end{eqnarray*}\n", + " $$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## An application of the inclusion-exclusion formula\n", + "\n", + "### Matching problem \n", + "\n", + "An absent-minded professor was preparing $n$ recommendation letters and envelopes that are supposed to send to $n$ distinct programs, but he ended up stuffing the letters into the envelopes randomly. What is the probability that none of the letters match their corresponding envelopes, assuming each scenario is equally likely to happen? \n", + "\n", + "\n", + "#### Note \n", + "This is a special case of the, slightly more general, well-known matching problem. Given the answer to question above, it is pretty straightforward to determine the probability (distribution) of exact $k$, $0 \\leq k\\leq n$, letters match their envelopes. (How?)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Conditional probability \n", + "\n", + "Let $A$, $B$ be two events with $\\Pof{B} > 0$. \n", + "\n", + "#### Definition\n", + "The conditional probability of $A$ conditioned on $B$, denoted by $\\Pof{A|B}$, is defined as\n", + "\n", + "$$\n", + "\\Pof{A|B} = \\frac{\\Pof{A \\cap B}}{\\Pof{B}}.\n", + "$$\n", + "\n", + "> It was as if we ignore all the sample points in $A$ that are not in $B$, then regard $B$ as a sample space to evaluate the probability of $A$ (normalized by the probability of $B$)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Properties of conditional probability \n", + "\n", + "* $\\Pof{\\cdot|B}$ is indeed a probability measure. \n", + "\n", + "\n", + "* $\\Pof{A \\cap B} = \\Pof{A|B} \\Pof{B} = \\Pof{B|A} \\Pof{A}$\n", + "\n", + "\n", + "* (Bayes' rule) \n", + "\n", + " $$\n", + " \\Pof{A|B} = \\frac{\\Pof{B|A}\\Pof{A}}{\\Pof{B|A}\\Pof{A} + \\Pof{B|A^c}\\Pof{A^c}}\n", + " $$\n", + " \n", + "\n", + "* (Generalized Bayes' rule) Let $\\{E_n\\}_{n=1}^N$ be a partition of the sample space $\\Omega$. Then\n", + "\n", + " $$\n", + " \\Pof{E_n|A} = \\frac{\\Pof{A|E_n}\\Pof{E_n}}{\\sum_{m=1}^N \\Pof{A|E_m}\\Pof{E_m}}\n", + " $$\n", + "\n", + "\n", + "* Let $\\{E_n\\}_{n=1}^N$ be a partition. Then\n", + "\n", + " $$\n", + " \\Pof{A} = \\sum_{n=1}^N \\Pof{A|E_n} \\Pof{E_n}\n", + " $$\n", + "\n", + "\n", + "* Let $\\{E_n\\}_{n=1}^N$ be a partition. Then\n", + "\n", + " $$\n", + " \\Pof{A|B} = \\sum_{n=1}^N \\Pof{A|B\\cap E_n} \\Pof{E_n | B}\n", + " $$" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Sunrise problem\n", + "\n", + "The sunrise problem, first introduced in the 18th century by Pierre-Simon Laplace, can be decribed as
\n", + "\"Given that we have observed sunrise every day, what is the probability that the sun will rise tomorrow morning?\"\n", + "\n", + "Let's consider a coin flip version of the problem as follows. There are $n+1$ coins numbered from $0$ to $n$. For $i=0,\\cdots,n$, the $i$th coins has probability $\\frac in$ turning up head if flipped. Therefore, the zeroth coin has no head whereas the $n$th coin has only heads. A coin was randomly chosen among the $n+1$ coins and has been flipped $k$ times, the results are all heads. What is the probability that the next flip is a head? " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Independence of events\n", + "\n", + "Two events $A$ and $B$ are called *independent* if \n", + "\n", + "$$\n", + "\\Pof{A\\cap B} = \\Pof{A} \\, \\Pof{B}.\n", + "$$\n", + "\n", + "#### Note\n", + "It is more intuitive if understood as $\\Pof{A|B} = \\Pof{A}$ or $\\Pof{B|A} = \\Pof{B}$.\n", + "\n", + "Three events are called *independent* if \n", + "\n", + "* $\\Pof{A \\cap B} = \\Pof{A} \\Pof{B}$, $\\Pof{A \\cap C} = \\Pof{A} \\Pof{C}$, and $\\Pof{B \\cap C} = \\Pof{B} \\Pof{C}$.\n", + "\n", + "\n", + "* $\\Pof{A \\cap B \\cap C} = \\Pof{A} \\Pof{B} \\Pof{C}$.\n", + "\n", + "The first condition is called *pairwise independent* which by itself alone does not imply the \"full independence\" of three events." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Random variable\n", + "\n", + "Let $(\\Omega, \\cF, \\P)$ be a probability space. \n", + "\n", + "#### Definition (Random variable)\n", + "A random variable on $\\Omega$ is a function from $\\Omega$ to $\\R$ such that \n", + "\n", + "$$\n", + "X^{-1}(B) := \\{ \\omega \\in \\Omega : X(\\omega) \\in B \\} \\in \\cF\n", + "$$\n", + "\n", + "for every Borel set $B \\subset \\R$.\n", + "\n", + "#### Note\n", + "In mathematical terms (measure theory), $X$ is called measurable function from $\\Omega$ to $\\R$.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## $\\sigma$-algebra generated by a random variable\n", + "\n", + "Let $X:\\Omega \\to \\R$. Then the collection of subsets of $\\Omega$ defined by\n", + "\n", + "$$\n", + "\\cF^X := \\{X^{-1}(B) \\subset \\Omega : B \\in \\cB \\}\n", + "$$\n", + "\n", + "is a $\\sigma$-algebra (why?). \n", + "\n", + "#### Definition \n", + "$\\cF^X$ is called the *$\\sigma$-algebra generated by $X$*. \n", + "\n", + "In other words, $\\cF^X$ is the smallest $\\sigma$-algebra that makes $X$ a random variable (or a measurable function). " + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## $\\sigma$-algebra generated by an event\n", + "\n", + "Let $A$ be an event. Define $\\cF^A$ by \n", + "\n", + "$$\n", + "\\cF^A = \\{\\emptyset, A, A^c, \\Omega\\}\n", + "$$\n", + "\n", + "which is obviously a $\\sigma$-algebra. \n", + "\n", + "#### Definition \n", + "$\\cF^A$ is called the $\\sigma$-algebra generated by $A$. \n", + "\n", + "#### Note\n", + "* $\\cF^A$ is the smallest $\\sigma$-algebra that contains the event $A$.\n", + "* $\\cF^A$ is also the $\\sigma$-algebra generated by the random variable $\\1_{A}$, which is also referred to as the indicator function of $A$. \n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Independence of random variables \n", + "\n", + "Two random variables $X$ and $Y$ are called *independent* if, for any Borel sets $A$ and $B$,\n", + "\n", + "$$\n", + "\\Pof{X\\in A, Y\\in B} = \\Pof{X \\in A}\\,\\Pof{Y \\in B}.\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Conditional expectation\n", + "\n", + "Let $(\\Omega, \\cF, \\P)$ be a probability space, $\\G$ a sub $\\sigma$-algebra of $\\cF$ and $X$ be a random variable with $\\E|X| < \\infty$.\n", + "\n", + "\n", + "#### Definition (Conditional expectation)\n", + "\n", + "The conditional expectation of $X$ given $\\G$, denoted by $\\E[X | \\G]$, is defined as any random variable $Y$ which satisfies \n", + "\n", + "* $Y$ is $\\G$-measurable.\n", + "\n", + "\n", + "* $\\displaystyle \\int_A X d\\P = \\int_A Y d\\P{}$ for all $A \\in \\G$.\n", + "\n", + "#### Remark\n", + "\n", + "* Any random variable $Y$ satisfies the two conditions in the definition is called a *version* of $\\E[X | \\G]$.\n", + "\n", + "* We can think of $\\G$ as describing the information we have, so $\\E[X|\\G]$ is the ``best guess\" of the value of $X$ given the information $\\G$, see the Minimizing property below." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Properties of conditional expectation\n", + "\n", + "Let $X$ and $Y$ be integrable random variables and $\\G$ be a $\\sigma$-algebra.\n", + "\n", + "* *Linearity*\n", + "\n", + " $\\E[\\alpha X + \\beta Y | \\G] = \\alpha \\E[X | \\G] + \\beta \\E[Y | \\G]$.\n", + "\n", + "\n", + "* *Monotonicity*\n", + "\n", + " If $X \\leq Y$, then $\\E[X | \\G] \\leq \\E[Y | \\G]$.\n", + "\n", + "\n", + "* *Chebyshev inequality* \n", + "\n", + " $\\lambda^2 \\Pof{|X| \\geq \\lambda |\\G} \\leq \\E[X^2|\\G]$ for all $\\lambda > 0$.\n", + "\n", + "\n", + "* *Jensen's inequality*\n", + "\n", + " If $\\phi$ is a convex function defined on $\\R$ and $\\E[|X|]< \\infty$, $\\E[|\\phi(X)|] < \\infty$, then $\\phi(\\E[X | \\G]) \\leq \\E[\\phi(X)|\\G]$.\n", + " \n", + "\n", + "* *Monotone Convergence Theorem*\n", + "\n", + " If $X_n \\geq 0$ for all $n$ and $X_n \\uparrow X$ with $\\E[X] \\leq +\\infty$, then $\\E[X_n | \\G] \\uparrow \\E[X |\\G]$.\n", + " \n", + "\n", + "* *Fatou's lemma*\n", + "\n", + " $X_n \\geq 0$ for all $n$, then $\\E[ \\liminf X_n |\\G] \\leq \\liminf \\E[X_n | \\G]$. \n", + " \n", + "\n", + "* *Lebesgue Dominating Convergence Theorem*\n", + "\n", + " If $X_n \\to X$ and $|X_n| \\leq Y$ with $\\E[Y] < \\infty$, then $\\E[X_n | \\G] \\to \\E[X | \\G]$.\n", + " \n", + " \n", + "* *Tower property*\n", + "\n", + " If $\\G_1 \\subset \\G_2$ are $\\sigma$-fields, then $\\E[ \\E[X | \\G_2] |\\G_1] = \\E[ \\E[X | \\G_1] |\\G_2] = \\E[X |\\G_1]$.\n", + " \n", + " \n", + "* If $X$ is $\\G$-measurable and $\\E[|Y|]<\\infty$, $\\E[|XY|]<\\infty$, then $\\E[XY | \\G] = X\\E[Y|\\G]$.\n", + "\n", + "\n", + "* *Minimizing property*\n", + "\n", + " If $\\E[X^2] < \\infty$, then $\\E[X|\\G]$ is the (unique) $\\G$-measurable random variable that minimizes the mean square error $\\E|X - Y|^2$ over the space of all square integrable $\\G$-measurable random variables.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Independence of $\\sigma$-algebras\n", + "\n", + "Let $\\cF$ and $\\cG$ be two $\\sigma$-algebras.\n", + "\n", + "#### Definition \n", + "$\\cF$ and $\\cG$ are called *independent* if for every $F \\in \\cF$ and $G \\in \\cG$ it holds that \n", + "\n", + "$$\n", + "\\Pof{F \\cap G} = \\Pof{F} \\; \\Pof{G}. \n", + "$$\n", + "\n", + "#### Theorem \n", + "Two random variables $X$ and $Y$ are independent if and only if the $\\sigma$-algebras $\\cF^X$ and $\\cF^Y$ are independent. \n", + "\n", + "For genenral case, we have \n", + "\n", + "#### Definition \n", + "$\\sigma$-algebras $\\cF_1, \\cdots, \\cF_n$ are called *independent* if for every $F_1 \\in \\cF_1, \\cdots, F_n \\in \\cF_n$, it holds that \n", + "\n", + "$$\n", + "\\Pof{\\bigcap_{i=1}^n F_i} = \\prod_{i=1}^n \\Pof{F_i}. \n", + "$$\n", + "\n", + "#### Theorem \n", + ">- Random variables $X_1, \\cdots, X_n$ are independent if and only if the $\\sigma$-algebras $\\cF^{X_1}, \\cdots, \\cF^{X_n}$ are independent.\n", + "\n", + ">- Events $A_1, \\cdots, A_n$ are independent if and only if the $\\sigma$-algebras $\\cF^{A_1}, \\cdots, \\cF^{A_n}$ are independent." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Stochastic process\n", + "\n", + "Let $(\\Omega, \\cF, \\P)$ be a probability space and $T=\\mathbb{N}\\cup\\{0\\}$ (the set of all nonnegative integers), $T = \\R$, or $T = \\{0,1,2,\\cdots, N\\}$, $N < \\infty$.\n", + "\n", + "#### Definition\n", + "A stochastic process is a map $X:T \\times \\Omega \\to \\R$ such that, for each $t\\in T$, $X_t=X_t(\\omega):=X(t,\\omega)$ is a random variable.\n", + "\n", + "\n", + "* For each fixed $\\omega \\in \\Omega$, the mapping $X(\\cdot,\\omega):T \\to \\R$ is called a *sample path*.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Filtration and filtered probability space\n", + "\n", + "\n", + "#### Definition (Filtration)\n", + "\n", + "An increasing family of $\\sigma$-fields indexed by the set $T$, i.e., $\\{ \\cF_t \\}_{t\\in T}$ is a family of $\\sigma$-fields such that $\\cF_s \\subset \\cF_t$ for every $s,t \\in T$ and $s < t$, is called a *filtration*. We\n", + " usually assume $\\cF_0 = \\{ \\emptyset, \\Omega\\}$ and $\\cF_N = \\cF$.\n", + "\n", + "\n", + "#### Definition (Filtered probability space)\n", + "\n", + "The probability space $(\\Omega, \\cF, \\P)$ equipped with a filtration $\\cF_t$, denoted by $(\\Omega, \\cF_t, \\P)$, is called a *filtered probability space*.\n", + "\n", + "\n", + "#### Definition (Adaptedness)\n", + "\n", + "A stochastic process $X$ is called *adapted* to the filtration $\\cF_t$ if $X_t$ is $\\cF_t$-measurable for every $t \\in T$.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Predictable process\n", + "\n", + "Let $(\\Omega, \\cF_t, \\P)$ be a filtered probability space.\n", + "\n", + "#### Definition (Predictable processes)\n", + "\n", + "A stochastic process $H$ defined on $\\Omega$ is called *predictable* if $H_t$ is $\\cF_{t-1}$-measurable for every $t \\in T$. Note that, as a convention, we don't define $H_0$. \n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Stopping time\n", + "\n", + "A random variable $\\tau:\\Omega \\to T$ is called a stopping time if $\\{\\tau \\leq t\\} \\in \\F_t$ for all $t \\in T$.\n", + "\n", + "\n", + "* In the discrete time case, the above definition of stopping time is equivalent to $\\{ \\tau = t \\} \\in \\F_t$ for all $t \\in T$.\n", + "\n", + "\n", + "* Typical stopping times are hitting times or the first time that a process leaves/enters a specific interval." + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/Lec02-NSD_Spring2018.ipynb b/Lec02-NSD_Spring2018.ipynb new file mode 100644 index 0000000..09be7f5 --- /dev/null +++ b/Lec02-NSD_Spring2018.ipynb @@ -0,0 +1,401 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "# Stochastic calculus and its applications in quantitative finance\n", + "## National School of Development, Spring 2018 \n", + "\n", + "## Lecture 2: Basic martingale theory\n", + "\n", + "\n", + "Tai-Ho Wang (王 太 和) \n", + " \n", + "

\n", + "and \n", + "
\n", + "

\n", + "\n", + "$$\n", + "\\newcommand{\\bea}{\\begin{eqnarray}}\n", + "\\newcommand{\\eea}{\\end{eqnarray}}\n", + "\\newcommand{\\supp}{\\mathrm{supp}}\n", + "\\newcommand{\\F}{\\mathcal{F} }\n", + "\\newcommand{\\G}{\\mathcal{G} }\n", + "\\newcommand{\\cF}{\\mathcal{F} }\n", + "\\newcommand{\\E}{\\mathbb{E} }\n", + "\\newcommand{\\Eof}[1]{\\mathbb{E}\\left[ #1 \\right]}\n", + "\\def\\Cov{{ \\mbox{Cov} }}\n", + "\\def\\Var{{ \\mbox{Var} }}\n", + "\\newcommand{\\1}{\\mathbf{1} }\n", + "\\newcommand{\\P}{\\mathbb{P} }\n", + "\\newcommand{\\Pof}[1]{\\mathbb{P}\\left[ #1 \\right]}\n", + "\\newcommand{\\QQ}{\\mathbb{Q} }\n", + "\\newcommand{\\R}{\\mathbb{R} }\n", + "\\newcommand{\\DD}{\\mathbb{D} }\n", + "\\newcommand{\\HH}{\\mathbb{H} }\n", + "\\newcommand{\\spn}{\\mathrm{span} }\n", + "\\newcommand{\\cov}{\\mathrm{cov} }\n", + "\\newcommand{\\HS}{\\mathcal{L}_{\\mathrm{HS}} }\n", + "\\newcommand{\\trace}{\\mathrm{trace} }\n", + "\\newcommand{\\LL}{\\mathcal{L} }\n", + "\\newcommand{\\s}{\\mathcal{S} }\n", + "\\newcommand{\\ee}{\\mathcal{E} }\n", + "\\newcommand{\\ff}{\\mathcal{F} }\n", + "\\newcommand{\\hh}{\\mathcal{H} }\n", + "\\newcommand{\\bb}{\\mathcal{B} }\n", + "\\newcommand{\\dd}{\\mathcal{D} }\n", + "\\newcommand{\\g}{\\mathcal{G} }\n", + "\\newcommand{\\half}{\\frac{1}{2} }\n", + "\\newcommand{\\T}{\\mathcal{T} }\n", + "\\newcommand{\\bit}{\\begin{itemize}}\n", + "\\newcommand{\\eit}{\\end{itemize}}\n", + "\\newcommand{\\beq}{\\begin{equation}}\n", + "\\newcommand{\\eeq}{\\end{equation}}\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Outline of Lecture 2\n", + "\n", + "* Review: filtered probability space, adapted stochastic processes\n", + "\n", + "* Martingale, submartingale, and supermartingale\n", + "\n", + "* Martingale transformation and discrete stochastic integral" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Probability space\n", + "\n", + "A probability space, usually denoted by a triplet $(\\Omega,\\cF,\\P)$, consists of \n", + "\n", + "* a sample space $\\Omega$, which contains all possible outcomes of random experiments/scenarios under consideration\n", + "\n", + "\n", + "* a $\\sigma$-algebra containing all the subsets to which probabilities are to be assigned.\n", + "\n", + "\n", + "* a probability measure, which is a set function assigning probabilities to every event in $\\cF$ and satisfies \n", + " \n", + " * $\\Pof{\\Omega}=1$\n", + " \n", + " * $0 \\leq \\Pof{E} \\leq 1$ for every $E \\in \\cF$ \n", + "
\n", + " * (countable additivity) If $E_1,E_2,\\cdots \\in \\cF$ and $E_i\\cap E_j = \\emptyset$ for all $i \\neq j$, then \n", + " \n", + " $$\n", + " \\Pof{\\bigcup_{i=1}^\\infty E_i} = \\sum_{i=1}^\\infty \\Pof{E_i}\n", + " $$" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Stochastic process\n", + "\n", + "Let $(\\Omega, \\cF, \\P)$ be a probability space and $T=\\mathbb{N}\\cup\\{0\\}$ (the set of all nonnegative integers), $T = \\R$, or $T = \\{0,1,2,\\cdots, N\\}$, $N < \\infty$.\n", + "\n", + "#### Definition\n", + "A stochastic process is a map $X:T \\times \\Omega \\to \\R$ such that, for each $t\\in T$, $X_t=X_t(\\omega):=X(t,\\omega)$ is a random variable.\n", + "\n", + "\n", + "* For each fixed $\\omega \\in \\Omega$, the mapping $X(\\cdot,\\omega):T \\to \\R$ is called a *sample path*.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Filtration and filtered probability space\n", + "\n", + "\n", + "#### Definition (Filtration)\n", + "\n", + "An increasing family of $\\sigma$-fields indexed by the set $T$, i.e., $\\{ \\cF_t \\}_{t\\in T}$ is a family of $\\sigma$-fields such that $\\cF_s \\subset \\cF_t$ for every $s,t \\in T$ and $s < t$, is called a *filtration*. We\n", + " usually assume $\\cF_0 = \\{ \\emptyset, \\Omega\\}$ and $\\cF_N = \\cF$.\n", + "\n", + "\n", + "#### Definition (Filtered probability space)\n", + "\n", + "The probability space $(\\Omega, \\cF, \\P)$ equipped with a filtration $\\cF_t$, denoted by $(\\Omega, \\cF_t, \\P)$, is called a *filtered probability space*.\n", + "\n", + "\n", + "#### Definition (Adaptedness)\n", + "\n", + "A stochastic process $X$ is called *adapted* to the filtration $\\cF_t$ if $X_t$ is $\\cF_t$-measurable for every $t \\in T$.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Martingale, sub, and super martingales\n", + "\n", + "Let $(\\Omega,\\cF_t,\\P)$ be a filtered probability space. A stochastic process $X$ is called a *martingale*\n", + "(*submartingale*, *supermartingale*) with respect to the filtration\n", + "$\\cF_t$ if it satisfies\n", + "\n", + "\n", + "* $\\E|X_t| < \\infty$ for every $t \\in T$.\n", + "\n", + "\n", + "* $X$ is adapted to $\\cF_t$.\n", + "\n", + "\n", + "* $\\E[ X_{t+1} | \\cF_t] = (\\geq,\\leq) X_t$ for all $t \\in T$.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Examples of martingale\n", + "\n", + "* A natural way to construct a martingale is by starting with an integrable random variable at the terminal time then conditioning backwards accordingly with respect to the filtration. Specifically, Let $Y$ be a random variable with $\\E|Y| < \\infty$ and define $X_t = \\E[Y | \\cF_t]$. Then $X_t$ apparently is a martingale.\n", + "\n", + "\n", + "* A natural question to ask is: do all the martingales come from conditioning backwards a terminal random variable? The answer is obviously yes if the time domain $T$ is a finite set. However, if the time domain is infinite, then we are essentially asking if the martingale sequence converges. \n", + "\n", + "\n", + "* Let $\\xi_1, \\xi_2, \\cdots$ be an iid sequence of random variables with $\\P[\\xi_i=1] = p$ and $\\P[\\xi_i=-1] = 1 - p$, for $i=1,2,\\cdots$, where $0 \\leq p \\leq 1$. Define $X_t = \\sum_{i=1}^t \\xi_i$. Then $X_t$ is a martingale if $p=1/2$; a supermartingale if $p<1/2$; a submartingale if $p>1/2$.\n", + "\n", + "\n", + "* Let $\\xi_1, \\xi_2, \\cdots$ be a sequence of integrable independent random variables with $\\E[\\xi_i] = 0$, for $i=1,2,\\cdots$. Define $X_t = \\sum_{i=1}^t \\xi_i$, i.e., $X_t$ is a random walk. Then $X_t$ is a martingale.\n", + "\n", + "\n", + "* If $X_t$ and $Y_t$ are two supermartingales, then $Z_t = X_t \\wedge Y_t$ is also a supermartingale." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Properties of martingale\n", + "\n", + "* For $t > s$,\n", + "\n", + " * if $X_t$ is a supermartingale, then $\\E[X_t | \\cF_s] \\leq X_s$.\n", + " \n", + " * if $X_t$ is a submartingale, then $\\E[X_t | \\cF_s] \\geq X_s$.\n", + " \n", + " * if $X_t$ is a martingale, then $\\E[X_t | \\cF_s] = X_s$.\n", + " " + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Predictable process\n", + "\n", + "Let $(\\Omega, \\cF_t, \\P)$ be a filtered probability space.\n", + "\n", + "#### Definition (Predictable processes)\n", + "\n", + "A stochastic process $H$ defined on $\\Omega$ is called *predictable* if $H_t$ is $\\cF_{t-1}$-measurable for every $t \\in T$. Note that, as a convention, we don't define $H_0$. \n", + "\n", + "\n", + "#### Theorem (Doob's decomposition)\n", + "\n", + "Any submartingale $X_t$ can be written in a unique way as $X_t = M_t + A_t$, where $M_t$ is a martingale and $A_t$ is a predictable and increasing sequence with $A_0=0$." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Discrete stochastic integral\n", + "\n", + "The *stochastic integral* of the predictable process $H_t$ with respect to the supermartingale (martingale, submartingale) $X_t$, denoted by $(H\\cdot X)_t$, is defined as\n", + "\n", + "$$\n", + " (H\\cdot X)_t = \\sum_{s=1}^t H_s \\Delta X_s,\n", + "$$\n", + "\n", + "where $\\Delta X_s = X_s - X_{s-1}$. As a convention, we define $(H \\cdot X)_0 = 0$.\n", + "\n", + "#### Remark\n", + "\n", + "Stochastic integral is a clever way of tracking P&L of a trading strategy over an investment horizon." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Properties of stochastic integral\n", + "\n", + "\n", + "* Let $X_t$ be a supermartingale (submartingale) and $H_t$ be nonnegative, bounded and predictable. Then $(H\\cdot X)_t$ is a supermartingale (submartingale).\n", + "\n", + "\n", + "* Let $X_t$ be a submartingale. Suppose $H_t$ and $K_t$ are bounded and predictable such that $H_t \\geq K_t$ for every $t$. Then $\\E[(H\\cdot X)_t] \\geq \\E[(K \\cdot X)_t]$ for every $t \\in T$.\n", + "\n", + "\n", + "* Let $X_t$ be a martingale and $H_t$ be bounded and predictable. (need not be nonnegative!) Then $(H\\cdot X)_t$ is a martingale.\n" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Stopping time\n", + "\n", + "A random variable $\\tau:\\Omega \\to T$ is called a stopping time if $\\{\\tau \\leq t\\} \\in \\F_t$ for all $t \\in T$.\n", + "\n", + "\n", + "* In the discrete time case, the above definition of stopping time is equivalent to $\\{ \\tau = t \\} \\in \\F_t$ for all $t \\in T$.\n", + "\n", + "\n", + "* Typical stopping times are hitting times or the first time that a process leaves/enters a specific interval." + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## Stopped processes\n", + "\n", + "#### Theorem \n", + "\n", + "If $\\tau$ is a stopping time and $X_t$ is a submartingale (martingale, supermartingale), then the stopped process $X_{\\tau \\wedge t}$ is also a submartingale (martingale, supermartingale).\n", + "\n", + "\n", + "#### Theorem\n", + "\n", + "If $X_t$ is a submartingale and $\\tau$ is a stopping time with $\\P[\\tau \\leq k] = 1$ (i.e., $\\tau$ is almost surely bounded above by $k$) for some $k\\in T$, then we have\n", + "\n", + "$$\n", + " \\E[X_0] \\leq \\E[X_{\\tau}] \\leq \\E[X_k].\n", + "$$\n", + "\n", + "Moreover, if $\\tau$ and $\\sigma$ are stopping times with\n", + "$\\P[\\sigma \\leq \\tau \\leq k] = 1$ (i.e., $\\sigma$ is almost surely\n", + "less than or equal to $\\tau$ and both of them are bounded above by\n", + "$k$), then\n", + "\n", + "$$\n", + " \\E[X_{\\sigma}] \\leq \\E[X_{\\tau}].\n", + "$$" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "slideshow": { + "slide_type": "slide" + } + }, + "source": [ + "## The optional stopping theorem\n", + "If $\\sigma \\leq \\tau$ are stopping times and $Y_{t\\wedge \\tau}$ is a uniformly integrable submartingale then \n", + "\n", + "$$\n", + "\\E[Y_\\sigma] \\leq \\E[Y_\\tau]\n", + "$$ \n", + "\n", + "and \n", + "\n", + "$$\n", + "Y_\\sigma \\leq \\E[Y_\\tau | \\F_\\sigma].\n", + "$$\n", + "\n", + "In partular, if $Y_{t\\wedge \\tau}$ is a uniformly integrable martingale, then $Y_\\sigma = \\E[Y_\\tau | \\F_\\sigma]$. In other words, up to the technical condition (i.e., the uniform integrability), the martingality holds even for stopping times. \n", + "\n", + "\n", + "#### Definition (Uniform integrability)\n", + "A family of random variables $\\{X_\\alpha\\}_{\\alpha\\in A}$, where $A$ is an index set, is called *uniformly integrable* if it satisfies\n", + "\n", + "$$\n", + " \\lim_{M \\to \\infty} \\sup_{\\alpha \\in A}\\Eof{|X_\\alpha|;|X_\\alpha| \\geq M} = 0\n", + "$$\n", + "\n", + "Typical examples are\n", + "\n", + "* Unformly bounded family of random variables, i.e., there exists an $M > 0$ such that $|X_\\alpha| \\leq M$, for all $\\alpha \\in A$.\n", + "\n", + "\n", + "* $X_\\alpha$'s are dominated by an integrable random variable, i.e., there exists a random variable $Y \\in L^1$ such that $|X_\\alpha| \\leq Y$ almost surely for all $\\alpha \\in A$." + ] + } + ], + "metadata": { + "celltoolbar": "Slideshow", + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.6.1" + } + }, + "nbformat": 4, + "nbformat_minor": 1 +} diff --git a/bb walk mouthopen state1.png b/bb walk mouthopen state1.png new file mode 100644 index 0000000..f813995 Binary files /dev/null and b/bb walk mouthopen state1.png differ diff --git a/bb walk mouthopen state2.png b/bb walk mouthopen state2.png new file mode 100644 index 0000000..6fab212 Binary files /dev/null and b/bb walk mouthopen state2.png differ diff --git a/bb walk mouthopen state3.png b/bb walk mouthopen state3.png new file mode 100644 index 0000000..199e69a Binary files /dev/null and b/bb walk mouthopen state3.png differ diff --git a/bb walk mouthshut state1.png b/bb walk mouthshut state1.png new file mode 100644 index 0000000..182131a Binary files /dev/null and b/bb walk mouthshut state1.png differ diff --git a/bb walk mouthshut state2.png b/bb walk mouthshut state2.png new file mode 100644 index 0000000..64ffb7b Binary files /dev/null and b/bb walk mouthshut state2.png differ diff --git a/bb walk mouthshut state3.png b/bb walk mouthshut state3.png new file mode 100644 index 0000000..7141871 Binary files /dev/null and b/bb walk mouthshut state3.png differ diff --git a/black cuttain with dots on the left.png b/black cuttain with dots on the left.png new file mode 100644 index 0000000..c7c8bb8 Binary files /dev/null and b/black cuttain with dots on the left.png differ diff --git a/block 1st floor middle.png b/block 1st floor middle.png new file mode 100644 index 0000000..88e40f0 Binary files /dev/null and b/block 1st floor middle.png differ diff --git a/block 1st floor right.png b/block 1st floor right.png new file mode 100644 index 0000000..0c7889d Binary files /dev/null and b/block 1st floor right.png differ diff --git a/block 1st floor with stone middle.png b/block 1st floor with stone middle.png new file mode 100644 index 0000000..dc0bae5 Binary files /dev/null and b/block 1st floor with stone middle.png differ diff --git a/block 1st floor yellow middle.png b/block 1st floor yellow middle.png new file mode 100644 index 0000000..aa80360 Binary files /dev/null and b/block 1st floor yellow middle.png differ diff --git a/block 1st floor yellow2 middle.png b/block 1st floor yellow2 middle.png new file mode 100644 index 0000000..0330db7 Binary files /dev/null and b/block 1st floor yellow2 middle.png differ diff --git a/block 2nd floor left.png b/block 2nd floor left.png new file mode 100644 index 0000000..cb39e72 Binary files /dev/null and b/block 2nd floor left.png differ diff --git a/block 2nd floor middle.png b/block 2nd floor middle.png new file mode 100644 index 0000000..e092a0b Binary files /dev/null and b/block 2nd floor middle.png differ diff --git a/block 2nd floor right.png b/block 2nd floor right.png new file mode 100644 index 0000000..d4053bd Binary files /dev/null and b/block 2nd floor right.png differ diff --git a/block brick 2nd floor middle.png b/block brick 2nd floor middle.png new file mode 100644 index 0000000..c9d8f3e Binary files /dev/null and b/block brick 2nd floor middle.png differ diff --git a/block green rock 2nd floor left.png b/block green rock 2nd floor left.png new file mode 100644 index 0000000..3e0ebdc Binary files /dev/null and b/block green rock 2nd floor left.png differ diff --git a/block green rock 2nd floor middle.png b/block green rock 2nd floor middle.png new file mode 100644 index 0000000..8c9688c Binary files /dev/null and b/block green rock 2nd floor middle.png differ diff --git a/block rock 2nd floor middle.png b/block rock 2nd floor middle.png new file mode 100644 index 0000000..2130f6f Binary files /dev/null and b/block rock 2nd floor middle.png differ diff --git a/button.png b/button.png new file mode 100644 index 0000000..43d5f65 Binary files /dev/null and b/button.png differ diff --git a/button.psb b/button.psb new file mode 100644 index 0000000..ff350f7 Binary files /dev/null and b/button.psb differ diff --git a/decoration candyhouse.png b/decoration candyhouse.png new file mode 100644 index 0000000..154fb46 Binary files /dev/null and b/decoration candyhouse.png differ diff --git a/decoration candyhouseonfire.png b/decoration candyhouseonfire.png new file mode 100644 index 0000000..5841dc5 Binary files /dev/null and b/decoration candyhouseonfire.png differ diff --git a/decoration circushouse.png b/decoration circushouse.png new file mode 100644 index 0000000..a592c37 Binary files /dev/null and b/decoration circushouse.png differ diff --git a/decoration flower.png b/decoration flower.png new file mode 100644 index 0000000..2527f6d Binary files /dev/null and b/decoration flower.png differ diff --git a/decoration flower2.png b/decoration flower2.png new file mode 100644 index 0000000..5943656 Binary files /dev/null and b/decoration flower2.png differ diff --git a/decoration fruithouse.png b/decoration fruithouse.png new file mode 100644 index 0000000..2f207eb Binary files /dev/null and b/decoration fruithouse.png differ diff --git a/decoration fruittree.png b/decoration fruittree.png new file mode 100644 index 0000000..63c83bb Binary files /dev/null and b/decoration fruittree.png differ diff --git a/decoration lamplight.png b/decoration lamplight.png new file mode 100644 index 0000000..ca1ee33 Binary files /dev/null and b/decoration lamplight.png differ diff --git a/decoration pavillion.png b/decoration pavillion.png new file mode 100644 index 0000000..6d2a8db Binary files /dev/null and b/decoration pavillion.png differ diff --git a/decoration sakura.png b/decoration sakura.png new file mode 100644 index 0000000..674e374 Binary files /dev/null and b/decoration sakura.png differ diff --git a/decoration stupid car.png b/decoration stupid car.png new file mode 100644 index 0000000..2be3438 Binary files /dev/null and b/decoration stupid car.png differ diff --git a/decoration tent.png b/decoration tent.png new file mode 100644 index 0000000..9a0d7f5 Binary files /dev/null and b/decoration tent.png differ diff --git a/decoration transfergate.png b/decoration transfergate.png new file mode 100644 index 0000000..27bb0ef Binary files /dev/null and b/decoration transfergate.png differ diff --git a/decoration tree.png b/decoration tree.png new file mode 100644 index 0000000..12a44af Binary files /dev/null and b/decoration tree.png differ diff --git a/decoration willow.png b/decoration willow.png new file mode 100644 index 0000000..82becf9 Binary files /dev/null and b/decoration willow.png differ diff --git a/dialog.png b/dialog.png new file mode 100644 index 0000000..26d3606 Binary files /dev/null and b/dialog.png differ diff --git a/game.cpp b/game.cpp index 3551c0a..811e2c0 100644 --- a/game.cpp +++ b/game.cpp @@ -45,8 +45,9 @@ void Game::displaymainmeneu() scene->addItem(titletext); //create the walking bean - Player *bean= new Player(); + bean= new Player(); scene->addItem(bean); + bean->moveautomatically(); //create the buttons @@ -57,7 +58,7 @@ void Game::displaymainmeneu() TextItem *text1= new TextItem(QString("START"),board); text1->setPos(150/2-text1->boundingRect().width()/2,20); - connect(text1,SIGNAL(clicked()),this,SLOT(vedeo1())); + connect(text1,SIGNAL(clicked()),this,SLOT(conversation1())); TextItem *text2= new TextItem(QString("LOAD GAMES"),board); @@ -78,4 +79,29 @@ void Game::displaymainmeneu() scene->addItem(board); +} + +void Game::conversation1() +{ + //qDebug("function conversation1 is deployed"); + scene->clear(); + QGraphicsPixmapItem *block[32]; + for(int i=0;i<32;i++){ + block[i]= new QGraphicsPixmapItem(); + block[i]->setPixmap(QPixmap(":/pictures/resoureces/pictures/block brick 2nd floor middle.png")); + block[i]->setPos(25*i,570); + scene->addItem(block[i]); + } + bean= new Player(); + scene->addItem(bean); + bean->setPixmap(QPixmap::fromImage(bean->walkpix[0])); + bean->setPos(100,570-120); + bean->setFlag(QGraphicsItem::ItemIsFocusable); + bean->setFocus(); + + + + + + } diff --git a/game.h b/game.h index b4da66e..e89185f 100644 --- a/game.h +++ b/game.h @@ -4,13 +4,23 @@ #include #include #include "player.h" +#include class Game:public QGraphicsView{ - //Q_OBJECT + Q_OBJECT + public: //public member functions Game(QWidget *parent=NULL); void displaymainmeneu(); + + //public attributes: + Player *bean; + +public slots: + void conversation1(); + + private: //public attributes QGraphicsScene *scene; diff --git a/player.cpp b/player.cpp index af6555c..1e08b1a 100644 --- a/player.cpp +++ b/player.cpp @@ -1,6 +1,9 @@ #include "player.h" #include #include +#include +#include +#include Player::Player(QGraphicsItem *parent) @@ -20,29 +23,55 @@ Player::Player(QGraphicsItem *parent) //initialize walkstate=0; + isjumping=0; } void Player::moveright() { - walkstate++; - walkstate%=3; - /*if(walkstate==0){ - walkstate+=(rand()%2)*3; - }*/ - setPixmap(QPixmap::fromImage(walkpix[walkstate])); - setPos(x()+5,y()); + QTimer *timer1= new QTimer(); + QTimer *timer3= new QTimer(); + QTimer *timer2= new QTimer(); + timer1->setSingleShot(true); + walkstate=1; + connect(timer1,SIGNAL(timeout()),this,SLOT(walkright())); + timer1->start(20); + + + + timer2->setSingleShot(true); + connect(timer2,SIGNAL(timeout()),this,SLOT(walkright())); + timer2->start(40); + + + + timer3->setSingleShot(true); + connect(timer3,SIGNAL(timeout()),this,SLOT(walkright())); + timer3->start(60); + + + + } void Player::moveleft() { - walkstate++; - walkstate%=3; - /*if(walkstate==0){ - walkstate+=(rand()%2)*3; - }*/ - walkstate+=6; - setPixmap(QPixmap::fromImage(walkpix[walkstate])); - setPos(x()-5,y()); + + QTimer *timer1= new QTimer(); + QTimer *timer3= new QTimer(); + QTimer *timer2= new QTimer(); + timer1->setSingleShot(true); + walkstate=7; + connect(timer1,SIGNAL(timeout()),this,SLOT(walkleft())); + timer1->start(20); + + timer2->setSingleShot(true); + connect(timer2,SIGNAL(timeout()),this,SLOT(walkleft())); + timer2->start(40); + + timer3->setSingleShot(true); + connect(timer3,SIGNAL(timeout()),this,SLOT(walkleft())); + timer3->start(60); + } @@ -54,9 +83,56 @@ void Player::moveautomatically() direction=1; QTimer *timer= new QTimer(); connect(timer,SIGNAL(timeout()),this,SLOT(walkauto())); - timer->start(50); + timer->start(70); } +void Player::jump() +{ + isjumping=1; + yspeed=20; + //delete jumptimer; + jumptimer= new QTimer(); + connect(jumptimer,SIGNAL(timeout()),this,SLOT(jumpup())); + jumptimer->start(10); +} + +void Player::keyPressEvent(QKeyEvent *event) +{ + + if (event->key()==Qt::Key_Left){ + if(pos().x()>0) + moveleft(); + } + if (event->key()==Qt::Key_Right){ + if (pos().x()<800-100) + moveright(); + } + if (event->key()==Qt::Key_Space){ + if(isjumping==0){ + jump();} + } + /*if (event->key()==Qt::Key_Down){ + if(pos().y()<600-100) + setPos(x(),y()+10); + } + if (event->key()==Qt::Key_Space){ + bullet *bull=new bullet(); + bull->setPos(x()+25,y()); + scene()->addItem(bull); + if (bulletsound->state()==QMediaPlayer::PlayingState){ + bulletsound->setPosition(0); + + } + if (bulletsound->state()==QMediaPlayer::StoppedState){ + bulletsound->play(); + } + + + }*/ + +} + + void Player::walkauto() { if(direction==1 && x()<=800-138-60){ @@ -79,3 +155,38 @@ void Player::walkauto() } } + +void Player::walkright() +{ + setPixmap(QPixmap::fromImage(walkpix[walkstate])); + setPos(x()+5+isjumping*10,y()); + walkstate++; + walkstate%=3; +} + +void Player::walkleft() +{ + + setPixmap(QPixmap::fromImage(walkpix[walkstate])); + setPos(x()-5-isjumping*10,y()); + walkstate++; + walkstate%=3; + walkstate+=6; +} + +void Player::jumpup() +{ + //qDebug("jumpup function is deployed"); + + if(y()<=570-120){ + setPos(x(),y()-yspeed*0.8); + yspeed-=0.8; + } + else{ + setPos(x(),570-120); + disconnect(jumptimer,SIGNAL(timeout()),this,SLOT(jumpup())); + delete jumptimer; + yspeed=0; + isjumping=0; + } +} diff --git a/player.h b/player.h index f772e8c..ddc4ca1 100644 --- a/player.h +++ b/player.h @@ -3,22 +3,37 @@ #include #include +#include class Player:public QObject, public QGraphicsPixmapItem{ Q_OBJECT public: - //constructor + //constructor Player(QGraphicsItem *parent=0); - //public member function + //public member function + //walk and jump void moveright(); void moveleft(); + void setwalkstate(int w){walkstate=w;} void moveautomatically(); + + void jump(); + + void keyPressEvent(QKeyEvent *event); + + //public attributes + QImage walkpix[12]; public slots: void walkauto(); + void walkright(); + void walkleft(); + void jumpup(); private: int direction; - QImage walkpix[12]; int walkstate; + float yspeed; + int isjumping; + QTimer *jumptimer; }; #endif // PLAYER_H diff --git a/res.qrc b/res.qrc index 0708ca0..779217e 100644 --- a/res.qrc +++ b/res.qrc @@ -13,5 +13,7 @@ resoureces/pictures/decoration circushouse.png resoureces/pictures/decoration fruithouse.png resoureces/pictures/decoration pavillion.png + resoureces/pictures/black cuttain with dots on the left.png + resoureces/pictures/block brick 2nd floor middle.png diff --git a/scheme left.png b/scheme left.png new file mode 100644 index 0000000..252d6a5 Binary files /dev/null and b/scheme left.png differ diff --git a/scheme middle.png b/scheme middle.png new file mode 100644 index 0000000..6bcb81b Binary files /dev/null and b/scheme middle.png differ diff --git a/stair middlepart.png b/stair middlepart.png new file mode 100644 index 0000000..b0424aa Binary files /dev/null and b/stair middlepart.png differ diff --git a/stair upperend.png b/stair upperend.png new file mode 100644 index 0000000..5f8f931 Binary files /dev/null and b/stair upperend.png differ diff --git a/theadventureoftheblackbean.pro.user b/theadventureoftheblackbean.pro.user index 49f3f3f..7f97d26 100644 --- a/theadventureoftheblackbean.pro.user +++ b/theadventureoftheblackbean.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId