VBA ≅ javascript on a webpage (Singletons)

Let’s focus on the typical use cases —

* A typical javascript app Operates on a DOM tree of the enclosing webpage.
* A typical VBA app operates on the EOM tree (Excel object model) of an attached workbook.

—-} In both scenarios, the programmer’s Main job is manipulation of the objects in the *OM. The *OM tree is essentially a big family of singletons. I’d say most GUI apps rely heavily on singletons even if you don’t like singletons.

Usually the VBA app is tailor made for that particular workbook. Tight coupling between the workbook and the VBA module. Javascript is similar.

simple VBA – copy sheets from Workbook A to B

Sub Btn_Click()
    Dim ws As Worksheet
    Dim targetWB, sourceWB As Workbook
    Set targetWB = ActiveWorkbook
    Workbooks.Open Filename:=Range(“B1”).Text, ReadOnly:=True
    Set sourceWB = ActiveWorkbook
    For Each ws In Worksheets
        ws.Copy After:=targetWB.Sheets(targetWB.Sheets.Count)
    Next ws
    sourceWB.Close SaveChanges:=False
End Sub

c# IV questions found online

I want to see some knowledge of things like the GAC, CLR and JIT. Here are some sample C#-specific questions: 
– Where are the framework assemblies stored? How is this useful? 
– Explain the abstract keyword and what is an example of its use? 
– Can you prevent a class from being inherited by another class? If so, how? 
Easy questions:
    * Is System.String a class or a struct (or reference or value type if you prefer).
    * Explain IDisposable and the 'using' statement.
    * Explain public, protected, private, and internal.
Intermediate questions:
    * What's the difference between Hashtable and Dictionary?
    * What does int? mean? Explain the relationship with Nullable.
Hard questions:
    * Explain the following snippet of code: 'from x in collection select new { x.Foo }'. What is the compiler doing? What is the CLR executing?
    * Explain the “yield” keyword. What is the compiler doing internally?
# What are Generics ? How do they affect performance.
# How would you define an Anonymous Functions? What is the difference between Anonymous Methods and Lambda Expressions?
# How does an Enumerator (IEnumerable & IEnumerator) work ? How does an Iterator work in C# 2.0 ?
# What is Garbage Collection ? How does it work ? What are generations in GC ?
# What is the role of an Extension Method ? When would you use it ?
# What is a Lambda Expression ? Where would you use it ?
# How would you define a new custom event ?
# How would you choose between a pure abstract base class and an interface.
# What is MVC how does it differ from MVP ?
# What is Dependency Injection / Inversion of Control ?
Everyone who writes code
    * Describe the difference between a Thread and a Process?
    * What is a Windows Service and how does its lifecycle differ from a “standard” EXE?
    * What is the maximum amount of memory any single process on Windows can address? Is this different than the maximum virtual memory for the system? How would this affect a system design?
    * What is the difference between an EXE and a DLL?
    * What is strong-typing versus weak-typing? Which is preferred? Why?
    * Corillian's product is a “Component Container.” Name at least 3 component containers that ship now with the Windows Server Family.
    * What is a PID? How is it useful when troubleshooting a system?
    * How many processes can listen on a single TCP/IP port?
    * What is the GAC? What problem does it solve?
Mid-Level .NET Developer
    * Describe the difference between Interface-oriented, Object-oriented and Aspect-oriented programming.
    * Describe what an Interface is and how it’s different from a Class.
    * What is Reflection?
    * What is the difference between XML Web Services using ASMX and .NET Remoting using SOAP?
    * Are the type system represented by XmlSchema and the CLS isomorphic?
    * Conceptually, what is the difference between early-binding and late-binding?
    * Is using Assembly.Load a static reference or dynamic reference?
    * When would using Assembly.LoadFrom or Assembly.LoadFile be appropriate?
    * What is an Asssembly Qualified Name? Is it a filename? How is it different?
    * Is this valid? Assembly.Load(“foo.dll”);
    * How is a strongly-named assembly different from one that isn’t strongly-named?
    * Can DateTimes be null?
    * What is the JIT? What is NGEN? What are limitations and benefits of each?
    * How does the generational garbage collector in the .NET CLR manage object lifetime? What is non-deterministic finalization?
    * What is the difference between Finalize() and Dispose()?
    * How is the using() pattern useful? What is IDisposable? How does it support deterministic finalization?
    * What does this useful command line do? tasklist /m “mscor*”
    * What is the difference between in-proc and out-of-proc?
    * What technology enables out-of-proc communication in .NET?
    * When you’re running a component within ASP.NET, what process is it running within on Windows XP? Windows 2000? Windows 2003?
