Wednesday, August 22, 2012

Fit those spectra!!!

Ignoring the continuum normalization (that part of my code still has issues... more on that later) and giving my "data" spectra (yellow) a doppler shift of 15 km/s, SNR (signal-to-noise ration) of 3000, and a line broadening of 0.1:

Before any fitting:

So, what I do in the fitting is I transform the model as if it had been broadened and shifted by some given amount, then evaluate the "likelihood" (this process is the Bayesian methodology that allows minimizing chi squared as a fitting mechanism...) at those values. Then, the Levenburg-Marquardt algorithm finds the highest likelihood value, which should be the correct values of doppler shift and line broadening.

After cross-correlation (to find an initial guess) and Levenburg-Marquardt:
Let's zoom in on that fit:

 What??? I can't see the data, the values for the transformed model are practically the same!! The fit is too good! Where is it? Zoom in further to see the slight discrepancy due to noise:

Fitting algorithm estimates a doppler shift of 14.9999878 (I expected 15) and a line broadening of 0.0999971716 (with no prior information!! I started the line broadening at 0, and expected to get 0.1).

Not bad!

Wednesday, August 8, 2012

Analogous Inspiration

 When I was a young kid (4-10), I wanted to be an astronaut. Then, suddenly, my ‘dream’ changed – I started telling people that I wanted to be a lawyer. I always thought that the reason for this change was that my dad always said that being a lawyer was a good career, but going through old papers while I was at home made me realize maybe this wasn’t the whole story.

I found a “newspaper” that I had written in fourth grade. Our class had a variety of student-run “businesses”: some people made crafts out of paper and traded them for rocks others collected on the playground. To be a part of this economy, I wrote a sporadic “newspaper,” proudly typed up in Microsoft Publisher and printed on long paper (to be more authentic), which I traded to get my share of rocks and paper bracelets. The teacher tried to stop us, as we used a lot of classroom supplies, but, I digress…

One of these “newspaper” “issues” contained a story about the space shuttle Challenger and its 1986 explosion. Reading it now, I am brought back memories of how shocked I was when I discovered that this had happened, that seven people had died. Even though I was reading about it fourteen years after the fact, I felt as if those people had just died. With my father away in Afghanistan, I was profoundly struck by the mortality of these people I saw as heroes (no doubt there was a fearful analogy in my fourth-grade mind to my father, who I also saw as my hero).

I am reminded of this shred of my past by the resoundingly successful landing of Curiosity on Mars, an emotional event I watched while observing at Palomar Observatory. Early in the evening on the day of the scheduled landing I excitedly texted my uncle, whose ten-year-old son has repeatedly told me that he wants to go to Caltech someday, to let him know about the landing.

After the message was sent, however, I worried, perhaps irrationally, about what would happen if Curiosity were to crash to the surface in a fiery mess instead of land as it was supposed to. Would my cousin lose faith in Caltech? NASA? Science? I remembered all too clearly my own first impression of science’s imperfections, and I was worried that were Curiosity to fail, it would have some momentous effect on my cousin’s life plans.

My fears were unfounded. Curiosity landed on the surface of Mars safely, and as I watched the jubilant celebration of the involved scientists and engineers on a laptop, in the data room of the Hale 200-inch, I imagined my cousin watching this celebration and how much this success would inspire him. I imagined him tracking the progress of the MSL over the next weeks, months, or even years as I voraciously consumed everything I could about Voyager when I learned about it as a child. I was fascinated with the ideal that somewhere, something we humans had made was doing science we couldn’t do ourselves. As I traced my fingers along the a map of our solar system with Voyager’s trajectory overlaid, so I hope will my cousin will someday have a map of Mars along which to trace a similar path.

Curiosity is inspirational. Now, I don’t need an inspiration to spur me towards science, having found my own way, but the awe Curiosity inspires might lead many children to follow it into a field of science.

There is also something else that I have realized in the ten years since I wrote that sad article about Challenger. There can be no progress without risk. Those imperfections that manifest as disaster are not failure, but merely poorly aimed steps forward.

Wednesday, August 1, 2012

Coding (more) Efficiently in Python! Part 1: prun

Since it’s been a while, I think I will write about how to make your python code more efficient! (Also, writing this up means I can stop being stuck on my project for a few minutes… hence, thus I posit my ulterior motive…)

