Information and discussion about graphics, figures & tables in LaTeX documents.
knutk
Posts: 2 Joined: Fri Feb 26, 2010 5:01 pm
Post
by knutk » Fri Feb 26, 2010 5:15 pm
Hi,
Im trying to draw a binomial tree with latex and the tikz package, I found an example and have tried to modify it to my needs, but haven't been successful. I have 2 problems;
1. I want the tree to be recombining, such that the arrow going up from B, and down from C, ends up in the same node, namely E.
2. I also want the arrows in the tree to have the same slope/angel.
Attached is a figure and the code I have used. Appreciate any help!
Code: Select all
% Set the overall layout of the tree
\tikzstyle{level 1}=[level distance=4cm, sibling distance=3.5cm,->]
\tikzstyle{level 2}=[level distance=4cm, sibling distance=2cm,->]
% Define styles for bags and leafs
\tikzstyle{bag} = [text width=2em, text centered]
\tikzstyle{end} = []
% The sloped option gives rotated edge labels. Personally
% I find sloped labels a bit difficult to read. Remove the sloped options
% to get horizontal labels.
\begin{tikzpicture}[grow=right, sloped]
\node[bag] {$\$ A$}
child {
node[bag] {B}
child {
node[end, label=right:
{D}] {}
edge from parent
node[above] {}
node[below] {$(1-p)^2$}
}
child {
node[end, label=right:
{E}] {}
edge from parent
node[above] {}
node[below] {$(1-p)p$}
}
edge from parent
node[above] {}
node[below] {$(1-p)$}
}
child {
node[bag] {C}
child {
node[end, label=right:
{E}] {}
edge from parent
node[above] {$(1-p)p$}
node[below] {}
}
child {
node[end, label=right:
{F}] {}
edge from parent
node[above] {$P^2$}
node[below] {}
}
edge from parent
node[above] {$P$}
node[below] {}
};
\end{tikzpicture}
Attachments
Option lattice.png (18.17 KiB) Viewed 31524 times
frabjous
Posts: 2064 Joined: Fri Mar 06, 2009 12:20 am
Post
by frabjous » Fri Feb 26, 2010 7:38 pm
What you describe I think goes against the expected structure of trees. I'd just do it without using a tree structure.
Here's it with the locations hard-coded. With some study of the tikz manual, I'm sure it could be done with relative locations instead. Too lazy to figure it out myself.
Code: Select all
\documentclass{article}
\usepackage{tikz}
\begin{document}
% Define styles for bags and leafs
\tikzstyle{bag} = [text width=2em, text centered]
\tikzstyle{end} = []
\begin{tikzpicture}[sloped]
\node (a) at ( 0,0) [bag] {$\$ A$};
\node (b) at ( 4,-1.5) [bag] {B};
\node (c) at ( 4,1.5) [bag] {C};
\node (d) at ( 8,-3) [bag] {D};
\node (e) at ( 8,0) [bag] {E};
\node (f) at ( 8,3) [bag] {F};
\draw [->] (a) to node [below] {$(1-p)$} (b);
\draw [->] (a) to node [above] {$P$} (c);
\draw [->] (c) to node [below] {$P^2$} (f);
\draw [->] (c) to node [above] {$(1-p)p$} (e);
\draw [->] (b) to node [below] {$(1-p)p$} (e);
\draw [->] (b) to node [above] {$(1-p)^2$} (d);
\end{tikzpicture}
\end{document}
lattice.png (7.48 KiB) Viewed 31517 times
localghost
Site Moderator
Posts: 9201 Joined: Fri Feb 02, 2007 12:06 pm
Post
by localghost » Fri Feb 26, 2010 8:30 pm
There is an alternative solution using the »matrix« library of the
pgf/tikZ package.
Code: Select all
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=1cm,
]
{
& & F \\
& C & \\
\$A & & E \\
& B & \\
& & D \\
};
\draw[->] (tree-3-1) -- (tree-2-2) node [midway,above] {$P$};
\draw[->] (tree-3-1) -- (tree-4-2) node [midway,below] {$(1-p)$};
\draw[->] (tree-2-2) -- (tree-1-3) node [midway,above] {$P^2$};
\draw[->] (tree-2-2) -- (tree-3-3) node [midway,below] {$(1-p)p$};
\draw[->] (tree-4-2) -- (tree-3-3) node [midway,above] {$(1-p)p$};
\draw[->] (tree-4-2) -- (tree-5-3) node [midway,below] {$(1-p)^2$};
\end{tikzpicture}
\end{document}
Best regards and welcome to the board
Thorsten
knutk
Posts: 2 Joined: Fri Feb 26, 2010 5:01 pm
Post
by knutk » Sun Feb 28, 2010 4:31 pm
Thank you both for the help. I went with the matrix solution.
Best
casperyc
Posts: 50 Joined: Thu Oct 15, 2009 11:23 pm
Post
by casperyc » Thu Mar 18, 2010 11:36 pm
Hi there,
Is there a way to input 'eqnarray*' in that?
I have tried to use
Code: Select all
\begin{tikzpicture}[sloped]
\node (a) at ( 0,0) [bag] {
\begin{eqnarray*}
c_0 &=& 2.72610 \\
\o_0 &=& 0.32588 \\
\phi_0 &=& -10.30910
\end{eqnarray*}
};
\node (b) at ( 4,-1.5) [bag] {49.5764};
\node (c) at ( 4,1.5) [bag] {32.4358};
\node (d) at ( 8,-3) [bag] {61.44549};
\node (e) at ( 8,0) [bag] {40.20051};
\node (f) at ( 8,3) [bag] {26.30105};
\draw [->] (a) to (b);
\draw [->] (a) to (c);
\draw [->] (c) to (e);
\draw [->] (c) to (f);
\draw [->] (b) to (d);
\draw [->] (b) to (e);
\end{tikzpicture}
My aim is to draw the 'tree' in the attachment
Attachments
Pages from MATH3022Solns5.jpg (32.52 KiB) Viewed 31411 times
localghost
Site Moderator
Posts: 9201 Joined: Fri Feb 02, 2007 12:06 pm
Post
by localghost » Thu Mar 18, 2010 11:53 pm
Please do not hijack threads that are marked as solved with the green checkmark (see Board Rules). Next time please open a new topic and add a link to the related one.
At first you should learn why not to use the
eqnarray environment any more [1]. At second you should know that a displayed math environment can't work. So try to insert an in-line equation in conjunction with the
aligned environment from the
amsmath package.
Code: Select all
\(
\begin{aligned}
c_0 &= 2.72610 \\
\phi_0 &= 0.32588 \\
\psi_0 &= -10.30910
\end{aligned}
\)
Your code sample won't work because you haven't declared the node style
bag as
frabjous did in his example.
[1]
The PracTeX Journal - TeX Users Group | Lars Madsen: Avoid eqnarray!
Best regards
Thorsten
feanor22
Posts: 32 Joined: Mon Oct 13, 2008 3:01 pm
Post
by feanor22 » Sun Feb 16, 2014 8:44 am
Hi
I have tried all examples and most of them worked, but the matrix approach didn't work...
Any ideas why??
Thanks
Johannes_B
Site Moderator
Posts: 4182 Joined: Thu Nov 01, 2012 4:08 pm
Post
by Johannes_B » Sun Feb 16, 2014 1:42 pm
Would you be able to post a minimal example, so we don't have to read this very old thread once again? Opening a new thread might also be a good idea (and linking here).
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
feanor22
Posts: 32 Joined: Mon Oct 13, 2008 3:01 pm
Post
by feanor22 » Thu Jul 31, 2014 8:03 am
Dear All
I have tried the examples on this page in a article class file but if I try to include the code in a beamer slide I get 100 errors.
Any ideas why?
Thanks in advance.
Code: Select all
\documentclass[xcolor=pdflatex,dvipsnames,table]{beamer}
\usepackage{tikz}
\usetikzlibrary{matrix}
\mode<presentation>
\usetheme{JuanLesPins}
\setbeamercovered{transparent=30}
\title[Investment Science ]{ Investment Science \\ Introduction to Binomial Trees }
\author[ Anonymous]
{ Investment Science}
\institute{ Blah University }
\date{today}
\begin{document}
\frame{\maketitle}
\begin{frame}
\begin{tikzpicture}[>=stealth,sloped]
\matrix (tree) [%
matrix of nodes,
minimum size=1cm,
column sep=3.5cm,
row sep=1cm,
]
{
& & F \\
& C & \\
\$A & & E \\
& B & \\
& & D \\
};
\draw[->] (tree-3-1) -- (tree-2-2) node [midway,above] {$p$};
\draw[->] (tree-3-1) -- (tree-4-2) node [midway,below] {$(1-p)$};
\draw[->] (tree-2-2) -- (tree-1-3) node [midway,above] {$p^2$};
\draw[->] (tree-2-2) -- (tree-3-3) node [midway,below] {$(1-p)p$};
\draw[->] (tree-4-2) -- (tree-3-3) node [midway,above] {$(1-p)p$};
\draw[->] (tree-4-2) -- (tree-5-3) node [midway,below] {$(1-p)^2$};
\end{tikzpicture}
\end{frame}
\end{document}
Last edited by
Stefan Kottwitz on Thu Jul 31, 2014 12:29 pm, edited 1 time in total.
Johannes_B
Site Moderator
Posts: 4182 Joined: Thu Nov 01, 2012 4:08 pm
Post
by Johannes_B » Thu Jul 31, 2014 10:15 am
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.