Monthly Archives: January 2015

Estimating GARCH parameters from S&P500 index data in Nspire

Yes this could be done easily even in Excel today with a modern PC, so why abuse a calculator that is probably running on 3.3V? But dare I ask why not. Calculators today, especially the top of the line products like the Nspire, are almost on par in terms of computing power with the PC back in the days when I am in college. It is almost a fun in itself to program these little machines for calculations so complex that owners of early generation calculators back then can never dream of.


One of the usage of GARCH in finance is the pricing of options. The gist of this model is that returns follow normal distribution with volatility dependent on time. Like most models, good parameters are crucial to the success in the model’s capability, and GARCH is no exception. To try out how well Nspire will do the parameter estimation that required computation technique including processing arrays of numerical data, logarithmic, and also optimization, a list of continuous daily S&P500 index figures are loaded into the Spreadsheet application in the Nspire, and the continuous compounded returns are then calculated in the next column, by the formula

Rt = LN(St/St-1)

where st denotes the price of the underlying asset at time t.

There are 1000 data points in this test set but Nspire seems to handle it decently in the Spreadsheet application, with the help from the “Fill” function that works exactly like in Excel, so that the row index will be worked out automatically (t and t-1 correspond to the row index). My only complaint is that unlike in Excel, there is no provision in the Nspire similar to the “Shift+End+Arrow” key feature that instantly jump to the last data row with selection.

The simplest form of GARCH, GARCH(1,1), is targeted in this test for parameter estimation. Three parameters in the model, namely alpha, beta, and omega, are to be estimated using the maximum likelihood method which is a popular technique. In this particular case, i.e. when p=1 and q=1, the GARCH model can be represented by

σt+12 = ω + α • rt2 + β • σt2

The coefficients alpha and beta exhibits the measurement of persistence of volatility. The more close to 1 for their sum, the more persistent the volatility is; while the reverse (towards zero) means a quicker reversal to the long term variance. A function in Nspire is written to do this calculation which takes the list from the log returns of the 1000 S&P data stored in the Spreadsheet application as variable log_return, and the actual maximization (for likelihood) of this function in Nspire is done via the Nelder-Mead algorithm, using the following kernel distribution function with the constants (2π) removed for calculation speed yet preserving the property of proportional to the maximum likelihood.

Σ (- ln(σt2 - rt2 / σt2), t=1..n)


It took the real Nspire more than two hours to finish the computation. I think I am going back to Excel 😉 Nevertheless, doing all these again from scratch on the Nspire is really a good refresher on how these algorithms, models and formulae work.

MTR City Saver RFID tickets

Just out of curiosity, a scan of the new MTR City Saver ticket revealed it is MIFARE just like the new generation single journey ticket that replaced its magnetic predecessor. Not surprising as the resemblance of the two look very much alike, including the bar code at the back. The colored gate arrangement is possibly a cost control measure as MTR is supporting both FeliCa and MIFARE contact-less tickets.


Coordinate conversion from HK80 grid to Google Map and vice versa using Nspire

At work there is a need to deal with data from a GIS system and its own local coordinate system which is different from the usual one as in GPS and Google Map. Transformation between these two coordinate systems is possible and is well documented at this article (link). A practical application with Google Map and public CCTV images also required such conversion.

For casual usage, a handy transformation function is coded in the Nspire to convert XY values between the two systems. Since the calculation is pretty simple, the built in function script is enough for the task and as such there is no need to summon the power of lua.

web tool from the local authority was used to verify the results. Due to rounding or difference in calculation methods, a little discrepancy is observed but is well within tolerable margins.


Since we already have X,Y, why not utilize the nice color screen of the Nspire by adding a map with the result plotted on? GoogleMap API is not feasible as the Nspire is not geared for the Internet. If we can settle on something simpler, and forget zoom-in or switching to satellite view, a simple map in the background can do the trick as well. After-all, a full blown map software is not something this calculator excel at. But then, plotting the approximate location helps shed some lights on the true nature of the results from arguably its most used perspective – on the map.

Comparing bug prediction methods by logistic growth and Gompertz curve in Nspire