Having never had formal training in coding (barring a high school course, in which we spent several months on ‘Hello World’…. So, no formal training), I am what I term a ‘functional coder.’ I can write code. But that’s about all. It might run and get the job done, but when you look at the actual raw code you will see recursive for loops and a stark lack of commenting, variable names like ‘bunny’ and ‘widget’ (hey, it seemed cute at the time! Who knew I would need to come back to this code later?) and almost every other mark of a rookie you can think of. This summer, this has begun to haunt me; I need to run little chunks of code thousands of times as I approach my correct parameters in a Markov Chain Monte Carlo (this process I am also trying to make more efficient, but, more on that later).

I started out knowing very little about making code efficient. Well, I did realize in Ay117 (the astrostats course I took this past spring) that my five-deep for loops could cause laughter when shown to others. I don’t want to be a comedian, though: I want to be a competent coder!

Tim (a graduate student in Professor Johnson’s group) showed me a couple cool tricks earlier this summer in making code more efficient, all of which I have been using ever since! I will show here my personal favorite diagnostic tool, prun.

prun is a function that is part of the ‘core.magic’ module (so, no need to import anything! It’s super easy!). You call is by simply writing, either on your command line on in a developing environment:

%prun functionname

When functionname is the function you want to test. There are also some options that you can insert as follows, when 'o' is the identifier of your option: documentation of prun.

This is what you might see when running prun from the command line.

For me, since I will be calling this function many, many times, I want to try and eliminate whatever is taking the most time. I see that the total time for this function is 1.584 seconds, which seems pretty good, right? Only two seconds? You can wait for two seconds for your function to run, right?

That is what I thought at first, too. 1.5 seconds seems fine at first glance, but the nature of my project is such that I will need to run this particular method hundreds or even thousands of times. Suddenly, a couple seconds isn’t sounding so good anymore… luckily, prun separates out for me which functions are taking the most time to run, and how many times they are being called. By looking at this chart generated by prun, I can see that the functions taking the most time are the interpolation and shift_lhood, I function I wrote. There are two easy ways to fix that! Firstly, you might notice that the interpolation it being called 11 times. By examining the code, I notice that the same function is being interpolated 11 times. Silly me, I hadn’t realized that I did that… well, that’s an easy fix!

The second thing I can do to improve the efficiency of this function is to improve my shift_likelihood function. The old version took, as according to the profile shown above, 0.121 seconds PER CALL to run (see the fourth row, fifth column). Here is the improved version of that function, shift_lhood, called on its own. To improve it, I tried using matrices instead of for loops to do element by element manipulations. Now it only takes 0.093 seconds! What an improvement!

prun is a useful function, and it’s my first step on the long journey to becoming a better coder (and not spending days and unneeded days running code)!

Thursday, June 21, 2012

I miss my running buddies!!

Without Clara and Stephanie (off at Berkeley and in Florida, respectively) I've been running on my own a lot recently... and when I say "recently" I mean "in the last three days." The loneliness is getting to me already!!

Fun run today, I thought it would be 6 miles, but apparently it was not. It was pretty safe, didn't run through too many sketchy areas (I think Orange Grove is a pretty road).

See the route I ran! East Orange Grove on MapMyRun.

I also started my SURF this week, with a (luckily) very direct application of the methods I learned last semester in Ay 117! So far, I have been writing code that generates a mock noisy spectra of a B-Star (which is basically a very big star!), then playing with this spectra and attempting to extract from it the shift I put it. This part I've done.

Now, the hard part - I need to write code to extract a doppler shift. This is tricky because a doppler shift or a given magnitude has different effects on different parts of the spectrum. If you don't know what a doppler shift is, imagine a firetruck, sirens blazing, passing you as you stand on the sidewalk. As the truck moves toward you, you will hear a shrill siren, which then becomes deeper after the truck passes you. The reason that this modulation occurs is that the sound waves are experiencing a doppler shift. When the truck is moving towards you, each successive sound wave is being emitted at a decreasing distance from you, meaning that this later wave has a "head start" on the earlier one. The frequency of sound waves reaching your ear is thus higher than the frequency at which they are emitted. The opposite is true if the truck is moving away from you - each successive wave has to travel even farther than the one before it.

The same thing happens with light. If a source emitting photons is moving away from us, then the distance between each photon is greater as time goes on. The effect of a doppler shift on the observed wavelength is given in this relationship:

