Tuesday, 4 April 2017

Collatz Conjecture - a study.

God does not care about our mathematical difficulties. He integrates empirically. - Einstein.
In Mathematics, we often come across Conjectures. A conjecture is a conclusion or proposition based on incomplete information, for which no proof has been found[1].
Collatz Conjecture is named after Lothar Collatz [2]. The conjecture is also known as the conjecture.
Now, let us see how it works, it is, in fact very simple, no , no , no etc.
For any given positive integer ,

If we keep repeating this process,
We use the Half Or Triple Plus One acronymed to .
So, for any given positive number, say , we do the following steps:
15 is odd 15 3+1=4646 is even
23 is odd 233+1=703510653160
80402010516842
You get the idea now. In this blogpost, I will explore various aspects of the Collatz Conjecture. A loose structure of the blog is going to be,
  1. Collatz for Negative numbers.
  2. Collatz for Fractions.
  3. Collatz for Irrational Numbers.
  4. Collatz for Prime numbers and pattern recognition (if any) in the number of steps it needs for different scenarios to reach to one.
  5. What is the possible approach for the proof of collatz conjecture?
  6. Difficult and beautiful.


1.

I don’t understand why do they have the condition, in the conjecture. Let’s look at an example and see what happens when Collatz conjecture is applied to number, when .

For, , let us do the collatz conjecture,
-5 is odd -5 3+1 -14 is even -7 is odd -20 is even -10 is even -5 is odd -5 3+1 -14 is even -7 is odd -20 is even -10 is even -5

So, there it is.
If we use the Collatz conjecture to negative numbers, we get oscillating values and they even eventually set to the starting number itself.

Modified Collatz Conjecture:


These are called “Cycles” that exist for negative numbers and probably also for positive numbers. There is no form of proof, that says that there is absolutely no positive number that gives rise to a similar situation.

2.

For this section, we define something called Number-decimals.
Although, we already know that Even, Odd or Prime are only defined for whole numbers.
: Even-Number-decimal are defined as terminating fractions, say that are converted to decimals, say . Then we remove the decimal point from the number to get .
(If you find this definition to be strange, please comment below and mail me with a possible modification)

Now, if , then is an Even-Number-decimal and if , then then is an Odd-Number-decimal
So, it is pretty clear that the conjecture behaves the same in this scenario.

For example, take , which when converted to decimals comes as . Now, we run the algorithm on to get 1. Similarly for other fractions as well.


In the above approach, we considered fractions as decimal numbers, that is only how we can use the Collatz Conjecture on fractions; that too only if the decimal is terminating in nature. Fractions such as (), , etc. cannot be used in the Collatz conjecture for fractions.

3.

Collatz conjecture for irrational numbers is same as saying that we apply Collatz Conjecture to non-terminating decimals.
Just like , as mentioned above cannot work for Collatz; irrational numbers won’t work too.
For revision purpose, an irrational number is defined as a number that cannot be determined as the ratio of two integers. For illustration, , however, , no exact way of saying this.

4.

Collatz for primes will behave the same, that is my guess. Let’s have a look at it.


Let’s take all the primes from , namely, .

We are sure that all of them will reach eventually. I want to analyze that, in how many steps do they reach and if there is any correlation.

For this, I wrote a small script in R. The code is as follows:
collatz_numbers <- function(n, list_col=c()) {
  if(n==1) return(c(list_col, 1));
  collatz(ifelse(n%%2==0, n/2, 3*n +1), c(list_col, n))} 

We shall define the number of steps taken by the number to reach as .

So,
collatz_number(2)=1 ( = 1)
collatz_number(3)=3, 10, 5, 16, 8, 4, 2, 1 ( = 8)
collatz_number(5)=5, 16, 8, 4, 2, 1( = 6)
collatz_number(7)=7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1( = 16)
collatz_number(11) =11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1 ( = 15)
collatz_number(13)=13, 40, 20, 10, 5, 16, 8, 4, 2, 1 ( = 10)
collatz_number(17)=17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1( = 13)
collatz_number(19)=19, 58, 29, 88, 44, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1( = 21)
If we plot the numbers versus the number of steps it took to get to , we get the following plot,