Senior Developers/Architects
    * What’s wrong with a line like this? DateTime.Parse(myString);
    * What are PDBs? Where must they be located for debugging to work?
    * What is cyclomatic complexity and why is it important?
    * Write a standard lock() plus “double check” to create a critical section around a variable access.
    * What is FullTrust? Do GAC’ed assemblies have FullTrust?
    * What benefit does your code receive if you decorate it with attributes demanding specific Security permissions?
    * What does this do? gacutil /l | find /i “Corillian”
    * What does this do? sn -t foo.dll
    * What ports must be open for DCOM over a firewall? What is the purpose of Port 135?
    * Contrast OOP and SOA. What are tenets of each?
    * How does the XmlSerializer work? What ACL permissions does a process using it require?
    * Why is catch(Exception) almost always a bad idea?
    * What is the difference between Debug.Write and Trace.Write? When should each be used?
    * What is the difference between a Debug and Release build? Is there a significant speed difference? Why or why not?
    * Does JITting occur per-assembly or per-method? How does this affect the working set?
    * Contrast the use of an abstract base class against an interface?
    * What is the difference between a.Equals(b) and a == b?
    * In the context of a comparison, what is object identity versus object equivalence?
    * How would one do a deep copy in .NET?
    * Explain current thinking around IClonable.
    * What is boxing?
    * Is string a value type or a reference type?
    * What is the significance of the “PropertySpecified” pattern used by the XmlSerializer? What problem does it attempt to solve?
    * Why are out parameters a bad idea in .NET? Are they?
    * Can attributes be placed on specific parameters to a method? Why is this useful?
C# Component Developers
    * Juxtapose the use of override with new. What is shadowing?
    * Explain the use of virtual, sealed, override, and abstract.
    * Explain the importance and use of each component of this string: Foo.Bar, Version=, Culture=neutral, PublicKeyToken=593777ae2d274679d
    * Explain the differences between public, protected, private and internal.
    * What benefit do you get from using a Primary Interop Assembly (PIA)?
    * By what mechanism does NUnit know what methods to test?
    * What is the difference between: catch(Exception e){throw e;} and catch(Exception e){throw;}
    * What is the difference between typeof(foo) and myFoo.GetType()?
    * Explain what’s happening in the first constructor: public class c{ public c(string a) : this() {;}; public c() {;} } How is this construct useful?
    * What is this? Can this be used within a static method?

##[12] some c++(mostly IV)questions

Q: what if I declare but not implement my dtor?
A: private copy ctor is often left that way. Linker will break.
Q: how to access a c++ api across network
A: corba?
Q: default behavior of the q(less) class-template
A: I have at least 2 posts on this. Basically it calls operator< in the target type (say Trade class). If missing, then compiler fails.
Q: how do you detect memory leak?
A: valgrind
%%A: customize op-new and op-delete to maintain counters?
Q: in java, i can bind a template type param — bound params. c++?
%%A: no, but I have a blog post on the alternative solutions
Q891: are assignment op usually marked const?
%%A: no it must modify the content.
Q891a: what if i override the default one with a non-const op=? will the default one still exist?
A: I think both will co-exist
Q: do people pass std::strings by ref? See Absolute C++
%%A: yes they should
Q: unwrap a null ptr
A: undefined behavior. Often termination.
Q: delete a null ptr
A: C++ guarantees that operator delete checks its argument for null-ness. If the argument is 0, the delete expression has no effect.
Q: comparing null pointers?
A: In C and C++ programming, two null pointers are guaranteed to compare equal
Q: use a ref to a reclaimed address?
A: Rare IV… undefined. May crash.
Q: can i declare a Vector to hold both Integers and Floats?
A: slicing. To get polymorphism, use ptr or ref.
Q: what op must NOT be overloaded as member func?
A: rare IV…… qq(.) is one of five.
Q: Can i declare an Insect variable to hold an Ant object, without using pointers? “Ant a; Insect i = a;”?
A: see post on slicing
Q: Without STL or malloc (both using heap), how does C handle large data structures.
%%A: I used to deal with large amounts of data without malloc. Global var, linked data structure, arrays
%%A: I can have a large array in main() or as a static variable, then use it as a “heap”

pitfalls with NTFS junction point

http://www.techrepublic.com/article/manually-creating-junction-points-in-windows-xp/5388706 and
http://en.wikipedia.org/wiki/NTFS_junction_point warn —

^^Junction point target must be a local file system directory. Target can’t be a file.
^^Most delete operations are junction-unfriendly. Can damage the target i.e. the real files !
^^DOS command dir can report odd free-space statistics on drives that contain folders acting as junction points.
^^Junction points can cause havoc with certain backup programs, that aren’t junction-point aware.
^^if a target folder C:\a\b\T contains some kind of link (symlink? hard link? shortcut?) then I find it troublesome to move the folder content somewhere and replace C:\a\b\T with a junction point

