Forums > Pricing & Modelling > Markov Chains for Price Modeling

 Page 1 of 2 Goto to page: [1], 2 Next
Display using:
 pwalk Total Posts: 4 Joined: Jun 2012
 Posted: 2012-08-04 23:34 Hi, I was wondering if anyone would be willing to throw me in the right direction for a beginner to moderate project in modeling (predicting) equity pricing within a financial market using markov chains. I've heard of Markov Chain Monte Carlo Method.I'm ideally going to do this in Python (numpy, scipy). But I know C++, maybe a better option?I am completely new to this, if it doesn't already show. Appreciate any knowledge provided.Thanks!pw
 Trev Total Posts: 31 Joined: Mar 2010
 Posted: 2012-08-05 01:13 If your intent is to predict prices, then you are best using a Kalman Filter which uses a Markovian process (note: Kalman Filter's, like a lot of estimation processes, are susceptible to noise). Google "numpy kalman filter" or "scipy kalman filter" as there looks to be some code out there.MCMC is primarily used for option pricing.Cheers.
 pwalk Total Posts: 4 Joined: Jun 2012
 Posted: 2012-08-05 02:33 Thank you Trev.
 mtsm Total Posts: 94 Joined: Dec 2010
 Posted: 2012-08-14 21:06 what??? Not sure what you are referring to Trev. Never heard of MCMC bieng used to price options.My intuitive understanding would point more into the direction of Markov Functional models. These can be approached in time-continuous and time-discrete form.
 rusti999 Total Posts: 13 Joined: Oct 2008
 Posted: 2012-08-14 21:31 Markov Chain Monte Carlo (MCMC) is primarily used to simulate from a probability distribution, the idea being that the probability distribution is the stationary distribution of the Markov chain. This is useful especially if the distribution does not have a nice analytical form, for instance, the posterior distributions in the majority of instances when you are doing complicated Bayesian analysis, and you want to get a characterization (e.g. moments) of those distributions. As far as I can tell, it doesn't have anything to do with modeling equity prices using Markov chains.
 jslade Total Posts: 751 Joined: Feb 2007
 Posted: 2012-08-15 01:46 You're probably thinking of Hidden Markov models. You can't really predict price (I mean, if you can, give me a call), but knowing when the probability of a regime switch in returns changes could be useful. http://www-stat.wharton.upenn.edu/~steele/Courses/956/Resource/HiddenMarkovModels/HMMApplications/RydenEtAl98.pdfPython has a few, though I suspect the R versions are superior. "Learning, n. The kind of ignorance distinguishing the studious."
 Nonius Founding MemberNonius Unbound Total Posts: 11347 Joined: Mar 2004
 Posted: 2012-08-16 10:57 jslade, literature recommendations on HMM/baum welch/EM for mixture of normals case? there is a zoo of papers out there, but a lot seem to be written for those who already are experts. An a=a etc moment....http://www.dinbali.com/?page_id=74
 WayconKidd Total Posts: 69 Joined: Mar 2010
 Posted: 2012-08-16 18:20 "Hidden Markov Models for Time Series: An Introduction Using R" by Zucchini and MacDonald has been pretty helpful thus far ('bout halfway through it).
 etrader12 Total Posts: 39 Joined: Jan 2010
 Posted: 2012-08-16 22:56 I would 2nd this (@WayconKidd) for regime based modelling using markov chains.http://134.76.173.220/hmm-with-r/R code and information available to review.There isn't much publicly available practical literature on the subject and practical financial applications, so much of the difficulty in understanding usefulness stems from a dearth of good (non-proprietary) examples defining input and output state factors and structure. For instance, a simple observable markov transition matrix can be used to model non IID sequence transitions (not HMM).Even though it is not financially oriented, I highly recommend Marsland, 'Machine Learning' for a very good intro with Python examples on markov chains, baum-welch, viterbi, etc.. algorithms.
 jslade Total Posts: 751 Joined: Feb 2007
 Posted: 2012-08-17 00:30 Nonius: I think the papers are all crap as tutorials, other than the classic Rabiner paper everyone uses to get the basics. That Zucchini book never got me far either. There is a slightly more helpful book whose name escapes me; it's organized along econometric lines. I'll dig up the name later if I can find it in my notes, but it is only a slight improvement over Zucchini (and it has no R examples).Screwing around with R packages, with the built in examples, and more importantly, with home-baked examples, is far more useful. The two I found useful are DepmixS4 and mhsmm. The latter is pretty oogly, but it was more useful than the other dozen-odd packages for HMM/HSMM. DepmixS4 is beautifully written, but you have to write your own predict(). "Learning, n. The kind of ignorance distinguishing the studious."
 WayconKidd Total Posts: 69 Joined: Mar 2010
 Posted: 2012-08-17 01:59 Would be interested in hearing about that book, jslade.I agree about depmixS4. I've also used the HMM R package and it is basic. Appears that the HMM functions from Python's scikit-learn are a port, of sorts, of HMM.Another R package that caught my eye recently is rEMM. Spent a few hours playing with it and there's some nifty stuff.
 jslade Total Posts: 751 Joined: Feb 2007
 Posted: 2012-08-17 04:59 I *think* it was "Hidden Markov Models in Finance" by Mamon and Elliot, but it's not presently in the local library, so I can't check. It isn't very good in any case. Just a marginal improvement on the Zucchini book (though it is some clever guy's name, I love writing the words, "Zucchini book").Checked out hsmm, HMM, rHMM, HiddenMarkov and didn't find them real helpful -crude stuff. mhsmm is crudely written, but effective. Thanks for the pointer to rEMM; it is one I managed to overlook. While it isn't a classical HMM, it seems to do stuff I care about.FWIIW, it's worth pointing out explicitly, you don't need to maximize likelihood to do HMM type things. I've used hybrid Kohonen/KNN and Kohonen/SVM to build forecasting gizmos with similar capabilities, minus the well-motivated probabilities. Quicker to implement, though harder to know what is going on sometimes. "Learning, n. The kind of ignorance distinguishing the studious."
 Nonius Founding MemberNonius Unbound Total Posts: 11347 Joined: Mar 2004
 Posted: 2012-08-18 21:56 I'm having a brainfart with all the "gentle" introductions. anyway, i downloaded jahmm library for java; maybe i'll try brute force learning with the code examples. An a=a etc moment....http://www.dinbali.com/?page_id=74
 Nonius Founding MemberNonius Unbound Total Posts: 11347 Joined: Mar 2004
 Posted: 2012-08-24 22:43 well, I calibrated a bunch of intraday data using Baum Welch. It seems interesting and I guess one of the ideas is to have the states represent regime shifts, ie, flat, up, down. Maybe I need to read more, but what seems to be hard is to nail down with high probability the hidden state that is the direction you want. at time n-1, you can infer pretty well what state you were just in, but then, unless I'm mistaken, you can only estimate state n up to transition probability. An a=a etc moment....http://www.dinbali.com/?page_id=74
 here Banned Total Posts: 98 Joined: Aug 2007
 Posted: 2012-08-25 11:48 generally speaking the markov property does not specify a distribution this is only a property. hence, if one intended to forecast a behavior random process in the future it is necessary to make more assumptions to predict switches between states. To accept stationary probably does not good property to catch dynamics. To go from diffusion model it should be clear why this class is too narrow and what actually we need to add to an extended model.
 Nonius Founding MemberNonius Unbound Total Posts: 11347 Joined: Mar 2004
 Posted: 2012-08-25 12:43 not sure i fully understand, but I'm guessing you're saying for prediction it's better to drop markov property and stationarity? An a=a etc moment....http://www.dinbali.com/?page_id=74
 here Banned Total Posts: 98 Joined: Aug 2007
 Posted: 2012-08-25 13:15 not sure i fully understand, but I'm guessing you're saying for prediction it's better to drop markov property and stationarity?------------------------------------------------------------------------------no, i am saying that markov property does not enough to present future distribution while a stationary of a markov chain might have sense for only a short period
 WayconKidd Total Posts: 69 Joined: Mar 2010
 Posted: 2012-08-25 22:33 So effectively the same problem that you'd see with any fit model (e.g. a VECM) — the model can only describe what it's "seen"? Thus one cannot assume that the model is robust with regard to the future.
 here Banned Total Posts: 98 Joined: Aug 2007
 Posted: 2012-08-27 01:03 There is no exact distribution in the market. We are assign a distribution to predict future move of the prices. Besides this when one implement the model it is common to use say daily data for example close prices. This technique suggests that unknown function parameters approximated by constants which are in turn are estimations of the average over a period of time.
 jslade Total Posts: 751 Joined: Feb 2007
 Posted: 2012-08-27 06:36 To understand HMM's, let's take a step back, and make up a time series which has a sort of Markov property; something that Markov switches between strong autocorrelation and anticorrelation. Yes, it's not the market, but there are things in the market that might act this way, and there are certainly other things which behave this way.***************************************************require(depmixS4)require(mhsmm)markov.switched.ar<-function(n,loiter=matrix(c(0.98,0.02,0.03,0.97),2,byrow=T),seed=0.1,ara=0.5,arb=-0.3,noisea=0.8,noiseb=0.5) { ## use the example from weigend & shi paper ## ar1 process with params as described above (defaults are W&S defaults) x = rep(0,n) state = markov.state(n,loiter) noise = rnorm(n) x[1] = seed for (i in 1:(n-1)){ if(state[i]==1) { x[i+1] = ara * x[i] + noisea*noise[i] } else { x[i+1] = arb * x[i] + noiseb*noise[i] } } xts(x=cbind(x,state),timeSequence(from=Sys.Date(),length.out=n))}markov.state<-function(n,mx) { ## depends on mhsmm package for now nst = nrow(mx) init=rep(1/nst,nst) sim.mc(init,mx,n)}markov.switched.ar(10000)->m.tsm.ts\$x -> observedm.ts\$state -> statelag(observed)[-1] -> laggedcbind(observed[-1],lagged) -> mydatanames(mydata)<-c("x","lagged")set.seed(1)depmix(x~1+lagged,data=mydata,nstates=2,trstart=runif(4))->modelfit(model,emc=em.control(maxit=1000,tol=1e-12,rand=FALSE)) -> fmposterior(fm) -> postmean(post\$state==state)## lookie how often I get the state rightsummary(fm)******************************************************Blammo, I am so badass, my sperm smoke unfiltered camels. I now have two fitted AR(1) models, and I know what the probability is of each forecast being relevant at a given time step. I even have a decent estimate of the noise distribution; hot damn. Try that with your vector auto-regression gizmo.Yeah, you could probably jury rig something like this with a moving window, and that's how I'd figure out if this is productive in the first place, but you can see how HMM's can be useful, just as a Kalman AR(1) model could be useful. You can also see how some more general ML technique would get such a forecast right, but it would probably be aided by using something like a HMM to segment the TS. Advantage of the HMM is you have an actual model. The disadvantage is ... you need to have an idea of what the model is. Exercise for the reader to use fancier noise or switching probabilities which are closer to 0.5 or nonlinear models or whatever. I've done some of this; the results are interesting, and to my knowledge, unpublished. "Learning, n. The kind of ignorance distinguishing the studious."
 here Banned Total Posts: 98 Joined: Aug 2007
 Posted: 2012-08-27 12:18 Is there a test to verify that statistical data or historical data in Finance hold Markov property? or this mathematical assumption?
 pj Total Posts: 2726 Joined: Jun 2004
 Posted: 2012-08-27 19:53 Since any stochastic process may be lookedat as a Markov process (if you take the whole history into account or define itnon time homogeneous) IMHO you need a stricter definition.For example, one can test "rigorously"whether is a Lévy process.A more general test? Good question.I imagine one could work out something for testing whether a chain is Markov. вакансия "Программист Психологической службы" -але! у нас ошибко! не работает бля-бля-бля -вы хотите об этом поговорить?
 astar Total Posts: 163 Joined: Mar 2007
 Posted: 2012-08-28 18:13 pj, wouldn't you need some sort of finite-dimensionality to get the useful properties such as the ergodic theorem?Perhaps the ergodic theorem is not really needed for this sort of pricing applications?
 Nonius Founding MemberNonius Unbound Total Posts: 11347 Joined: Mar 2004
 Posted: 2012-09-09 13:48 apropos,was looking at ≈Modeling Asset Prices for Algorithmic and High Frequency Trading on equation 9a, in the terms in the summation in the denominator inside the Ln, should that really be a +1 or should the +1 be in the subscript, ie, shifting to the next arrival. haven't gone through the BW algo deeply, so, was just wondering. An a=a etc moment....http://www.dinbali.com/?page_id=74
 Nonius Founding MemberNonius Unbound Total Posts: 11347 Joined: Mar 2004
 Posted: 2012-09-15 11:15 jslade, thanks for your post there; I'm a little late in reading it. An a=a etc moment....http://www.dinbali.com/?page_id=74
 Page 1 of 2 Goto to page: [1], 2 Next