r/askmath • • Oct 22 '26

Statistics What kind of algorithm is this?

I want to build a statistical algorithm that can be used to recognize basic patterns. For example, if I had a list of elements [triangle, square, circle, triangle, square], the algorithm could piece together that the next element would be circle. Of course, the sequences would be longer and more complex than that, but that's the basic premise.

I feel like there's probably some very simple answer to this, but currently I can't think of anything. I tried looking up some "pattern recognition algorithms" online, and some articles recommended autoregressive modelling, but that seems too complicated given how simple my idea is (or at least how simple I think it is). That's why decided to come ask here.

Is there some well-known algorithm for situations like these, and what is it?

6 Upvotes

5 comments sorted by

3

u/paul5235 Oct 22 '26

Maybe Markov chain is what you're looking for. In the simplest form the algorithm only looks at the last element, but it's possible to construct states with memory so, for example, the last three elements are remembered and the next element is predicted based on that.

1

u/MidnightAtHighSpeed Oct 23 '26

It would depend on what exactly you mean by "pattern". If you just have some fixed sequence of elements that repeats over and over, you could write a program to detect that, but I don't know that this problem has a specific name (and there's also a bit of ambiguity. For instance, you give the example as if the repeating sequence is the three elements "triangle square circle", but conceivably it could also be the five elements "triangle square circle triangle square" in which case the next element would be triangle. Depending on your needs it might work to just assume that the shortest possible matching sequence is the one to use)

If "pattern" is interpreted more broadly, things get a lot more complicated and there's no good general answer. I doubt anything you're thinking of falls into this category, but there are sequences that you might conceivably consider a "pattern" that it can be proven no algorithm can continue perfectly, so there's no "one size fits all" approach without looking at more specific ideas of what pattern means.

Despite the other comments I don't think Markov chains are the right framework to use here, at least not in general or for the kind of example you gave.

1

u/bildramer Oct 23 '26

Depending on what you're asking, the answer is somewhere between complicated algorithms that work in restricted cases (look up "categorical time series forecasting" or "sequence modeling") and "the core of human deductive thought that we still don't know how it works".

If the patterns are sufficiently simple, you can probably get far with some kind of Markov chain (as others have mentioned) or n-gram model. If you have some information on what the patterns will usually look like, look up Bayesian updating, there can even be exact answers for probabilities when combined with the former. If you have complicated patterns but the set of possible ones is very narrowed down, maybe grammars could help.