^^Junction points do not work at boot, so it’s impossible to “redirect” i.e. substitute a hardlink for e.g.:

Nevertheless, it is possible to redirect:
\Documents and Settings
\Program Files
\Program Files (x86)

RiskReversal -ve bid / +ve ask

Refer to the one-way RR quote in http://bigblog.tanbin.com/2012/06/fx-vol-quoting-convention.html.

Q1: What if 25Delta risk reversal bid/ask quotes are both positive?

As in the above example, dealer (say UBS) gives an RR Ask quote of 3.521%. Let’s say we have some hacker/insider friend to peek at UBS database, and we find the call’s Ask implied-vol is 9.521% and the put’s Bid implied-vol is 6%. In other words, dealer is willing to Write the 25Delta call at an annualized implied-vol of 9% and simultaneously Buy the Put @i-vol of 6%.

Now we ask the same dealer for a bid price. Dealer is bidding 2.8%. Our friend reveals that dealer is secretly willing to Buy the call @i-vol=8.9% (Lower quote) and simultaneously Write the put @i-vol=6.1% (Higher quote).

If you compare the bid vs ask on the call, as market maker the dealer is putting out 2-way quotes to buy low sell high.

If you compare the bid vs ask on the put, as market maker the dealer is putting out 2-way quotes to buy low sell high.

In this scenario, RR bid is below RR ask but both positive.

Q2: Could an RR bid be negative while the ask positive?

Ok We are serious about Selling an RR. To get a better bid price, we ask Dealer2 (SCB) for a Bid quote. Dealer is bidding -0.2%. Our insider tells us this dealer is willing to Buy the call @i-vol=5.9% and simultaneously Write the put @i-vol=6.1%

Between these dealers, Dealer1 would be the best (highest) bid. Now Dealer1 withdraws its quote. Dealer2 is the only bid. Market best RR bid is now negative.

Q2b: When would be RR bid and ask have opposite signs?
A: I guess when the 2 currencies are almost equal in terms of downside/upside

Q3: what if best RR bid and best RR ask are both negative? I think this is the norm in some currency pairs. Suppose market is bearish on the commodity currency (1st) and bullish on the quote currency (2nd). Treating commodity currency as an asset, Sink insurance costs more than surge insurance. Put premium exceeds Call premium. RR would be negative in both bid and ask.

mortgage is like a callable bond, briefly

The mortgage you take on can be, for educational purpose, compared to a personally issued bond (you as issuer) with a predefined monthly repayment and a fixed interest rate. (The floating interest mortgage is comparable to a floating-interest bond.)

When you refinance at a lower interest, it's similar to a callable-bond issuer exercising the call option and then refinance.

The call option is embedded in the “bond”. The call option allows the issuer/borrower to “buy” back the bond from the lender, thereby ending the contract.

GUI (wpf/swing..) design is more OO

Background: “non-GUI” can mean anything — including but not limited to web, database-centric, MOM, workflow, number-crunching, data transfer/transformation gateway, market-data-driven … To avoid over-generalization and to provide some concrete focus, I’d say let’s look at telecom/business systems. This blog post is about my general observation that GUI systems are “more inherently” object-oriented.

But why bother? Well, if anyone is learning any OO design pattern, the best example is often found in GUI space. GUI architects use more OO design principles than in non-GUI. A serious GUI OO design should. If you want to improve on an OO design for a GUI app, or if you start from scratch, use OO design patterns liberally.

Here are my observations as to why GUI design is more inherently object-oriented —

#1) main characters — there are relatively few objects as Models, ViewModels, Views, timers, singleton collections… Mostly stateful, singleton domain entities. OO design pattern is most useful in such a context.
** transient objects —- In most non-GUI systems most objects are transient (as revealed by a profiler). In GUI, quite a few objects — not just the jcomponents, listeners and events — have well-defined life cycles from creation to destruction. Can you say the same about non-GUI apps? In non-GUI, there are rather few long-living domain objects.
** Domain objects —- (More than in non-GUI apps) majority of classes in a GUI system including the main characters and numerous non-visual objects map nicely into domain objects. This is a boon to OO designers, with profound consequences. OO analysis is more worthwhile, more natural and more justified.
** ownership – in GUI an object is often “owned” by another in a strict sense. Given the “main characters”, ownership is often well-defined.
** complex interaction – there’s more inter-object cooperation and interaction in GUI systems. In some methods, you easily identify some of the “main characters”.

