dotnet must convert your source code into machine code

You know that Java and c# convert (Phase 1) your source code into IL bytecode, but it must still become (Phase 2) machine code before it can run on the processor, under supervision of the VM.

In Phase 1, the Intermediate Language compiler (javac or csc.exe) converts source code into platform-Independent IL bytecode, packaged into physical files — class files, jars or assemblies in extended PE [1] format. Note Phase 2 would produces no file.
** Unlike a C++ object file the IL files aren’t strictly executable on the Real Machine. Executable on the VM installed in the Real Machine.
**The most attractive feature of the JVM lies in the platform-independent bytecode. Write once run anywhere (including Mac, Linux, etc) with decent performance comparable to C. That’s why we could (unbelievable at first) build jars on windows and run them on unix.

In Phase 2, the JIT compiler converts IL bytecode into platform-dependent, CPU-specific machine code.
** presumably, happens at run time, each time you execute the IL files
** Is there a command line tool to run JIT compilers and produce the native executable file? Look at ngen.exe. Even if you can get the native code it is designed to execute within the VM

Phase 2 is less visible. When app developers talk about compilation, they mean Phase 1

Phase 2 is platform-specific. Phase 1 is platform-independent.

Regular assembly file holds IL code on disk. Upon first use each method is JIT-compiled to platform-dependent machine code. So if you monitor the execution speed of such a method, you see
app launch – slow – fast – fast – fast …app launch – slow – fast – fast …app launch – slow – fast – fast …

Now, ngen.exe dose a pro-active JIT, so the native machine code is saved on disk in the GAC and/or native image cache. The same method would show no “slow” —
app launch – fast – fast – fast …app launch – fast – fast – fast …app launch – fast – fast – fast …

It’s interesting to compare perl. I believe perl also compiles source code into bytecode, which gets interpreted.

For python, see http://stackoverflow.com/questions/471191/why-compile-python-code

[1] for the PE format extended for dotnet, see other posts in this blog, or see http://www.informit.com/articles/article.aspx?p=25350

rolling fwd measure#Yuri

(label: fixedIncome, finMath)

 

In my exam Prof Yuri asked about T-fwd measure and the choice of T.

