import package5 #(python)when package5 = directory

Python import statement has many trivial or non-trivial scenarios. Here are some common ones:

Common scenario 1: we “import modu2” to execute modu2.py and also exposes all the “public” objects and functions in modu2 as modu2.something

(Simplest scenario but still not known to some programmers.)

Common scenario 2: if pack3 is a directory containing some python modules, we can
“import pack3.modu5” or “from pack3 import modu5”. Easy to understand.

Here’s a relative unusual scenario:

./pip install --verbose --no-deps --index-url=http://ficlonapd05.macbank:9090/api/ pymodels-linux==0.0.1.480 # assuming 0.0.1.480 is the pymodels version to test

./python
>>> import pymodels
>>> pymodels.__file__
'/home/tcrmg/vtan7/myenv/lib/python2.6/site-packages/pymodels/__init__.pyc'
>>> pymodels.BlackImplyVol(.2, 3,3, 1, 0, 1,1)

In this case, there’s a pymodels directory but BlackImplVol is a function in the compiled library ./pymodels/pymodels_imp.so. How is this function accessible?

A: when “import pymodels” is run, the ./pymodels/__init__.py script executes, which does  “from pymodels_imp import * “

book value of leverage

Simple definition of book-value according to Wikipedia — Assets such as buildings and equipment are valued based on their initial acquisition cost.

Now we focus on book value of leverage. A simple analog is the leverage of a property bought on an (unsecured) commercial loan

Suppose the house was bought for $600k with $480k loan. After a few years, loan stays at $480 (to be paid off at maturity), but house doubles to $1.2m.

Book value of EQ is still 600-480 = $120k, but current EQ would be 1.2m – 480k = 720k.

The book value of leverage was and is still 600/120 = 5.0. In contrast, The current value of leverage would be (1200k)/720k, which is lower and safer than book value of leverage.
Now the bleak picture — suppose AS value drops from 600k to 500k. Book leverage remains 600/120 = 5.0. In contrast, Current value of leverage is 500/(500 – 480) = 25.0. Dangerously high leverage. Further decline in asset valuation would wipe out equity and the entire account is under water. Some say the property is under-water but i feel really we are talking about the borrower i.e. owner of the property — i call it the “account”.
—-
(Book value of) Leverage in “literature” is defined as(book value of) ASset / EQuity (book value)

Equivalently,

   (LIability + EQ) / EQ …. (all book values)

The denominator is much lower as book value than the current value. For a listed company, Current value of total equity is total market cap == current share price * total shares issued so far. In contrast, Book value is the initial capital of the founder + actual dollars raised through the IPO, ignoring the increase in value of each share. Why is this book value less useful? We need to understand the term “shareholder equity”.  This term logically means the “value” of the shares held by the shareholders (say a private club of …. 500 teachers). Like the value of your house, this “value” increases over time.

git remote update

I had a strange problem

  1. I cloned a repo and had only one branch
  2. I know there’s a remote branch rb3, but
  3. git checkout rb3 # failed with “error: pathspec ‘rb3’ did not match any file(s) known to git.”

To investigate, I ran

  1. git branch -a # shows no trace of rb3
  2. git remote update
  3. git branch -a # now shows remotes/origin/rb3

I now think the problem was — I have a dir named ‘rb3’  !

programmer^mathematician^basketballer #market depth

Programmer is one of few skill with a good “market depth”:

* a mediocre talent can find jobs to pay a decent blue-color knowledge worker wage
* a top talent can always find challenging and well-paying roles. There’s over-demand for the next 100 years. * Any talent level in between can easily find jobs with a suitable salary. * You can even teach this skill at various schools

I guess healthcare as a skill is similar.

Basketballers (any professional athletes) – very few get paid a good salary.

Mathematicians – I only know the teaching and quant jobs.

struct packing^memory alignment %%experiment

Sound byte — packing is for structs but alignment and padding is Not only for structs

Longer version — alignment and padding apply to Any object placement, whether that object is a field of a struct or an independent object on stack. In contrast, the packing technique is specific to a struct having multiple fields.

https://github.com/tiger40490/repo1/blob/cpp1/cpp1/memAlign.cpp has my test code to reveal the rules:

  1. For a (8-byte) long long object on stack (or in a struct), the address is 8-byte aligned. So padding is added by compiler, unless you say “packed” on a struct.
  2. For a (4-byte) int object on stack (or in a struct), the address is 4-byte aligned.
  3. For a (2-byte) short object on stack (or in a struct), the address is 2-byte aligned.
  4. For a char object on stack (or in a struct), the address is 1-byte aligned. All memory address are 1-byte aligned, so compiler adds no padding.

http://stackoverflow.com/questions/11108328/double-alignment Wug’s answer echoes Ashish’s comment that tiny fields like char should be grouped together, due to Rule #4. This applies to stack layout as well [1]. However, compiler optimization can be subversive:

Not only can the compiler reorder the stack layout of the local variables, it can assign them to registers, assign them to live sometimes in registers and sometimes on the stack, it can assign two locals to the same slot in memory (if their live ranges do not overlap) and it can even completely eliminate variables.

[1] See https://stackoverflow.com/questions/1061818/stack-allocation-padding-and-alignment

never exercise American Call (no-div), again

Rule 1: For a given no-dividend stock, early exercise of American call is never optimal.
Rule 1b: therefore, the price is similar to a European call. In other words, the early exercise feature is worthless.

To simplify (not over-simplify) the explanation, it’s useful to assume zero interest rate.

The key insight is that short-selling stock is always better than exercise. Given strike is $100 but the current price is super high at $150.
* Exercise means “sell at $150 immediately after buying underlier at $100”.
* Short means “sell at $150 but delay the buying till expiry”

Why *delay* the buy? Because we hold a right not an obligation to buy.
– If terminal price is $201 or anything above strike, then the final buy is at $100, same as the Exercise route.
– If terminal price is $89 or anything below strike, then the final buy is BETTER than the Exercise route.

You can also think in terms of a super-replicating portfolio, but I find it less intuitive.

So in real markets when stock is very high and you are tempted to exercise, don’t sit there and risk losing the opportunity. 1) Short sell if you are allowed
2) Exercise if you can’t short sell

When interest rate is present, the argument is only slightly different. Invest the short sell proceeds in a bond.

for every black matrix cell, paint its entire row/col black

https://www.geeksforgeeks.org/a-boolean-matrix-question/

Q: Given a boolean matrix mat[M][N] of size M X N, modify it such that if a matrix cell mat[i][j] is 1 then make all the cells of ith row and jth column as 1.

–sol1
1) typewriter scan for the next black (i.e. 1)
2) once found, change it to brown (i.e. 2) and paint all cells on its row and its column to brown.
3) go back to #1 until no more
4) change all brown to black.

c++atomic^volatile #fencing

See other posts about volatile, in both c++ and java.

For a “volatile” variable, c++ (unlike java) compiler is not required to introduce memory fence i.e. updates by one thread is not guaranteed to be visible globally. See Eric Lippert on http://stackoverflow.com/questions/26307071/does-the-c-volatile-keyword-introduce-a-memory-fence

based on [[effModernC++]]
atomic volatile
optimization: reorder prevented permitted 😦
atomicity of RMW operations enforced not enfoced
optimization: redundant access skip permitted prevented
cross-thr visibility of update probably enforced no guarantee [1]
memory fencing enforced no guarantee

[1] https://en.wikipedia.org/wiki/Memory_barrier