– necessity —- In many non-GUI projects, sometimes you can make do without OO. OO is more necessary (and natural) to GUI projects. I think GUI developers often grow into serious OO designers.
– state — visual components (often singletons) are first-class objects in the purest sense. Each visual object needs a supporting cast of other objects such as the Model or ViewModel objects. They all have a large number of well-defined states (properties).
– modular —- OO is at its best if you need to divide up a big project into modules each for a different developer. Large GUI projects often have natural demarcations and well-defined modular interfaces. Non-GUI large OO projects have a few clear demarcations + many more unclear demarcations.
– Reusable component —- GUI system can use/create many reusable components. Plug-n-play.
** EJB were hyped as shrink-wrapped commercial components but I don’t think they are. GUI components are the best example of commercial reusable OO components.

– actors —- are more prevalent in GUI Entity-Relation design. GUI OO design includes a real “actor”, and long-living domain objects associated with the actors. Other OO designs seldom have one. Web site is a different story — the domain objects related to a “session” are often short-lived.
– asynchronous, message driven —- such systems are more amicable to OO modelling, partly because each sender/receive maintains its own state.
– Sequence diagram —- useful to event-driven GUI.
– STA —- A fundamental reason why GUI systems use “Single-Threaded Apartments” is the long-living domain objects. Need to protect their “consistency”
– Encapsulation —- (and Object State) Most important GUI objects have well-defined, often with standardized stateful fields. Not so in non-GUI.
– abstract interfaces —- many classes must support the same “interface”. We often need to treat the a bunch of Types the same way. This calls for interfaces.
– Inheritance
– Design patterns —- map nicely to GUI OO, more nicely than non-GUI OO. See http://bigblog.tanbin.com/2011/01/swing-1-primary-design-pattern.html for a list of design patterns in Swing

TWAP – basics

TWAP has multiple meanings

1) twap is name of an Execution benchmark
2) twap is name of a trading algo that aims to beat twap benchmark

One site says — The algo breaks up a large order and releases dynamically determined slices of the order to the market using evenly divided time slots between a start and end time. The aim is to execute the order close to the average price between the start and end times thereby minimizing market impact.

Another site says — Executes an order evenly over a user defined time period. Unlike VWAP, scheduling is done on an even basis rather than trying to anticipate the volume curve of the period as VWAP does. The most common use of TWAP is for distributing big orders throughout the trading day. For example let’s say you want to buy 100,000 shares of Morgan Stanley. Putting one such a big order would vastly impact the market and the price most likely would start to raise. To prevent that, investor can define time period in TWAP Strategy over which they want to buy shares. It will slice evenly big order into smaller ones and execute them over defined period.

Higher risks of market impact when market volume is ultra thin but our strategy continues to send the same order size at the same pace as before — our orders would likely become large orders causing deep impact.

Can work well as an alternative to VWAP over short periods, particularly into the close. Basic illustration of vwap vs twap benchmark — Suppose there are only two trades during 10-11am:

– 100 shares for $10 at 10am
– 10000 shares for $11 at 10:30am

* The TWAP is $10.50 because the last traded price was at $10 for 30 minutes and at $11 for 30minutes
* The VWAP is $10.99 — a more realistic reflection of the price one would likely get

https://empirica.io/blog/twap-strategy/ describes intra-day TWAP formula


SOAP serialization (to xml) – c#

Suppose a c# serializable class has 10 fields, and 3 of them are string fields. When we populate the 10 fields, we must say xxxSpecified=true on the 7 non-string fields, otherwise xml will not include them. If we don’t populate the 3 string fields and don’t set xxxSpecified on the other 7, then entire object is silently skipped. I spent many hours discovering this rule.

This is part of the standard c# serialization behaviour. I guess it’s standard for SOAP. http://open.convio.com/webservices/apidoc/concepts/visual_studio.html says

“To distinguish between null elements that should be specified as nil and elements that should simply be omitted, the standard XML serializer generated by the .NET framework adds a special flag to each non-String attribute. This flag must be set to true for the attribute to be serialized as part of a SOAP message”

Simple rule — if generated serializable class show xxxSpecified properties, then better use them.

back-to-back ccy swap, basics

(see also P198 [[Marki]])


Both Investopedia and Wikipedia authors pick out this one motivation as the main usage of currency swap.

http://www.investopedia.com/terms/c/cross-currency-swap.asp — The reason companies use cross-currency swaps is to take advantage of comparative advantages. For example, if a U.S. company is looking to acquire some yen, and a Japanese company is looking to acquire U.S. dollars, these two companies could perform a swap. The Japanese company likely has better access to Japanese debt markets and could get more favorable terms on a yen loan than if the U.S. company went in directly to the Japanese debt market itself.