Which is an increasing series, as per the Wikipedia article[1], have a look at this [3] to have a more clear idea.
The picture below also agrees to our finding,

One interesting observation is how HUGE numbers appear in the sequence. For that, I thoughtlessly try numbers out on my function,
collatz_number(20) = 20 10 5 16 8 4 2 1.
collatz_number(21)=21 64 32 16 8 4 2 1
collatz_number(22)=22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
collatz_number(23)=23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
collatz_number(24)=24 12 6 3 10 5 16 8 4 2 1
collatz_number(25)=25 76 38 19 58 29 88 44 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1
collatz_number(26)=26 13 40 20 10 5 16 8 4 2 1
collatz_number(27)=27 82 41 124 62 31 94 47 142 71 214 107 322 161 484 242 121 364 182 91 274 137 412 206 103 310 155 466 233 700 350 175 526 263 790 395 1186 593 1780 890 445 1336 668 334 167 502 251 754 377 1132 566 283 850 425 1276 638 319 958 479 1438 719 2158 1079 3238 1619 4858 2429 7288 3644 1822 911 2734 1367 4102 2051 6154 3077 9232 4616 2308 1154 577 1732 866 433 1300 650 325 976 488 244 122 61 184 92 46 23 70 35 106 53 160 80 40 20 10 5 16 8 4 2 1
collatz_number(28)=28 14 7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1

All the largest numbers in the sequence are made bold. We can clearly see their chaotic nature, but there is never really any chaos, we just don’t look from a broad enough perspective.


5.

I have failed pathetically, at finding a proper proof for the the conjecture. However, I have some points that could be the lines on which someone could work for proving the conjecture. I will mention them in bullet points,
  • The first task is to show that for all positive numbers, there is not a single number that gives rise to a cycle (to know what is a “cycle”, read section 1).
  • Understanding the behavior of steps with increasing or decreasing numbers.
  • Understanding that is usually the universal way of converting any number into an Even number, so I guess, he played the CLEVER trick there.
  • Looking at even and odd as and and finding if they follow a particular pattern. Further, we can convert these Binary strings to Hexadecimal strings and see if there is a possibility of a Collatz conjecture in Alphabetical domain. [Original idea by Pragyaditya Das].

6.

Collatz conjecture is very nice, if you haven’t seen it already, let me point it out to you.

No matter how many time we do and to a system; if we semi-periodically keep pulling from the system; it will eventually lead to .

Collatz conjecture is the simplest mathematical open-problem available; you can explain all your non-math, non-science or non-engineering friends about it; hell! they might even give a noob try to prove it even.


Cheers! with a sad end…

Thursday, 30 March 2017

Bernoulli Numbers - Explanation

Augusta Ada King-Noel, Countess of Lovelace (10 December 1815 – 27 November 1852) was an English mathematician and writer, chiefly known for her work on Charles Babbage’s proposed mechanical general-purpose computer, the Analytical Engine. [1].
She is widely regarded as the first computer programmer. She wrote an algorithm to calculate the Bernoulli numbers, for more please visit the previous blog here.
So, I thought to study and analyze Bernoulli numbers.

Approach 1 :


Let us begin,
Definition: The Bernoulli numbers are defined as the co-efficients of the power series of the expansion of . For , we define so that,



Let us apply some mathematical rigor into it and see what happens.
We have,

Now, we know the McLaurin Series is,

Let, , to find its McLaurin coefficients we must evaluate for all =

By substitution, the McLaurin Series expansion of is,


Using in , we get,

Let’s see which coefficient has the in the expanded right-hand part of this equation for some (we expect this coefficient to be a zero). may appear if from the first sum multiplies the from the second one for some . Thus,

for any . Thus

Now for we have from

and thus



Approach 2 :


We know,

and so on…

Then, how about,

Mathematicians have always been fascinated with such classic general formulae. So was JohannFaulhaber.
Let,
Define the following exponential generating function with (initially) indeterminate

