## Continued Uniform Distributions |
## October 2nd, 2017 |

Today we are going to work on an interesting probability distribution that I spent some time thinking about recently. The simplest distribution is the uniform distribution. The uniform distribution has equal probability for all numbers in a range. For example, if you have a uniform distribution on $\{1,2,3\}$, then you have a $\frac{1}{3}$ probability of getting each number in that range. It is a little confusing to think about continuous distributions, because each number actually has a 0 percent chance of occurring. If we have a uniform distribution on $[0,5]$, then each individual point has a probability of 0 of occurring, but the probability that we get a number *near* each number is $\frac{1}{5}$. Make sense? A uniform distribution on $[0,a]$ has probability density function, denoted $f_x$, given by

\[

f_x=

\begin{cases}

\frac{1}{a} & 0\leq x\leq a \\

0 & \text{else} \\

\end{cases}

\]

**I. Our Distribution**

So, let’s say we want to generate a number $x$ from a uniform distribution on $[0,a]$, and *then* we want to generate a number $y$ from a uniform distribution on $[0,x]$. Let’s write down what we are given; we know that $x$ is uniformly distributed on $[0,a]$ and is independent of $y$

\[

f_x=

\begin{cases}

\frac{1}{a} & 0\leq x\leq a \\

0 & \text{else} \\

\end{cases}

\]

Additionally, if we are given $x$, we know what the distrubution for $y$ is — it’s uniform on $[0,x]$. We denote this as $f_{y|x}$, the probability of $y$ *given* $x$.

\[

f_{y|x}=

\begin{cases}

\frac{1}{x} & 0\leq y\leq x \\

0 & \text{else} \\

\end{cases}

\]

**II. Manipulation**

We start by finding $f_{xy}$, the probability that *both* $x$ and $y$ occur. We can use the formula

$$ f_{xy} = f_{y|x}\cdot f_{x}$$

This gives us

$$f_{xy} = \frac{1}{ax}$$

We know that $x$ will never be more than $a$ or less than 0, and that $y$ will never be more than $x$ or less than 0. So, we can properly define our probability function.

\[f_{xy}=

\begin{cases}

\frac{1}{ax} & 0\leq y\leq x \hspace{3mm}\text{and}\hspace{3mm} 0\leq x\leq a\\

0 & \text{else} \\

\end{cases}

\]

This area is shown in the picture below

To find $f_y$, we need to integrate x to remove it from the equation. If we wanted to integrate over the entire region, we would do

$$\int_0^a\int_y^a f_{xy}\hspace{2mm}\text{d}x\text{d}y \hspace{6mm}\text{or}\hspace{6mm}\int_0^a\int_0^x f_{xy}\hspace{2mm}\text{d}y\text{d}x$$

Since we want to eliminate $x$, we need $x$ to be the interior variable of integration. This will ensure that we have no $x$ term in the integrand for the exterior integral. Therefore, to eliminate $x$, we will use

$$f_y = \int_y^a f_{xy}\hspace{2mm}\text{d}x$$

$$ = \frac{1}{a}\int_y^a \frac{1}{x}\hspace{2mm}\text{d}x$$

$$ = \frac{1}{a}\ln{x}\hspace{2mm}\Big|_{x=y}^{x=a}$$

$$ = \frac{1}{a}(\ln{a}-\ln{y})$$

$$ = \frac{1}{a}\ln{\frac{a}{y}}$$

$$ \bbox[15px,border:1px solid black]{f_y = \frac{\ln\frac{a}{y}}{a} \hspace{7mm} 0\leq y\leq a}$$

## Laser Harp : Hackathon 2017 |
## May 18th, 2017 |

For the second time, I served as a mentor for my high school/middle school’s annual Hackathon. Despite being extremely busy helping out with the incredible projects the students were building, I found time and was able to work with my friend Andrew to complete a Laser Harp. Here is a video demonstrating our harp

https://drive.google.com/open?id=0B2rOmItJ08iScVBTZVJEVVhoX0E

In this post, I will attempt to explain the basics of how this project was built. Hopefully this project inspires others and sparks some interest in both Electronic Instruments and Arduino as a whole.

– Arduino Mega

– (8) 5V lasers

– (8) photoresistors

– (8) 3k$\Omega$ resistors

We used the Arduino Mega because we needed 8 analog inputs and the Arduino Uno only has 6. We needed our lasers, of course, and the photoresistors and 3k resistors were be used to detect the breaking of the laser beam.

To sense how much light was hitting the photoresistor, we used a potential divider, shown below

To find the ideal value for the resistor, we first looked at the datasheet for the photoresistors. When dark, the resistance is around 10k$\Omega$, and when exposed to natural light, the resistance is approximately 1k$\Omega$. With $R_{pr}$ being the resistance of the photoresistor, the voltage across a potential divider is given as

$$V_o = V_i \cdot \frac{R_{pr}}{R_{pr} + R}$$

We wanted to maximize the difference between $V_o$ when the beam was shining and $V_o$ when the beam was broken.

$$V_{dark} – V_{light} = V_i \cdot \frac{R_{dark}}{R_{dark} + R}-V_i \cdot \frac{R_{light}}{R_{light} + R}$$

$$\approx V_i \cdot \frac{10000}{10000+ R}-V_i \cdot \frac{1000}{1000 + R}$$

Graphing this function as a function of R, we see that the largest difference occurs around $R=3000$. By using this value, we maximized the difference between our two readings, minimizing the chance that we would make an error.

Unfortunately, we didn’t take pictures while actually building, so we have to skip the construction step. It was pretty easy, though, as it’s pretty much just 4 pieces of wood screwed together in a rectangle. For the code, we used a program called Max/MSP. It’s a visual programming language that is extremely well documented and has very nice MIDI interfacing. MIDI, which stands for Musical Instrument Digital Interface, is the globally accepted protocol for electronic instruments (instruments that don’t actually have an acoustic chamber, essentially). We used the Arduino to read in analog data, and then used a serial port to relay that data into Max. Here is a look at the Max patch we wrote

It looks really complicated. Well, actually, *it IS pretty complicated*, but I’m going to ignore all the boring stuff, which eliminates some of the more complicated things. I’m going to focus mostly on two parts; the note generation section and the scale selector.

Let’s go piece by piece here and analyze what is happening. The user selects a starting note (C is MIDI note 60, C# is 61,… B is 71), and a scale pattern. It sets the starting note as $n1$, the note of the first string on our harp. That’s the left side of the picture. On the right, we have the scale setter. It uses the pattern for the scale shape selected (a major scale is 2212221). As it cycles through, the router object connects the next note to the correct string, going up by one for each note. By adding the interval width to the previous note, we build the scale note by note. At the bottom we see the notes for C Major, the default scale.

This is the subpatch that actually takes an analog signal reading and decides what to do. It takes the analog value (the 1 with the blue triangle below it at the top of the image) and puts it into two if statements. If it goes above the threshold, it triggers the note, but it uses a onebang object, which only sends 1 “trigger” message at a time. Once the signal goes below the threshold (i.e you unblock the laser), the note off is sent, and the note on onebang is reopened. This prevents the user from sending tons and tons of notes when only plucking the “string” once. The pack objects just insert the correct note number into a message that is then sent to the MIDI output.

This project was pretty awesome. It worked on the first go, which is always nice. In addition, I got to work with a bunch of awesome students who were pretty interested in what we were cooking up with a bunch of lasers. Andrew and I were extremely proud of the harp, especially given that we only had 2 days to complete it. As always, thanks for reading!