Wikipedia has a similiar comment — Another currency swap structure is to combine the exchange of loan principal with an interest rate swap. In such a swap, interest cash flows are not netted before they are paid to the counterparty (as they would be in a vanilla interest rate swap) because they are denominated in different currencies. As each party effectively borrows on the other’s behalf, this type of swap is also known as a back-to-back loan.

Wikipedia article later cites back-to-back as one of the 2 main uses of currency swaps.

VBA variables lifespan ^ scope

If you dislike perl’s strictVars, then skip —

OptionExplicit must be declared in each module. Like perl strictVars. VBE -} menubar -} tools -} options -} editor tab -}RequireVariableDefintion would insert OE in every new module.

–var Scopes
* procedure-scope, where “procedure” also includes functions. Remember the “Locals” window.
* module-scope? shared among procedures within the module. Less popular
* public? global, across modules.
* There might be other scopes but less used. I’d say procedure scope and public are the most used.

–var lifespan is a less commonly used feature.
Look at “staic” variables — just like C.

When a macro completes, static/module/public variables will keep the residual value whereas procedure variables are destroyed.

pthread-based thread pool – full source code

On my Ubuntu laptop, i tested the source code bundled with [[pthreads programming]]. I once spent an hour studying the source code.

  • uses malloc/free, not new/delete
  • uses a struct to describe each task, with a pointer to a void func returning void, taking in a void ptr
  • I have problem compiling it in g++, but gcc worked.
  • use a request queue (task queue?) A single Producer adds tasks to it; multiple consumers (pool threads) remove them.
  • uses 3 condVars to ….
  • uses a single mutex

Which files have “substance”? Only the tpool.c. The header file contains no implementation. The test code is like unit test. The bugtest is optional.

comment in DOS batch files

REM (safer than labels) may be used mid-stream within a command line. 

In contrast, Single colon or double colon Labels should always start at the first non-whitespace character in a command line.

REM Comment line 1
REM Comment line 2
:: Comment line 3
:: Comment line 4

are all allowed. However, this is bad --
See also http://www.robvanderwoude.com/comments.php

y mkt-makers do delta hedge — my take again

A1: earn bid/ask spread
A2: Well the same kind of problem as the unhedged long call — insufficient insulation.

Q: why do vol market makers take the trouble to maintain a dynamic hedge?

Say I am long call (IBM for eg). Tiny rises are welcome, but tiny drops hurt pnl. As a MM (i.e. market maker), i need to minimize any negative impact to my pnl, which threatens to destabilize me, and the SS (i.e. sell side) infrastructure.

Therefore on the SS we must immediately delta-hedge our long call, usually by short stock. Net delta = 0. This insulates us from both positive and negative impacts. (But then what’s our profit model? [Q1])

Most of the time during a single day IBM would move relatively slowly and by relatively small magnitude, so our initial delta hedge should suffice, but next day we should probably re-hedge or “dynamic hedging”. Key question — What if we don’t?

Say our initial call delta is +44, so initial hedge short 44 shares and achieve 0 delta. Next day our call delta becomes +55 [1], net delta is +11. (What’s the problem? [Q2]), so we short more stocks.

The nice thing (due to long gamma) is, we Sell IBM when it rises, and Buy when it drops. However, Bid/ask spread [2] and commission hurts the profit. Nevertheless, this is a standard SS strategy — long gamma and delta hedge dynamically.

Doesn’t work if we are short gamma.

[1] Over a longer period such as a day, underlier move is often too big for our delta hedge. (Big foot for small shoe).

[2] Note we PAY the bid/ask spread as market taker. Alternatively, it would be perfect if we hedge-sell/hedge-buy the stock using limit orders, thereby earning the bid/ask spread.

dynamic glass-drop #BNP

http://en.wikipedia.org/wiki/Dynamic_programming#Egg_dropping_puzzle is a similar but more ambiguous problem.

If we toss a wine glass out of a 3rd floor window it may break, but it may not if tossed from a 2nd floor window.

Q: with exactly two wine glasses taken from a (Japanese:) production line, you want to identify starting at exactly which level of a 11-floor tower this brand of glass will break if tossed out of the window. Equivalently, determine the highest safe floor. Optimization goal — minimize worst-case cost. Top floor is known to break, so answer might be 1st floor, 2nd floor,… or top floor — 11 possible answers, but 10 floors to test.

Needless to say, if the glass breaks at 6th floor, it will surely break at a higher level.

—- Analysis —-
With just a single glass, you have no strategy. You have to try from 1st floor up. If your classmate starts on 2nd floor and breaks her only glass there, she is disqualified because with no more glass to use, it’s now impossible to know whether it would survive1st floor.