We find



This is an entire function in so that can be taken to be any complex number.
We next recall the exponential generating function for the Bernoulli polynomials

where denotes the Bernoulli number (with the convention ). We obtain the Faulhaber formula by expanding the generating function as follows:

Solving it is again very complex, so, finally we get,

Note that, , odd ; that is why Faulhaber defines .

Verification:

Let us consider the following values,



We put, , and and see that = .

Hence, Proved.

Wednesday, 29 March 2017

A Byronic Hero, Harry Potter and Bernoulli Numbers.

George Gordon Byron (22-01-1788 to 19-4-1824), is commonly known as Lord Byron, was a British Poet, a Politician and one of the brightest figures of the Romantic Movement. He is very easily regarded as one of the greatest British Poets of all times.
Considered to be the first modern-style celebrity. His image and thought of the Byronic Hero fascinated the public, his peers and his followers from the Literature community and from other communities as well. [3]
The first Byronic Hero, was probably Bryon himself. His wife Annabella coined the common, uncommon term “Byromania” to closely explain and depict the clamor around him. He was the classic depiction of the modern day Rockstar. He was fond of self-promotion and was supremely self-aware. All his portraits were drawn with a mindset of advertising him as a “Man of Action” and not as a poet or a romanticist. [2]
Lord Byron was a bisexual; however, the claims are yet to confirmed. Of course, the confirmation is not possible due to the heavy wave of suppression it has faced for decades. [1]
The figure, Byronic Hero, infuses much of Lord Byron’s work; in fact, many consider Byron, himself to be a perfect example of the characteristics of the Byronic Hero.
Popular study points that Lord Byron’s Byronic Hero was influenced by John Milton’s famous piece Satan from Paradise Lost. [4]
An excerpt is as follows:
In shape and gesture proudly eminent,
Stood like a tower. His form had yet not lost
All her original brightness, nor appeared
Less then Archangel ruined, and the excess
Of glory obscured: as when the sun new-risen
Looks through the horizontal misty air
Shorn of his beams, or from behind the moon,
In dim eclipse, disastrous twilight sheds
On half the nations, and with fear of change
Perplexes monarchs. Darkened so, yet shone
Above them all the archangel; but his face
Deep scars of thunder had intrenched, and care
Sat on his faded cheek, but under brows
Of dauntless courage, and considerate pride
Waiting revenge. Cruel his eye, but cast
Signs of remorse and passion, to behold
The fellows of his crime, the followers rather
(Far other once beheld in bliss), condemned
Forever now to have their lot in pain.
The Byronic Hero is a variant of the Romantic Hero [5]. Although there are traits and characteristics that exemplify the type, both Byron’s own persona as well as characters from his writings are considered to provide defining features.
The Byronic Hero first made it’s public appearance with Lord Byron’s semi-autobiographical epic narrative poem Childe Harold’s Pilgrimage, can be read for free on Project Gutenberg here Childe Harold’s Pilgrimage.
Thomas Babington Macaulay, popularly known Lord Macaulay, a British Historian and critic described the character as “a man proud, moody, cynical, with defiance on his brow, and misery in his heart, a scorner of his kind, implacable in revenge, yet capable of deep and strong affection“.
Byronic Hero was the instrument to many Romantic Classics and Gothic Love pieces of the 19th Century. One of the noteworthy one is Emily Brontë’s Wuthering Heights. The notorious, yet loved, Heathcliff is a classic depiction of a Byronic Hero.
A picture depicting the salient features of Heathcliff’s characteristics is as follows:
http://www.shmoop.com/wuthering-heights/heathcliff.html
In American Renaissance, writers like Poe and Hawthrone took active reference in their work from the Byronic Hero.
In fact, Professor Severus Snape, of the Harry Potter Series, played by Late Alan Rickman is a Byronic Hero. He is dark, he is arrogant; yet demonstrated supreme love and sacrifice.
enter image description here
In this blog, as we are talking of Lord Byron; I don’t want to miss the chance to show my admiration for Ada Lovelace, Lord Byron’s daughter. Who is widely regarded as the first computer programmer. She was the first person to actually recognize the capabilities of Charles Babbage’s Analytical Engine, beyond pure mathematical tasks.
Charles Babbage’s seminar at University of Turin in 1840 on his Analytical Engine was recorded by Luigi Menabrea, a young Italian Engineer and future Prime Minister of the country. The language of recording was French. That is when Charles Wheatstone, the founder of the Wheatstone Bridge, asked Ada to translate that. She not only translated the paper, but also added detailed notes to it. She broke-down the lecture into parts from to . In section , she explained the method of Bernoulli’s number using the Analytical Engine.
The following picture shows her algorithm:
Ada's Algo
For people wondering what are Bernoulli’s numbers, let me provide a very simple explanation,
Benoulli’s numbers are defined from the power series expansion of ; for integers we write so that,