Which is dependent on the wavelength, represented by lambda. At higher wavelengths, there will be more space between successive wave fronts. This is why we cannot say that with a doppler shift, a 10 nm wave becomes a 12 nm wave and a 16 nm wave becomes a 18 nm wave. the 16 nm wave will actually be larger than 18 nm, since as the wave itself is longer, the shift must also be bigger.

To get this working, I need to tackle interpolating functions. AH! Wish me luck!

Monday, January 16, 2012

Fun With Kepler!

What is Kepler?

The easiest way to imagine what Kepler is doing is to think of it as staring at the sky, taking measurements at how much light is being received from different spots of the sky at different times. Kepler is focused on one part of the sky, observing continuously, measuring for changes in the flux of light reaching the telescope.

What is Kepler looking for?

Kepler’s primary goal is to find planets. Specifically, Kepler’s aim is to identify planets and planet candidates that will help scientists understand the types of and distribution of planets in the universe. In order to find these planets, Kepler uses the transit method, with measures dips in light as a planet crosses a distant star.

What do these fluctuations in light mean?

Kepler is looking for stars that have a dip in luminosities, because this could indicate a planet transit. For a planet to ‘transit’ a star means that the planet is crossing in between us (the observers) and the star. One example of a transit closer to home is Venus. When Venus transited the sun several years ago, you might remember pictures of the transit showed a dark circle on the sun. That was the planet blocked part of the sun’s light from reaching earth.

When a planet transits a distant star, it decreases the amount of light reaching observers here. So, when Kepler measures a temporary decrease in light from a star, it could mean that a planet is transiting across the star.

Problems with the transit method

Although the transit method is conceptually simple, there are many problems that accompany it. For example, not every dim in light indicates a planet. Other possible causes for the dimming include a transiting binary star, three-star system, or two stars blending their light together. So, if Kepler detects a dip in light from a source, it COULD be, but is not necessarily, a planet. This is why the “planets” Kepler identifies are called “planet candidates.” Scientists have estimates the “false positive” rate – that is, how many sources that are not actually planets are accidentally identified as such by the Kepler mission[i]. This rate, although small, is not insignificant, thus the differentiation between confirmed planets and planet candidates.

In an ideal world...

As the name “direct imaging” implies, the best way to identify an exoplanet is to actually see it. There are two main reasons that this is hard to do, however:

High contrast ratio between luminous sun and planet (which reflects only a very small amount of light)

The atmospheric distortion blends light from distinct sources into what appears to be one source

Adaptive Optics can really help!

This is where adaptive optics comes in! Adaptive optics counteracts the atmospheric distortion. As a result, photons are received in a pattern closer to that by which they were emitted. If there were a dark spot on the star (say… caused by a transiting planet!) then ideally, that spot would also be received by the mirror. Without adaptive optics, it is very difficult if not impossible to see this spot.

Adaptive optics also allow the resolving of very bright stars and the planets next to them. Adaptive optics can measure sources within one arcsecond of each other, even if one of the soruces (a planet!) is 10 million times less luminous than the other (a star!)[ii].

Adaptive optics is really the most reliable way to confirm a planet candidate is actually a planet (or to show that it is NOT a planet).



Monday, January 9, 2012

First Ay 21 class

Since I am waiting for code to run anyway, I thought I would look up some topics from the first Ay 21 lecture that were mentioned but that I wasn’t extremely familiar with/ didn’t know what they were, and briefly summarize what I glean from a quick, superficial review.

String theory