I feel this is NOT a probability problem, so each toss is bound to pass or fail with 100% certainty but we just don’t know it.

We don’t want to toss first glass @2nd, @4th, @6th… because too many first-glass tests. Neither do we want to toss first glass @5th because if broken, then 2nd glass must try @1@2@3@4. Therefore first glass toss should be somewhere between ( @2, @5 ) exclusive.

—-%% solution, with heavy use of symbols —–
I feel this problem is tractable by dynamic programming. Let
* L denote the highest _safe__ floor known so far, and
* U denote the lowest _unsafe_ floor known so far. U is initially at top floor, and L is initially 0, not 1
* u denote U-1

u >= L is an invariant. The Upper and Lower bounds. (u-L) is the maximum number of floors to test. When we get u-L = 0 and final Answer is the current value of U.

Here’s a typical example
6 —- U known unsafe
5 —- u, so 3 floors to test (u-L)
2 —- L known safe

z8 denotes the worst case # tests required to locate the Answer with both glasses when u-L = 8 i.e. 8 floors to test. Could be z(L=0, u=8) or z(L=1,  u=9). Same thing.

For example, z(1,3) means worst case # tests when 1st and 4th storeys need no testing. Note z(1,3) == z(2,4) == z2

z1 = 1 i.e. one toss needed
z2 = 2
z3 = 2 // B! C || BA. The untested floors are named (upward) ABCDEFG (if 7 floors to try), and trailing ! means broken.

z4 = 3 // B! CD || BA
z5 = 3 // C! +2 || C then z2 // if C broken, then must test last glass at D and E
z6 = 3 // C! +2 || C z3  // ABCDEF
z7 = 4 // (D! + 3 || D z3) or (C! + 2 || C z4)
z8 = 4 // (D! + 3||D z4) or the smarter (C! + 2||C z5)
z9 = 4 // (D! + 3||D z5) or (C! + 2 || C z6)
z10 = 4 // (D!+3||D z6). Can’t use (C! + 2 || C z7) — for a 11-storey tower, we need 4 tosses.

I think there are cleverer implementations. This is not brute force. It builds up useful results with lower z() values and use them to work out higher z() values — dynamic programming.

This solution not only finds where to start the test and the worst-case # tests. It also works out the exact decision tree.

–Kyle’s insight

For a build of 100, the max cost is $14 i.e. 14 drops, so the first floor to try is 14th. I didn’t believe him, so he showed

  • if glass AA breaks, then subsequent max cost is $13 as BB must try floor 1,2,3…13. Total worst cost = $14
  • else AA would try 14+13=27th floor. If AA breaks, then subsequent max cost is $12 as BB must try #15,#16,..#26. Total cost is still $14
  • else AA would try 27+12=39th floor. If AA breaks, then subsequent max cost is $11. Total cost is still $14..
  • …AA’s 10th try is #95, at a $10 cost
  • On the 11th try, AA will try #99. If AA breaks, then subsequent max cost is $3 i.e. #96,97,98. Total cost is $14
  • else on the 12th try, AA will try #100. Total cost is $12

https://brilliant.org/wiki/egg-dropping/ explains the math x(x+1)/2=100 so x = 13.65, so firs floor to try is #14.



dynamic poker game in Zhou Xinfeng book

Q: you are dealt the 52 cards in a random sequence.
Each red card earns you $360
Each black card costs you $360
Game ends at end of the 52 draws or any time you quit. How do you optimize your earning and how much is admission ticket worth?

Let’s denote the amount of money you take home as H, a random variable. Your net profit/loss would be H minus admission price. If 555 reasonable/intelligent people play this game, then there would be five hundred and fifty-five values of H. What’s the average? That would be the answer.

p or “+” denotes the # of positive cards earned so far
n or “-” denotes the # of negative cards suffered so far

Exp(H|p=25,n=26) = Exp(H|p=26,n=26) = $0.
There’s an intrinsic value in our hands, Defined as i=(p-n). The e-value or Extrinsic value Evaluated from Expectation, may be higher or lower. Whenever i-value > e-value, we should exit. This is our strategy/policy.

Whenever intrinsic value <$0, E(H) is out of the money but always non-negative because we can wait till maturity and get all 52 cards.

E(p24,n26) = p(next is p)E(p25,n26) = 100%E(p25,n26) = $0
E(p26,n25) = $360     because we will exit

E(p25n25) = Prob(next is p)E(p26,n25) + Prob(next card is n)E(p25,n26) = 50%$360+0 = $180
E(p24n25) = p(p)E(p25,n25) + p(n)E(p24,n26)= p(p)E(p25,n25)+$0 = 66%$180= $120
E(p25n24)= p(p)E(26,n24)+p(n)E(p25,n25)= 33%$360×2 + 66%$180= $360