Multiplying both sides by ; will give,
,
.
Very confusing, right? To me too. Some rigorous math has to go in to understand this; and Lovelace wrote an algorithm for this when women weren’t even allowed to attend school.
Cheers!

Sunday, 5 February 2017

Novice Alorithm design for Opinion Mining.

A Beginner’s guide to Opinion Mining.


Today any new comer to the world of Data Analysis will come across the term, “Sentiment Analysis”.
Question: What is Sentiment Analysis?
Definition: Analysis of the customer/user feedback about a particular situation/product is called sentiment analysis.
Now, there are projects and papers that do this task with many advanced techniques, such as NLP.
I choose to do it using a very simple method, I basically wanted to do a semi-supervised, search based, supervised-updating algorithm.
The pseudo code of the algorithm,
1. Initialise:
    negative[n] = [bad, worse, worst, fucked, shit]
    positive[n] = [good, awesome, better, best, cool]
2. Input Reviews.txt
3. Read Reviews.txt
4. Clean Reviews.txt(remove special characters and punctuation)
5. Search Review.txt
6. For each hit of word from negative[n], we add a -1 to the score. 
7. For each hit of word from positive[n], we add a +1 to the score.
The code for this is:
#List of words on the analysis occurs.
Good = ['nice','great','good','awesome', 'growth', 'bought', 'buy', ]
Bad = ['jerk','hate', 'change', 'privacy', 'problem', 'apple']


#Opening text file containing twitters
file = open("Reviews.txt", "r").read().split(' ')


print file

words = file

text = [word.strip(",.") for line in words for word in line.lower().split()]

postivity = 0
negativity = 0
no_significance = 0

for word in words:
    if(word in Good):
        print "found "+str(word)
        postivity = postivity + 1
        print "++"
    if(word in Bad):
        print "found "+str(word)
        negativity = negativity + -1
        print "--"
    print "\n"

print "\nthe input text has a positivity rating of : "+str(postivity)
print "\nthe input text has a negativity rating of : "+str(negativity)
print "\nUseless words: "+str(no_significance)

total = postivity + negativity + no_significance

print "\nTotal Score: "+str(total)


if postivity > negativity:
        print "\nRecommended Product"
else:
        print("\nNot recommended")

That is it, that is what I did.
The algorithm in itself is not that cool.
And you all know that, I always want the cool :D
To make it a little awesome, I used the word-severity based scoring.
So, in this case, instead of scoring all the words from negative[n] and positive[n] as and respectively, we do the give varied scores on the basis of the severity of the word.
Writing the words of negative[n] in the order of increasing severity, we get,
bad worse worst shit fucked,
the scores will be,
bad =
worse =
worst =
shit = and
fucked =
Now, we do the same for the words in positive[n], the scores are,
good =
awesome =
better =
best =
cool =
The implementation of this part is still under development.

Cheers!

Friday, 3 February 2017

Multiplication Algorithms.