I’ve always thought of string theory as being some crazy theory, but never actually looked up what it was. String theory posits that the universe is made up of one-dimensional vibrating strings, which serve as the most basic particle. The theory also requires extra dimensions – I am very interested in how a theory could require extra dimensions, and how these dimensions are defined. String theory is an attempt to reconcile general relativity and quantum mechanics, which is why it was mentioned with respect to the Age of Quantum Gravity (that bit of time at the beginging of the universe when GR and QM were both important, and you couldn’t describe the physics of a discrete event with only one of the two theories.


M-theory is an extension of string theory. There are different ‘types’ of string theory, and M-theory is a unifying theory meant to connect them all.

Curvature of the Universe

It is hard to imagine space itself being curved. Best I can understand it, the exact curvature of the universe is as of yet an unsolved problem. The concepts of curvature, density, and expansion of the universe are all realted: there is a critical density at which expansion would stop, and similarly the density also deteremines whether the universe is curved like a sphere, like a hyperbola, or not at all. In a curved universe, light would bend as it travels.

Carrol and Ostlie has a section on curved spacetime (it beings on page 1185). This sections says that the curvature must be constant throughout the universe for a given point in time, although the curvature may vary as the time elapsed since the big bang changes. Curvature is a time-dependant function.

Dark Energy

In class, dark energy was mentioned as being (possibly) one of the “extra” components of mass in the universe, holding 73% of the mass. Dark energy is something that Einstein predicted when he developed his cosmological costant. Dark energy, different than dark matter, exists throughout the universe, although we are unsure what form it may take. It could be a constant energy density (the cosmological constant) or it could exist in fields or some other form. Dark energy also contributes to the expansion of the universe.

Weak/Strong Forces

There are four fundamental kinds of forces in the universe: weak, strong, gravity, and electromagnetic. Unlike gravity and EM forces, the effects of which dwindle but do not disappear as distance increases, weak and strong forces only act on a atomically small scale.

The strong force is what holds a nucleas together, even as the positively charged protons attempt to repell each other. I can’t believe I never heard about this before! When the protons and neutrons are considered to be composed of quarks, the strong force can be attributed to the “color force” between the quarks. The weak force allows one “flavor” of quark to change to another, allowing nuclear fusion to take place, and is thus necessary for every star that burns.


This is another subject I have read about briefly but never really considered until now. I had thought that quarks were as “imaginary” as string theory; however, this is not true. Quarks are the elementary particles of the universe in that the 6 flaovrs of quarks make up the protons, neutrons, mesons, and all other particles which then, successively, make up matter itself. Mesons are particles composed of two quarks, and baryons are particles composed of three quarks. Protons and neutrons are thus mesons (constructed from up and down quarks). Quarks exist in the flavors up, down, strange, charm, bottom, and top.

My code is still running… the more I learn about physics, the more I realize I don’t know! I am super-excited about quarks – I need to go check out a book on elementary particle physics from the library because they seem SUPER COOL and I want to learn more about them!


Wikipedia, Carrol & Ostlie

Sunday, January 8, 2012

Why is AO imaging important?

In the last post, I included a cool picture showing the difference in imaging resolution due to AO. AO is very useful in direct imaging, because the higher resolution allows better differentiation between different sources. One complication that has always been as issue in detecting exoplanets accurately is that even if observers measure a light curve indicating that there may be a planet, the light dip might actually be due to something else, such as a transiting binary. This is of particular importance when wide-sky surveys are used, because each individual source has probably not been studied at length; therefore, the superficial appearance of their being a planet might be accepted when it should be rejected. Even when studied at length, the false positives may appear so similar to real planets that they continue to fool observers. This paper shows one specific instance where a source appeared to have a planet (indeed, it passed many of the ‘tests’ of planethood) but was actually a false positive. The fact that there is a false positive rate in all planet-finding surveys is a problem: this leads to the difference between a planet candidate and a confirmed planet.

One good thing about the Kepler false-positive rate (the rate at which things that are not planets are being called planets) is that it is not random. There are very specific reasons as to why a light curve might be altered to appear that there was a planet where one did not exist. Some of these reasons might be an eclipsing binary, “blend” (this is when a less bright star, not cataloged in Kepler, ‘mimics’ a planet) or the existence of a “hierarchal triple” (a multiple – three in this case, as its name implies – star system) (On the Low False Positive Probabilities of Kepler Planet Candidates, Morton and Johnson). Knowing the possible reasons that there might be a false positive in the Kepler data allows a statistical correction for this. Using approximations and creating probability distributions, Morton and Johnson developed a possible model for the false positive rate. Although this will not tell which individuals stars have planets, it makes the Kepler distribution as a whole more helpful, as observers have a way to estimate how many planet candidates, on the whole, are likely planets.

The process for determining, after a planet candidate has been found, whether or not it is a real planet can be lengthy. It also shows the importance of adaptive optics in confirming planets, as AO images can be used in either preliminary or follow-up observations. Many false positives are due to light being misattributed to an incorrect source, and adaptive optics reduces the degree to which this happens.