Category Archives: probability

Cryptographic roots in Blockchain technology

The Blockchain is expected to be the revolutionary technology to take the centre stage in our society where the traditional ledger system once dominates, from bitcoin that emerged in the finance sector to fields where transactions are dependent on authenticity, be it a paper document from bank, an import / export data exchange, or even documents in judicial systems, it is important to understand the principles of its fundamental roots in cryptography.

For example, to ensure the rightful spending of currency in bitcoins, there are a lot of technology being in place on the virtual money market. One being the Elliptic Curve Cryptography that is based on mathematics to ensure the identity of parties involved in any bitcoin transactions.

blockchain1

Advertisements

Plotting gamma distribution in TI Nspire

The gamma function is not a built-in function in the TI Nspire. Nevertheless this function can easily be defined and used for visualising the gamma probability distribution function.

gdf2

gdf1

Performing Shapiro-Wilk test on TI-84

On TI-84, although the Shapiro-Wilk test is not available as built-in function, it is possible to calculate the W statistics by using the calculator’s rich set of list and statistics function. It is not trivial, but doable.

First set up a list of sample values. Using the example from the original Shapiro-Wilk paper, the samples are input into a list called W, while the N variable denotes the dimension and U stores inverse of its square root. The data sample in W must be sorted in ascending order.

shapiro84-2 shapiro84-1

Next is to generate the M list based on W. This list is to calculate the inverse normal distribution based on the index value and the dimension of W list. Doing this in TI-84 is easy with the help of the seq() function. The complete expression is:
seq(invNorm( (I-0.375) / (N+0.25), 0, 1), I, 1, N, 1)

shapiro84-3 shapiro84-4

Now that the List M is ready, it is time to derive the sum of square of it and we store it into variable m (not the list M).
shapiro84-5

Since the approximation algorithm is used instead of look up table as proposed in the original paper, the next step is to prepare List A. This list is arranged in such a way that the first two and last two elements have different calculation than all the others. The last two values (N-1, N) of List A are prepared in the Y5 and Y6 equations. These are approximating equations. The first two elements are the negation of these two in a particular order.
shapiro84-6 shapiro84-7

One more variable, ε, has to be calculated at this point before we generate List A.
(m - 2*M(N)² - 2*M(N-1)²) / (1 - 2*Y6² - 2*Y5²)
shapiro84-8

Next is to generate the List A. As explained, four elements namely the first, second, N-1, and N elements of this list is calculated unlike all other elements. The screen below is for all except that four special elements.
shapiro84-9

The remaining steps for the four elements of this List A are then carried out as below.
shapiro84-10

Finally, all three lists are ready for calculation.
shapiro84-11

To calculate the W statistics, use the Linear Regression function in the TI-84 as below for W and A. The correlation coefficient r² is the W statistics as shown in R. See the next installment for the Z statistic and P value calculations from this W statistic, also on the TI-84.

shapiro84-14 shapiro84-18

Confidence Interval for Odds Ratio calculation in TI Nspire

In comparing the relative odds of an outcome, odds ratio is often used and can be easily calculated after constructing the frequency table as in below where the row-wise values represent the treatment and column-wise as the outcome.
oddsratio-ci1

Even though there is no built-in function for the calculation for the confidence interval of odds ratio in TI Nspire, doing it in the Calculator page is pretty straight forward.

oddsratio-ci2

Constructing matrix in TI Nspire for Markov chain with Poisson distribution

In calculation for Markov chain, matrix setup can be complicated evn with the user-friendly interface on the TI Nspire. The built-in function like identity(), constructMat() etc are useful on specific cases.

For Markov chain calculation, the model setup in terms of matrix sometimes get quite complicated and demanded a few exceptions from the built-in function mentioned above that are geared towards common linear algebra.

The example below attempts to simply the construction of the Markov model by use of piece-wise functions in building matrix. The problem to solve in Markov process is on a stock resupply problem, where an imaginary shop keeps stock of 1 to 3, and replenish on each day only when all stock is exhausted. Poisson distribution is assumed on the demand side.

Firstly a function called markov_prob is created, which takes the demand as variable and return the corresponding Poisson distribution. Note that how the function handle probability for demand greater than 3. A short-hand function mkp() is created afterwards to ease reading in the matrix.
markovstock1

When the function is defined, the Markov model can be formulated in a new 3 x 3 matrix created using the template. It is sometimes not easy to define state transition function as in this example problem. Therefore, the shorthand function just created will help in filling in the states as matrix elements. For example, P11 means the stock next day (Sx+1) equals stock today (Sx) which is 1, and that means the demand is zero, and thus mkp(0). There are some curve ball like P12, where Sx+1 > Sx which is not possible, since that means the shop has more stock the next day for any day with non-zero stock (i.e. negative demand), and thus the probability is zero.markovstock2

After setting up the Markov model, the equilibrium distribution can be deduced. The below is one of the methods using Nelder-Mead algorithm.
markovstock3

Based on this model, more figures can be deduced for decision making on fine tuning stock keeping strategy.

Using Nelder-Mead to solve for Markov chain stationary distribution

Taking the same problem from previous installments, another approach using the Nelder-Mead algorithm is tried and successfully obtained the same answer. After formulating the equation for passing to the Nelder-Mead program in TI Nspire, which required the same algebraic manipulation as in the previous linsolve() approach, another more intuitive formula is tried. The second one directly calculates matrix in the minimization equation and therefore render the algebraic manipulation unnecessary.

The first setting is as below.
markov_stationarydist_NM1

While the second setting that directly calculates on the matrix is shown below.
markov_stationarydist_NM2

Finding stationary distribution in Markov chain with TI Nspire

For Markov chain calculation with a transition matrix P:
markov_stationarydist1

attempt to find the stationary distribution using TI Nspire solve function apparently is not appropriate:
markov_stationarydist2

But no worry, the question can be solved using the linsolve function because this problem can be broken down into a series of linear equations by the properties of stationary distribution of Markov chain. In TI Nspire, a pop-up menu style wizard will guide through the linear solver:
markov_stationarydist3

When finished setting up the template, the equations can be entered directly in the Calculator screen.markov_stationarydist4

A shortcut to this in the TI Nspire can be referenced here as a one-liner making use of matrices operations.