I am presently enrolled in a course named Computational Techniques in Control Engineering. The Syllabus is Math extensive and we are expected to be pretty fluent in programming too; which is a great combination.
The course is handled by Professor A Ramakalyan.
The syllabus is as follows,
Syllabus
More details about it can be found at the website of National Institute of Technology, Trichy | Academic Curriculum | Instrumentation and Control Engineering.
So, there was one class in which he concluded the class with a detailed derivation of Gram-Schmidt Orthogonalization process.
And then he asks us what is the product of and , so with the basic grade school polynomial multiplication skill that we all have, we can easily look into the following,




Note :
It was simple and straight forward, which was highly unlikely to happen in a course taken by that particular teacher.
He asked us to find out the number of multiplications that we involved in finding the above product.
It is clear that, there are 4 multiplications. He then asked us to do find the same product using 3 multiplications!
This is where the level of awesomeness hits so high that it almost looks CRAZY!
As soon as I got back to the room, I started Googling and looked at some random research papers and stuff.
Hence this blog post.
In this blog post, we will briefly study the essence of Multiplication Algorithms; we will study the Karatsuba Algorithm in detail. We will also see the answer to the mind blowing question by Ramakalyan Sir.

Karatsuba Algorithm.

The usual grade school style of multiplication takes around time. Karatsuba is his paper “A. Karatsuba and Yu. Ofman (1962). “Multiplication of Many-Digital Numbers by Automatic Computers”. Proceedings of the USSR Academy of Sciences. 145: 293–294. Translation in the academic journal Physics-Doklady, 7 (1963), pp. 595–596”, presented that this multiplication can be done faster; in .
This number might seem pretty insignificant, but in cases where we tackle very very large multiplication problems, this works wonders.
I was looking into stack-overflow for a proper explanation while I found this answer. Let us look briefly into it,

Let, and
We are concerned with two numbers basically, and .
Now, we compute three multiplications, , , and .
Now,

and, .
This is how the Karatsuba Algorithm works.
To keep stuff in perspective, let us consider an example, where two -digit numbers are multiplied. [].
As per the classical multiplication algorithm, it will required, single multiplications, that means, multiplications; however by the Karatsuba Algorithm, this number can be reduced to single multiplications. And, if you look into the values of and , you will see that the latter is much much greater than the former.
Karatsuba algorithm was the first algorithm to be faster or better than the traditional multiplication algorithm that took a quadratic time.

Side note:

We must also consider the Gauss Complex Multiplication algorithm. It precise speaks of the problem.
This also works by decreasing the number of multiplications and increasing the number of additions and subtractions.
For the ,
we find,



Finally the real part,


if we look at the traditional method, we will see that we use subtraction, and addition. However, in this method, we use additions and subtractions.

Cheers!

Tuesday, 31 January 2017

Control Systems Engineering with MATLAB - 1

Hello guys,
I hope you liked my previous blog on making your own R packages. If you haven’t yet seen it, please have a look at it here|How I made my first R Package?.


Today, I will do some very elementary Control system using MATLAB.


This blog post will have a little mathematics, so, brace yourselves.


Whenever, we talk of transfer functions, we think of Transfer functions, now what are transfer functions?
To keep it simple, if your = and your = ,
Then, the transfer function is given by the ratio of the laplace transform of the change in to the change in , mathematically,


= , here means Transfer function in -domain; Laplace domain is generally callled the -domain.


I believe, this is a very common fact. Now, let’s do some MATLAB.


We define a transfer function in MATLAB using the function tf function.


An illustration is as follows,
Imgur


You can change the co-efficients as you wish. just change the numbers inside the [ ].


For example,
H = tf([1 2], [1 2 3]), will yield,
=


Now, we convert the transfer function from to , basically from continuous to discrete domain,


For this, we use the c2d function, which is basically the continuous to discrete function for MATLAB.


The function looks like G = c2d(_transfer function_, _sampling time_),
So, for,
H = tf([1 2], [1 2 3])
the c2d function when applied will look like,
G = c2d(H, 0.1) 
0.1 second is the sampling time.


Have a look at this picture,
Imgur


Similarly, the d2c command also does the reverse, but, as your intuition speaks, since we are going into the continuous from the discrete domain, we don’t need a sampling time.


So, the syntax is,