E(p24,n24)= half of E(25,24)+E(24,25)= half of  $360+$120= $240
E(p23,n25)= p(p)E(24,25)
+ p(n)E(p23,n26) # $0
=3/4x$120= $90
E(p23,n24)= p(p)E(24,24)+p(n)E(23,25)= 3/5 .$240+2/5 .$90

Baye’s theoreom, my first learning note

Q: What’s the relationship between p(A|B) and p(B|A)? This looks rather abstract. It’s good to see an example but …

A: Baye’s theoreom lets us compute one from the other.

p(A|B) = p(B|A) p(A) / p(B)

In practical problem solving, you often need to expand
– p(B) i.e. the denominator, using the conditional probability tree and law of total probability
– p(A) part of the numerator, using the conditional probability tree and law of total probability

2kinds@essential developer tools #WallSt+elsewhere

Note: There are also “common investigative” tools, but for now i will ignore them. Reasons is that most developers have only superficial knowledge of them, so the myriad of advanced features actually fall into #2).

Note: in the get-things-done stage, performance tools are much less “useful” than logic-revealing tools. In my experience, these tools seldom shed light on perf problems in DB, java, or MOM. Performance symptoms are often resolved by understanding logical flow. Simplest and best tools simply reveal intermediate data.
I think most interview questions on dev tools largely fall into these two categories. Their value is more “real” than other tools.

1) common tools, either indispensable or for productivity
* turn on/off exception breakpoint in VS
* add references in VS
* resolve missing assembly/references in VS
* app.config in VS projects
* setting classpath in IDE, makefile, autosys…
* eclipse plugin to locate classes in jars anywhere in windows
* literally dozens of IDE pains
* cvs,
* maven, ant,
* MS excel
* junction in windows
* vi
* bash
* grep, find, tail
* browser view-source

2) Specialized investigative/analytical/instrumentation tools — offers RARE insights. RARELY needed but RARE value. Most (if not all) of the stronger developers I met have these secret weapons. However, most of us don’t have bandwidth or motivation to learn all of these (obscure) features, because we don’t know which ones are worth learning.
* JMS — browser, weblogic JMS console…
* compiler flags
* tcpdump, snoop, lsof
* truss
* core dump analysis
* sar, vmstat, perfmeter,
* query plan
* sys tables, sp_lock, sp_depend
* set statistics io
* debuggers (IDE)
* IDE call hierarchy
* IDE search
* thread dump
* jvmstart tools — visualgc, jps,
* bytecode inspector, decompiler
* profilers
* leak detector
* jconsole
* jvmstat tools — visualgc, jps,..
* http://wiki.caucho.com/Category:Troubleshooting
* any tools for code tracing Other productivity skills that no software tools can help:
* log analysis
* when I first came to autoreo, i quickly characterized a few key tables.

COM api argument passing VBA to c#

Context – a c# COM addin exposes a “service” or API method callable from VBA. VBA passes an excel range object into the api. 2 ways to pass the object.

1) submit (theRange) – c# gets a 2D array of Objects

2) submit theRange – c# gets a “System._ComObject” instance. You need to cast it to a Microsoft.Office.Interop.Excel.Range object. Your debugger may not show the content of the object, but you can read all the data content.

2-headed coin – inherent "pool" distribution

Q1: Given a suspicious coin. You toss 5 times and get all tails. You suspect Tail is more than 50% chance. What’s a fair estimate of that chance (K)?

Analysis: The value of K could be 51%, 97%, even 40%, so K is a continuous random variable whose PDF is unknown. What’s a fair estimate of K? The Expected value of K? So the task is to estimate the PDF.

Might be too tough. Now look at a simpler version —

Q2: A suspicious coin is known as either fair (50/50) or 2-headed. 10 tosses, all heads. What’s the probability of unfair? You could, if you like, imagine that you randomly pick one from a mixed pool of fair/unfair coins.

Q2b (equivalent to Q2): Given a pool of 3600 coins, each being either fair or 2-headed. You pick one at random and throw 5 times. All heads. What’s the probability of unfair?

If you assume half the coins 2-headed then see answer to Q3b. But for now let’s assume 36 (1% of population) are 2-headed, answer is much Lower.
U := unfair coin picked.
A := 5 tosses all head
P(U|A) is the goal of the quiz.

P(U|A) = P(U & A)/P(A). Now P(U&A) = P(U) = 1% and P(A) = 1% + 99%/2^5 = 1% + 99%/32 ~= 4%. Therefore,
P(U|A) ~= 25%.

