# random computation

## Friday, September 4, 2020

## Friday, August 21, 2020

### Weight visualization of 0-layer autoencoder on MNIST

## Thursday, June 18, 2020

### What do neural nets do on noise inputs?

__Intro__

__Results__

## Wednesday, October 9, 2019

### A brief explanation of Heap's algorithm

# A brief explanation of Heap's algorithm

Heap's algorithm (named after its inventor and not the data structure) generates all

## CASE 1

The following is the pattern of swaps generated by Heap's algorithm for N=6.

```
(1 5)
(1 6)
(1 5)
(2 6)
(1 5)
(3 6)
(1 5)
(4 6)
(1 5)
(5 6)
(1 5)
```

- Consider the first 3 swaps. These only involve elements 1, 5, 6. (Equivalent to (1)(5 6)).
- Consider the final 3 swaps. These only involve elements 1, 5, 6. (Equivalent to (1 6)(5)).
- Consider the middle swaps (all swaps not yet considered, from (2 6) to (4 6)). For these, the right column swaps will never interact with the left column swaps because they never have an element in common. The right column swaps will form a cycle and the left column will fix the 1 and 5. (1)(2 3 4 6)(5).
- In total we have:

```
(1)(5 6) (1)(2 3 4 6)(5) (1 6)(5)
```

Direct calculation reveals this reduces to a cycle:

```
(1 6 5 2 3 4)
```

- The general pattern for (even) N can be seen as:

```
(1)(N-1 N) (1)(2 3 4 5 ... N-2 N)(N-1) (1 N)(N-1)
```

which always reduces to

```
(1 N N-1 2 3 4 5 ... N-2)
```

in particular, a full cycle.

## CASE 2

The following is the pattern of swaps generated by Heap's algorithm for N=5:

```
(1 4 3 2)
(1 5)
(1 4 3 2)
(1 5)
(1 4 3 2)
(1 5)
(1 4 3 2)
(1 5)
(1 4 3 2)
```

Direct calculation reveals this reduces to

```
(1 5)(2)(3)(4)
```

This is the general case for all (odd) N:

```
(1 N)(2)(3)...(N-1)
```

In other words only the first and last elements swap and the rest of the elements are fixed.

## All together

From the point of view of an arbitrary index location K, Heap's algorithm alternates a prefix action with a specified sequence of pair swaps depending on the prefix action.

If the prefix action cycles the prefix as in case 2 above then the pair swaps are the (1 K), (1 K), ...,(1 K) sequence. This results in the prefix that includes K doing the head-tail swap.

If the prefix action is the head-tail swap then the pair swaps are (1 K), (2 K), (3 K),...,(K-1 K) as in case 1 above. This results in the prefix that includes K doing a full cycle.

We have to keep track of an

## Tuesday, April 16, 2019

### Uniformly sampling large binary integers with an upper bound

## Problem

##
Naive rejection has worst case exponential $E_{cost}$

## Prefix matching and early rejection

*Prefix matching*. We will perform rejection sampling restricted to the smallest subtree that contains all of the in-range leaves (instead of sampling from the entire tree). We will refer to this smallest containing subtree as the$SCS$ . Note that there are$D$ different$SCS$ s and that each$U$ is contained under one of them.*Early rejection*. We abort a tree descent as soon as the last$0,1$ sample places us in a subtree with no legal leaves rather than continuing for the full length$D$ .

*modified sampling*.

##
Under modified sampling, $p_{rej}\over p_{acc}$ achieves maximum at $U=100...0$

##
Under modified sampling, $E_{waste}$ achieves maximum at $U=100...0$

- For all
$U$ that correspond to the same$SCS$ each additional$U_d=0$ contributes an additional positive term in$(4)$ and thus increases$E_{waste}(U)$ . Therefore the$U$ that maximizes$E_{waste}(U)$ for a given$SCS$ is the$U$ with all possible elements$0$ . - Across
$SCS$ of different heights$E_{waste}(U)$ is maximized by the$SCS$ that is the full binary tree for$D$ . This is because the max$U$ for smaller$SCS$ simply leave out terms from$(4)$ above.