# Transforming data to Gaussian

Transforming data to Gaussian using probability integral transform in Matlab:

n = 500;
x=exp(randn(n,1))+(randi(2,[n 1])-1).*(10+3*randn(n,1));
fhat = @(in) sum(x <= in)/n;
Fhat = @(A) arrayfun(fhat,A);
y=Fhat(x); z = icdf('normal',y,0,1);
figure; subplot(1,2,1); hist(x,20); xlabel('x');
subplot(1,2,2); hist(z,20); xlabel('z');


Results:

# Matlab: different colormaps for subplots

I often want different subplots in one Matlab figure to have different colormaps. However, colormap is a figure property, so it’s not trivial, except that it is… with these utilities:

This works for everything except colorbars: http://www.mathworks.com/matlabcentral/fileexchange/7943-freezecolors—unfreezecolors

Post-2010, Matlab refreshes colorbars with each subplot, so you’ll need this to freeze colorbars: http://www.mathworks.com/matlabcentral/fileexchange/24371-colormap-and-colorbar-utilities–feb-2014-

# Converting between decimal and unary in Matlab

Decimal to unary:

[1:4]'==3   % returns [0 0 1 0 ]

Unary to decimal:

max([0 0 1 0] .* (1:4))   % returns 3

# Confidence Intervals from Pivots

This is an example of using a pivot to find a confidence interval.

$X_1,...,X_n \sim \text{Uniform}(0,\theta).$

1. Find a pivot:

Let $Q=X_{(n)}/\theta$.

2. Find its distribution:

$P(Q \le t)= P(X_i \le t\theta)^n = t^n$.

3. Find an expression involving an upper and lower bound on the pivot:

$P(a \le Q \le b) = b^n-a^n$ This implies $P(a \le Q \le 1) = 1-a^n$.

4. Substitute the expression for the pivot from Step 1, and set the RHS to $1-\alpha$.

$P(a \le X_{(n)}/\theta \le 1)=1-a^n$

$P(1/a \ge \theta/X_{(n)} \ge 1) = 1-a^n$

$P( X_{(n)} \le \theta \le \frac{X_{(n)}}{a} ) = 1-a^n$

Let $1-\alpha = 1-a^n$. Then $a=\alpha^{1/n}$.

$P(X_{(n)} \le \theta \le \frac{X_{(n)}}{\alpha^{1/n}})=1-\alpha$

This gives us $[X_{(n)},\frac{X_{(n)}}{\alpha^{1/n}}]$ as a $1-\alpha$ CI for $\theta$.