I said T should match the date of cashflow. If a deal has multiple cashflow dates, then we would need a rolling fwd measure.  See [[Hull]

However, for a standard swaption, I said we should use the expiry date of the option. The swap rate revealed on that date would be the underlier and assumed to follow a LogNormal distro under the chosen T-fwd measure.

higher order function ^ first class function

See also http://stackoverflow.com/questions/10141124/any-difference-between-first-class-function-and-high-order-function.

Higher Order Function — A “boss” function that takes in smaller worker functions. Usually these workers are “applied” on a sequence.

Simplest Example: Python filter()/reduce()/map()

First Class Function — Each function is treated like a regular object and passed in/out of HOF. Often implemented as lambda (or closure). Functor too [[Essential C++]] P127.

Why FirstClass? I think objects are the only first class thing in traditional languages.

Simplest Example: Python lambda
——————
Python has other HOF features. I guess decorator might be.

STL functors qualify as FCF. STL algorithms taking those functors qualify as HOF. For example,
for_each()
transform()
generate()
find_if()
copy_if()

C# Linq has many HOF functions like ForEach(), Aggregate() and many aggregate functions. Many other C# function take (unicast) delegate arguments.
C# lambda (and anonymous delegates) is the typical FCF.

python q[import]directive complexity imt java/c++/c#

I would say it “does more work” not just “more complicated”…

Name conflicts, name resolution … are the main purpose of (import/using/include) in java/c++/c#. (Side question — Can c++ header files execute arbitrary statements? I think so since it’s just pasted in… Minor question)

In contrast, python modules are executed line by line the first time they are imported. P300 [[programming python]]. I think this can include arbitrary statements. This is the major departure from “Tradition”.

I guess “from … import …” is more traditional and won’t execute arbitrary code??

WPF binding system queries VM upon PropertyChanged event

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx pointed out …

The INotifyPropertyChanged interface contains an event pseudo-field called PropertyChanged.

Whenever a property on a ViewModel object (or a Model object) has a new value, it can (should?) raise the PropertyChanged event to notify the WPF binding system. Upon receiving that notification, the binding system queries the property, and the bound property on some UI element receives the new value. I believe this is how new data hits the screen.

I believe the callbacks run on the UI thread, just like swing.

In order for WPF to know which property on the ViewModel object has changed, the PropertyChangedEventArgs class exposes a PropertyName property of type String. You must be careful to pass the correct property name into that event argument; otherwise, WPF will end up querying the wrong property for a new value.

For PropertyChanged to work, i think we have to use xaml binding with a prop name. In contrast, the alternative — defining ItemSource in codebehind probably doesn’t work. 

sample code showing boost scoped_lock# !! c++14

#include <iostream>
#include <boost/thread.hpp>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
boost::posix_time::milliseconds sleepTime(1);
 
template<typename T>
class MyQueue {
 public:
 void enqueue(const T& x) {
  cout << "\t\t\t > enqueuing ... " << x << "\n";
  boost::mutex::scoped_lock    myScopedLock(this->mutex_);
  cout << "\t\t\t >> just got lock ... " << x << "\n";
  list_.push_back(x);
  // A scoped_lock is destroyed (and thus unlocked) when it goes out of scope
 }
 T dequeue() {
  boost::mutex::scoped_lock lock(this->mutex_);
  if (list_.empty()) {
   throw 0; // unlock
  }
  T tmp = list_.front();
  list_.pop_front();
  cout << "< dequeu " << tmp << "\n";
  return (tmp);
 }
 private:
 std::list<T> list_;
 boost::mutex mutex_;
};
MyQueue<std::string> queueOfStrings;
int reps = 5;
void sendSomething() {
 for (int i = 0; i < reps; ++i) {
  stringstream st;
  st << i;
  queueOfStrings.enqueue("item_" + st.str());
  boost::this_thread::sleep(sleepTime);
 }
}
void recvSomething() {
 for (int i = 0; i < reps*3; ++i) {
  try {
   queueOfStrings.dequeue();
  } catch (int ex) {
   cout << "<- - (    ) after releasing lock, catching " << ex <<"\n";
   boost::this_thread::sleep(sleepTime*2);
  }
 }
}
int main() {
 boost::thread thr1(sendSomething);
 boost::thread thr2(recvSomething);
 thr1.join();
 thr2.join();
}

double-ptr usage #5 – pointer allocated on heap

Whenever a pointer Object (32-bit object[1]) is allocated on heap, there’s usually (always?) a double pointer somewhere.

new int*; //returns an address, i.e. the address of our pointer Object. If you save this address in a var3, then var3 must be a double-ptr.

int ** var3 = new int*; //compiles fine, but you should not access **var3

However, I feel we seldom allocate a pointer on heap directly. More common patterns of allocating pointer on heap are
– if an Account class has a pointer field, then an Account on heap would have this pointer field allocated on heap.
– a pointer array allocated on heap

[1] assuming a 32-bit machine.

CompletionService.java – initial phrasebook

producer/consumer – upgraded.

buffer – as explained elsewhere in my blog, there’s buffer in any async design. In the ExecutorCompletionService scenario, the buffer is the “completion queue”. In the classic producer/consumer scenario, buffer is the item queue.

items = “tasks” – In P/C setup, Thread 1 could be producing “items” and Thread 2 could be taking up the items off the buffer and using them. In the important special case of task items, the consumer thread
(possibly worker thread) would pick up the task from queue and execute them. CompletionService is all about task items.

tasks executed by..? – in P/C with task queue, tasks are executed by consumer. In CompletionService, tasks are executed by the mysterious “Service”, not consumer. See CompletionService javadoc.

3-party – 1 more than P/C. Beside the P and C threads, the task executor could run on another thread.

func ptr as template non-type param

This is the #1 eye-opener in [[essential c++]]

http://bigblog.tanbin.com/2012/03/non-dummy-type-template-parameters.html describes the background of Non-Dummy-Type parameters in a class template. Things like the maxRows in template class matrix_double. [[Essential c++]] succinctly describes that usage but also illustrates on P185 how you can put in a func ptr Type (not a dummy type) in place of the “int maxRows”. I find it a very powerful technique. (I guess java and c# take other routes since they don’t support NDT template parameters.) This is how I guess it works. 

First understand a real usage scenario and focus on a concrete class instantiated from such a template. At runtime such a class can have 888 class-instances, but they all share a single special “static field”, which is the function address[1] supplied to concretize the template.
If you later concretize the template with a 2nd function address, you get a 2nd concrete class. You can create 877 instances of this 2nd class. 
For the simple NDT, you supply an integer constant like 55 when you concretize the template matrix_double. Similarly, you supply a function address as a constant when concritizing our numeric_sequence template. More generally, any constant expression can be used for a NDT.
How useful is this technique? It allows you to concretize a template using a specific function address — a specific “behavior”. It could beat a static field in some cases. For example, You can concretize a given template
* once with type Account + AccountBehavior
* once with type Student + StudentBehavior
* once with type int + intBehavior
[1] the address of a real function, not a func ptr Type.

2 focuses when learning a new tech skill: IV^GTD

When we learn a new tech skill, we need very, very specific goals and guidelines to help us choose what NOT to dig into too early.

Focus #1: IV topics, a well-defined focus. Basically textbook knowledge. Go deep enough but not too deep on a particular subtopic. Examples — op-new, pure virtual, const ref param of copier, recursive locks, OOM calling new,

Focus #2: GTD i.e. dev productivity — as measured by managers, bench-marked against other developers. Incidentally, perceived productivity sometimes depends so heavily on local system knowledge that a new hire always lags behind no matter what. I feel productivity and GTD has low dependency on #1, as most of the textbook knowledge is irrelevant.

Focus #2b: tools — see post on 2 categories of tools.

Focus #2c? tips on StackOverflow. No such books. No way to

Focus #?: best practices — or Acceptable practices and well-trodden paths

Focus #?: learn all the syntax details to read any source code on books, interviews and online

Focus #: high-level design decisions, for technical leadership roles. Myriads of decisions are made every hour during development, some at high some at low levels. Apparently, a lot of these decisions involve theoretical understanding.

Q-Q plot learning notes

Based on http://en.wikipedia.org/wiki/Q%E2%80%93Q_plot

2 simple use cases. First look at 2 distinct continuous distributions, like Normal vs Gamma, or LogNormal vs Exponential. Be concrete — Use real numbers to replace the abstract parameters.

We (won’t but) could plot the two CDF curves, both strongly increasing from 0 to 1. To keep things simple we will restrict the random variables to be (0, +inf).

Now invert both CDF functions to get so-called quantile function. So for each q value between 0 and 1, like 0.25, we can look up the corresponding “quantile” value that the random variable can take on.

We look up both quantile functions to get 2 such quantile values. Use them as (x,y) coordinate and plot a point. If we pick enough q values, we will see a curve emerging — the Q-Q plot. X-axis will be the range values of one distribution and Y-axis the other distribution. Both could be 0 to inf.

Now 2nd use case (more useful) — comparing an empirical distribution (Y) against a theoretical model (X). We still can look up the quantile value for any q value between 0 and 1, so we still can get a Q-Q plot.

expectation of 2 (possibly correlated) RV multiplied

I guess this is too basic to be covered other sites.

First, a random variable is not a regular variable we use in algebra or calculus. It’s more like a noisegen…

Second, X1 * X2 is not really multiplying 2 numbers and you get another number. This expression actually represents a random variable that is strictly “controlled” by 2 other (possibly correlated) random variables. At any moment, the output is the product of those two
outputs.

E[ X1*X2 ] how can we simplify, and how is it related to E[X1]*E[X2] ?

Let’s denote u1 as the expectation or population mean of X1. The key formula is

E[X1*X2] = u1*u2 + Cov(X1 , X2)

case: when independent or uncorrelated, E[X1*X2] = u1*u2
case: when positively correlated, Cov > 0, so E[X1*X2] > u1*u2
case: when negatively correlated, Cov < 0, so E[X1*X2] < u1*u2

Easily verifiable in matlab —

a=[1:5]’
b=-a
cov(a,b)
mean(a.*a)
mean(a.*b)