I = d2c(G)


Look at the image below,
Imgur




That is all for today folks. In the next blog, I will be talking about Gain and Phase margins, Bode Plots and various other control engineering concepts.
Cheers!

Monday, 7 November 2016

How I made my first "serious" R package?

Hello there,

It was when I was working on a Human Activity Recognition system using R when a very good and challenging idea occurred to me.

The idea was not something that could be done with minimal effort, in fact, it was one of the toughest projects that I have ever done.

The idea was, to CREATE AN R PACKAGE OF MY OWN.

So, I read up some blogs on the internet and go to know how to create a basic R package. 

The associated packages needed to proceed with the work are :

  1. Devtools.
  2. ROxygen2
The above-mentioned packages can be easily installed by the following code:



After you are done with the above step. You need to load both the packages. Simple enough, I will still add the code here,

Now, we are all set-up.

One thing I would like to point out is that packages are nothing but functions.

So, let's write a function for our package.

I would like to write a function for getting the Benford Score of a given digit.

I don't know if Benford score is a legit term, but what I mean by it is, given a digit ( say $i$ ),
what is the probability that the first digit of any random number will be $i$ .

Wikipedia Article on Benford Law,

Benford's law, also called the first-digit law, is an observation about the frequency distribution of leading digits in many real-life sets of numerical data. The law states that in many naturally occurring collections of numbers, the leading significant digit is likely to be small.For example, in sets which obey the law, the number 1 appears as the most significant digit about 30% of the time, while 9 appears as the most significant digit less than 5% of the time. By contrast, if the digits were distributed uniformly, they would each occur about 11.1% of the time.Benford's law also makes (different) predictions about the distribution of second digits, third digits, digit combinations, and so on.
Mathematically, the probability of $i$ being the first digit of any number is given by,

$\hspace{2.0cm}$  $\large {P(i)}$ = $\large {log_{10} (1 + \frac{1}{i})}$

Writing it into a function won't be a big deal.

Let's start then, there isn't much to explain. Therefore, the code is as follows,


Now, when you run,


You must get something like this : [1] 30.103 

This is the probability of $1$ occurring in the first place.

Go ahead and try for other digits as well, and you will see the probabilities decreasing as you go from $1$ to $9$.

Yeah, this is the Zipf's Law for numbers.

Okay, enough with the small talk.

Let's make some babies now,

Oops! Sorry. Packages.

Follow the following steps,
  1. Save the function file as main.R in a folder.
    No specific reason, I just like the name "main".
  2. Press Ctrl+Shift+H and travel to the folder in which main.R is saved and select that folder as the working directory. 
  3. Now, type create("Myfirstpkg"),
    You should see something like this,

    Creating package 'Myfirstpkg' in 'C:/Users/user/Desktop/pkgFolder'
    No DESCRIPTION found. Creating with values:

    Package: package

    Title: What the Package Does (one line, title case)

    Version: 0.0.0.9000
    Authors@R: person("First", "Last", email = "first.last@example.com", role = c("aut", "cre")) Description: What the package does (one paragraph). Depends: R (>= 3.3.2) License: What license is it under? Encoding: UTF-8 LazyData: true * Creating `package.Rproj` from template. * Adding `.Rproj.user`, `.Rhistory`, `.RData` to ./.gitignore
  4. Now, inside the folder, you will see another folder named after your package name, here it is Myfirstpkg

  5. Go inside Myfirstpkg folder, there will be another folder named "R", put your main.R in that folder.
  6. Now, do some documentation,
  7. After this, go inside the Myfirstpkg through RStudio console (Refer Step 1) and then, type, document(),

    You should see something like this, 


After all these, you need to edit the DESCRIPTION file as follows,


The above image is taken from Writing R Extensions.

After you are done with it, do the following,


Yaaaaay!!

You made your first R package. :)

For more serious development, upload your file to Win Builder R.

If you pass all the cases without any error, submit it to CRAN. :)

Please have a look at my package : csvFileDescriptor-on Github. Install it and please star it if you like the functionality.

Cheers.