Stochastic Knitting

12K16714

Intro: Stochastic Knitting

While working on a sweater for most of November, I was struck with an idea: What would stochastic knitting look like? That is, what would it look like if you randomly determined the state of each stitch depending on a probability distribution? I was specifically interested in a linear gradient, the chance of each stitch being a knit or a purl based on which row it is in. Start with all knits at the bottom, and as you get higher, more and more stitches are purls until the top row is 100% purl. I really like playing with texture, and I thought it might look pretty cool. So after finishing the sweater, I started playing.

The first question was how to generate the pattern? I could have worked out a system using dice, but that sounds really cumbersome. You'd have to do something like roll 2d10 for each and every stitch, comparing that value to a threshold which was determined by the row count. Being a programmer, I naturally decided that was something best left to a computer. That way I could add a visualization system, to get an idea what the distribution would look like, letting me tweak parameters endlessly before even casting on.

STEP 1: The Pattern Generator

The next question was what distribution to use? Easiest would be to just linearly vary the probability from top to bottom, but that wouldn't allow for much customization. I decided to use the cumulative distribution function of the normal distribution instead. This has the nice property of being sigmoidal -- it starts off flat, gets steeper in the middle, and then levels off again. Just how flat and how steep depend on the standard deviation (sigma), which means that if you want a tight, narrow transition band from state A to state B, you can get it. You can also get a wide, slow transition if you want it. Perfect!

After a bit of work I had my stochastic pattern generator up and publicly accessibly. It lets you set sigma, mean, row and stitch counts, stitch size for the visualization, and the color of the two types of stitches. It also provides an instruction generator, telling you how many stitches of which color to knit next.

Note that the instructions are randomly generated every time you click next, so what you'll knit isn't actually what is shown in the visualization. In that sense, there is no actual pattern being generated. Both the visualization and whatever you end up knitting are both just samples from the probability distribution. There is a redraw button which will resample the visualization, so you can get a sense of what it is likely to look like.

STEP 2: First Experiments

The first thing I did was knit a 20x20 swatch transitioning from knits to purls. I thought the change in texture would be really interesting. It turns out... not so much. It just looks like garbage. Oh well.

Undeterred, I tried it again with color, using some spare yarn left over after the sweater. Much better this time! I really liked the effect, but I thought it would be better with higher contrast, less variegated colors. So I grabbed some new skeins on the way home one night and tried again.

Success! This was finally looking like what I had imagined. The only problem was the width of the strip of medium grey in the middle. This happened because I simply knit one 20x20 transition on top of another, first from black to medium grey, then another from medium grey to light grey. Since the color in each section gets (roughly speaking) 1/2 of the area, the medium grey in the middle ended up with 2/4 or 50% of the entire surface. Obviously I needed to be able to vary the mean of the distribution, so I could push the transition zone up or down. This way when doing multiple transitions stacked on top of each other like this, the first could be centered 66% of the way up, and the second one at 33%, and the final areas would be more or less equal!

STEP 3: A Stochastic Hat

With that fix in, there was only one thing left to do: knit something real using this technique. I had too many other projects going on to commit to anything big, so I picked out a simple hat pattern and set to work. (This was my first colorwork project, and I'm not going to show you the inside of the hat. It's kind of a horror show, thought it did get better as I went along.)

Using the pattern generator is fairly easy. It tells you how many stitches of a color to knit. After doing this, you hit 'next', and it tells you how many of the next. Since it isn't following a pre-existing pattern, but randomly determining this on the fly, it makes for a kind of fun surprise to see what comes out. If you get lost, no problem! You can set the row and stitch counter manually, and it will start generating from that point. Since each stitch is randomly determined anyway, it doesn't matter!

I did have to have my laptop out while knitting, to keep generating the instructions, which was a bit cumbersome. I've been doing some Android smartwatch development recently, so maybe I'll create a version for that. But it wasn't too bad. It might actually be less distracting than most patterns, because you don't really have to keep track of where you are. Just follow the immediate instructions, and let the program do the tracking for you.

Most importantly, I'm very happy with the result! The pattern it generates is really interesting. I'd really like to see it done over a larger area. Play around with the pattern generator using 100x100 grids with the stitches shrunk down so you can see it all. I think a sweater made this way would look absolutely stunning. Not that I'll have the time to work on that for months now...

Let me know if you use the generator. Send pics! I'd love to see them.

12 Comments

This is absolutely amazing! I love the randomness of the output. Something that I will try on a hat. Thanks for sharing.

Very cool. I love gradient yarns. I thought there had to be a way to do gradient knitting!

Brilliant! Another way to graduate the colours is to knit with 2 or 3 strands of yarn, and change one at a time to fade the colours. But I love the 'digital' look of the stochastic method :-)

This is super awesome. I am totally going to use your generator (when I finish my list of current projects). In the mean time I shall be putting my thinking cap on to work out what to use the pattern for....

Thumbs up for the stuff.... Great ..

I love the hat but I'm a new bee not sure how beginner could do this. But awesome for you great job!
Wow! Well done. Creative idea + programming + hands-on making = a wonderful project. I agree, a sweater in this pattern would be killer

I don't knit, but I thought this was really cool! I like the way you explain your thought process at each step (like a good programmer) and the results look very good.

As a knitter, I think this is totally cool! I love the random seeming pattern.

That is a really cool visualization! It makes it really easy to see how the probability transitions from mostly "A" to mostly "B", but not with any kind of pattern.

The integral (CDF) of the Gaussian is erf(x), called the "Error function." It is also referred to as a sigmoid. The Gaussian width (sigma) transforms into the slope of the sigmoid at the origin (the half-height), which is the parameter you're tuning to get the transition you want.

Besides probabilities, it's also used with neural networks (specifically, feed-forward/back-propagation learning) where it is the non-linear summing which transforms the multiple inputs to a neuron into an output.

I'd like to point out that your original formulation, transitioning between knits and purls, only looks like "garbage" to a non-physicist. For the rest of us, the probability distribution is obvious, and pretty cool :-)