Analysis can be performed on a sample set of data with cumulative bug counts collected over 12 days to obtain parameters to fit in models for future prediction. Column A and B are data, with the standard Nspire logistic regression function executed on column C and D to obtain the parameters a,b,c. Column E is the function value of the logistic function but not the one built-in with Nspire, instead the parameters are obtained separately using the Nelder-Mead program from the previous post.

There are other models besides logistic regression for prediction, one being an sigmoid function called Gompertz function and is applied to the same data set to obtain the parameters for comparison with the more common logistic function. Since the parameters are obtained in a similar fashion as the logistic function, i.e. by minimizing the sum of errors, the Nelder-Mead program can be reused. After obtaining the parameters, the function values on the data set are calculated and shown in Column F.

The application of the Nelder-Mead program to obtain the parameters of the logistic regression is shown below. Firstly the logi function is declared, and the sum of squared error is declared in the numfunc_logi function which in turn will be passed to the nm function in order to obtain the minimum by the Nelder-Mead algorithm. As shown below the results are exactly the same with the Nspire built-in logistic regression function (a=64.003, b=9.0317, c=0.33644, albeit the Nspire formula named a,b,c differently).

The application of the Nelder-Mead program to obtain the parameters of the Gompertz function is similar.

The number of bugs, data fit for both functions are plotted in the below graph alongside with the logistic regression curve. Hard to tell which of the two functions is better?

Turns out there is some guess better than others. As the calculation of Ru value below shown, the Gompertz function provided a little better fit in this bug prediction case. To calculate, obtain the one-var stats from the bugs data (only the sum of squares of deviation, stat.SSX is needed), and then plug in other values accordingly. Similar to the R coefficient in regression analysis, the larger value is, the better the prediction. And in this case, 0.9248 from Gompertz outperformed 0.9107 from logistic.
growth5Eduguesstimate is what I’d call this conclusion 😉

Yubikey, Beagleboard, and Precise Pangolin

Yibico provided library for accessing the Yubikey API on linux platform, but unfortunately there is an issue with the signed char (OMAP is ARM) that will give an error message of “Server response signature was invalid (BAD_SERVER_SIGNATURE)”. A rebuild of the package from source is needed for this package to work, in this case, Ubuntu Precise Pangolin on Beagleboard XM.

A USB extension cord comes handy to connect the Yukikey. By the way, it looks like my RFID model is discontinued and being replaced by the NFC version at Yukikey.

Login with Yubikey.yubibeagle2

Barrier Breaker

Finally got a chance to upgrade the little TP-Link WR703N to Barrier Breaker which was released last October.

Standard fresh upgrade from a stock 703N at with basic authentication admin/admin.

Welcome screen displayed soon after flashing.

The recipe for Barrier Breaker:

66% storage remaining. Hmm.. extroot might not work for this release.

Upgraded interface.

Overall nothing exciting for this release so far. Look forward to Chaos Calmer.

Usage of the Nelder-Mead program for Logistic Regression in Nspire

The Nelder-Mead program from the previous post is handy to solve a couple of problems, one being finding the parameters for logistic regression. The TI Nspire comes with logistic regression function but I can’t seem to find a way to have it solve equations with more than one categorical dependent variable.  Since finding the parameters for the logistic curve required maximum likelihood, the Nelder-Mead program will be able to help.

A sample problem goes like this: Given the following daily variables (i) temperature, (ii) whether this is Wednesday, Saturday, or Sunday, the sale of a particular product is recorded. Note that “sale of a particular product” referred to a binary value. Either it is sold that day, or not. The number of sale does not matter.

Sample data:

The calculations:
nmlr2The first equation, l, defined parts of the likelihood function and is a short hand so the resulting function will be less clumsy. It is defined as piece-wise function taking the variables for day of week and temperature with their coefficients, as well as the sold or not value which govern the sign of the return value (p or 1-p). The second equation defined the maximum likelihood function (the next one is an alternative). Setting initial guess into p and plug into the nm function and the results is obtained, in the resultsmatrix. The logistic function is thus y=1/(1+e^-(2.44*x1 + 0.54*x2 – 15.20)) and ready to use for predicting values.