# Generalized Linear Model

This article on Generalized Linear Model (GLM) is based on the first four lectures of Machine Learning by Andrew Ng. But the structure of the article is quite different from the lecture. I will talk about exponential family of distributions first. Then I will discuss the general idea of GLM. Finally, I will try to derive some well known learning algorithms from GLM.

## Exponential Family

A family of distributions is an exponential family if it can be parametrized by vector $\eta$ in the form $$P(y; \eta) = b(y)\exp(\eta^{T} T(y)-a(\eta)),$$ where $T(y)$ and $b(y)$ are (vector-valued) functions in terms of $y$, and $a(\eta)$ is a function in terms of $\eta$.

$\eta$ is called the natural parameter and $T(y)$ is called the sufficient statistic.

# LeetCode Contest 49

class Solution(object):
def findLengthOfLCIS(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
ans = i = 0
while i < len(nums):
j = i + 1
while j < len(nums) and nums[j] > nums[j-1]:
j += 1
ans = max(ans, j-i)
i = j
return ans


# LeetCode Contest 48

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def findSecondMinimumValue(self, root):
"""
:type root: TreeNode
:rtype: int
"""
nums = set()
def dfs(root):
if not root:
return
dfs(root.left)
dfs(root.right)
dfs(root)
return -1 if len(nums)<2 else sorted(list(nums))[1]


# LeetCode Contest 47

class Solution(object):
def checkPossibility(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
def check(i):
if i >= len(nums):
return True
temp = [nums[j] for j in xrange(len(nums)) if j != i]
return all(temp[j]>=temp[j-1] for j in xrange(1, len(temp)))
i = 1
while i < len(nums) and nums[i] >= nums[i-1]:
i += 1
return check(i) or check(i-1)


# 意志力

“自我损耗”是作者以及书中提到的研究者在做实验的时候常用的手段。这也有警醒的意味：意志力减弱的时候，渴望还会变强！

1.你的意志力是有限的，使用就会消耗。 2.你从同一账户提取意志力用于各种不同任务。

# LeetCode Contest 45

class Solution(object):
def judgeCircle(self, moves):
"""
:type moves: str
:rtype: bool
"""
cnt = collections.Counter(moves)
return cnt['L']==cnt['R'] and cnt['D']==cnt['U']


# Pelican Signals

Pelican的插件系统是使用blinkersignal实现的。Pelican所有可以用的signals可以在signals.py找到。本文的目的是记录这些signals是在Pelican运行中什么时候发出的。

(1) Pelican有一个叫做Pelican的类，含有程序的主体框架。当Pelican的一个实例pelican初始化完成之后（基本设置，加载插件），发出第一个signal。

signals.initialized.send(pelican)


# LeetCode Contest 44

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def findTarget(self, root, k):
"""
:type root: TreeNode
:type k: int
:rtype: bool
"""
nums = collections.defaultdict(int)
def dfs(root):
if not root:
return
n = root.val
nums[n] += 1
dfs(root.left)
dfs(root.right)
dfs(root)
for n in nums.keys():
m = k - n
if n != m and m in nums:
return True
if n == m and nums[n] > 1:
return True
return False


# LeetCode Contest 43

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
def findDuplicateSubtrees(self, root):
"""
:type root: TreeNode
:rtype: List[TreeNode]
"""
seen = {}
def dfs(root):
if not root:
return ''
left = dfs(root.left)
right = dfs(root.right)
s = '{}({})({})'.format(root.val, left, right)
if s not in seen:
seen[s] = [1, root]
else:
seen[s][0] += 1
return s
dfs(root)
ans = []
for c, node in seen.values():
if c > 1:
ans.append(node)
return ans