The answer, the estimated probability, depends on the underlying pool distribution, so to estimate P(U|A), we must estimate that distribution P(U). Note the notations — P(U) denotes pool-distribution inherent in the pool. P(U) is an unknown we are guessing. The goal of the quiz P(U|A) is an “educated guess” but never equal to the “guess” unless P(A)=100%. In fact, we are not trying to estimate P(U). Goal is P(U|A), a completely different entity.

Some say as we do more experiments our educated guess will get more accurate, but I don’t think so.

See http://bigblog.tanbin.com/2012/10/10-heads-in-row-on-fairunfair-coin.html.

Q3: Given 2 coins, 1 of them have no tail (i.e. unfair coin). You pick one and toss it 5 times and get all heads . What’s the probability you picked a Unfair coin?
Much easier. I think P(U|A) = 32/33. Here’s the calc

U := unfair coin picked.
A := 5 tosses all head

P(U|A) = P(U & A)/P(A). Now P(A) = 1/2 + 1/64 and P(U&A) = P(U) = 1/2.

Q3b (equivalent?) Given a pool of 3600 coins, half of them 2-headed. You pick one at random and throw 5 times. All heads. What’s the probability of fair?

design a param as 1)non-type template arg ^ 2)ctor arg

Q: how do you choose to make “length=5” a ctor arg or a non-type template argument? For a context, you can imagine a custom array class template (D Duffy)

I feel non-type template arg was invented for a practical need but that history is not important. I feel the philosophical or technical reason for it is less important than the actual usage in practice, which I have not seen many. My rule is to avoid it and favor the more familiar ctor arg unless we see a compelling reason AND a proven usage pattern.

However, for self-practice we should feel free to play with it.

I believe [[Alexandresc]]  covers this.

xaml pretty print

#) You can try the imperfect Format Document command from the Edit->Advanced menu to nicely format your XML document. (http://sidersdd.wordpress.com/2008/04/21/format-xml-in-visual-studio/)

#) http://www.softlion.com/webTools/XmlPrettyPrint/default.aspx is good

#) http://www.corefiling.com/opensource/xmlpp.html can sort attributes but unfortunately removes indents on xaml comments and converts double-quote to single-quote

xmlprettyprint.com is less precise about indent

notepad++ HTML-tidy is broken

vwap exec-algo is non-trivial

See other posts on ExecutionRisk.

Some friends say vwap is the simplest trading algorithm, compared to most algorithms. I am newbie. I feel vwap is not a no-brainer that you can master in an hour.

2 datetimes — formula can apply between any 2 datetimes, but by default is [StartOfDay – now]

VWAP has 3 meanings —
1) vwap is name of a live soft mkt datum displayed as a live creeping curve similar to a “moving average price of last 3 trades”. VWAP is averaged by volume, and averaged over all trades since SOD (on that cusip). Do we include just sells? Well, each buy trade matches exactly one sell.

2) vwap is name of a common Execution Benchmark when a sell-side receives a block order from a buy-side
** guaranteed VWAP execution.
** Say you had a sizeable buy order executed below average price. Well done, but how do you define “below average price”? Default definition is vwap. Arrive Price is another definition.

Suppose a buy-side has a large (500,000 shares) block sell – tough job. A sell side (usually a i-bank) typically alleviates the buy-side’s problem immediately by buying the shares wholesale from the buy-side, and then selling them off later. The contract price might be the next 30 days’ vwap Minus 1 cent, which is 1 cent WEAKER than vwap. With a block-Sell, it’s hard to guarantee a price Above vwap; it’s more achievable to guarantee a price Below vwap.

3) vwap is name of a standard execution algo for Trading machines
** impact – minimal market impact. This is an advantage of vwap strategies. Many passive traders prefer vwap strategies. (But large order may not get fully filled)
** The other side of the coin — execution risk is high with VWAP

** price, quantity, timing, LO/MO, when to cancel — the key output of this or any algo. Here are Some of the trading tips
**** empirically, to beat vwap benchmark, you need good forecast of intraday volume pattern
**** rule of thumb — trade most heavily when markets are most liquid.
**** Note there’s usually a desire to finish working the block order within a time-frame, rather than indefinitely. If indefinitely or order is small, you can beat vwap benchmark by using limit orders
**** Note using limit orders earns us the b/a spread while using mkt orders costs us the spread but is faster.
**** if you want to aggressively participate in current volume and avoid missing the wave, then use MO rather than LO.
**** block traders sometimes divide the given timeframe into, say, 10 bins and allocate more/less than 10% of the block to each bin according to volume forecast, and trade according to the plan. Just like project planning and execution.
**** Note minimizing mkt impact is one of the top 3 criteria. If you sell first part of a block-Sell with massive impact, price would stay high for the rest of the day and you can’t complete the order.