Mathias Brandewinder on .NET, VSTO and Excel development, and quantitative analysis.
by Mathias 28. June 2010 13:14

A client asked me recently a fun probability question, which revolved around figuring out the probability of success of a research program. In a simplified form, here is the problem: imagine that you have multiple labs, each developing products which have independent probabilities of succeeding – what is the probability of more than a certain number of products being eventually successful?

Let’s illustrate on a simple example. Product A has a 30% probability of success, and product B a 60% probability of success. Combining these into a probability tree, we work out that there is an 18% chance of having 2 products successful, 18% + 12 % + 42% = 72% chance of having 1 or more products succeed, and 28% chances of a total failure.

SimpleBinaryTree

It’s not a very complicated theoretical problem. Practically, however, when the number of products increases, the number of outcomes becomes large, fairly fast – and working out every single combination by hand is extremely tedious.

Fortunately, using a simple trick, we can generate these combinations with minimal effort. The representation of integers in base 2 is a decomposition in powers of 2, resulting in a unique sequence of 0 and 1. In our simplified example, if we consider the numbers 0, 1, 2 and 3, their decomposition is

0 = 0 x 2^2 + 0 x 2^1 –> 00

1 = 0 x 2^2 + 1 ^ 2^1 –> 01

2 = 1 x 2^2 + 0 x 2^1 –> 10

3 = 1 x 2^2 + 1 x 2^2 –> 11

As a result, if if consider a 1 to encode the success of a product, and a 0 its failure, the binary representation of integers from 0 to 3 gives us all possible outcomes for our two-products scenario.

More...

by Mathias 2. June 2010 05:43

It seems like all the cool kids are using either Git or Mercurial these days, so I feel like a dinosaur sticking to Subversion and Tortoise for version control. In the meanwhile, I just figured out a small Tortoise trick yesterday.

In my experience, the number one dumb mistake that happens with Subversion is adding a new file in a project, and forgetting to add that new file when committing. To avoid this, before a commit, I right-click on my project, and select add, which shows all the local files that haven’t been added to the repository. The problem is that you get a bazillion files this way, some of them you know you are never going to add, like the Bin and Obj folders for instance.

AddFiles

Easy fix: right-click TortoiseSVN, settings, and you’ll see the following:

TortoiseSettings

The text box “Global ignore pattern” defines what patterns you want to exclude; in my case I wanted to remove bin and obj folders, and ReSharper related files, which typically contain _ReSharper, so I added

bin obj *_ReSharper*

to the list of patterns. Et voila! Once again, I just wish I had taken the time to read the user manual. This type of dumb process detail just takes a few seconds here and there, but adds up over time; I wouldn’t want to know how many hours I spent un-selecting files in this list over the last 5 years…

by Mathias 30. May 2010 11:01

Silence is gold. Or… is it? You may have noticed that VSTO swallows exceptions; that is, if something goes wrong in your add-in code, Office will discreetly carry on as if nothing had happened. Consider the following code:

public partial class ThisAddIn
{
    private int counter;

    private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        this.Application.SheetActivate += SheetActivated;
    }

    private void SheetActivated(object sheet)
    {
        MessageBox.Show("Counter = " + this.counter.ToString());
        throw new ArgumentException("Something went south here.");
        counter++;
    }

The add-in is supposed to maintain a counter of how many times the user has changed the activate sheet. However, a bug throws an exception right before the counter is updated. If you run this code, you’ll see that the MessageBox keeps being displayed every time you change the selected worksheet, but the counter stays firmly at zero, and never gets updated.

More...

by Mathias 3. February 2010 13:25

Activating a worksheet is a fairly common task in Office automation; however, in a VSTO project, if you call:

myWorksheet.Activate();

… building the project will give you the following warning:

Ambiguity between method 'Microsoft.Office.Interop.Excel._Worksheet.Activate()' and non-method 'Microsoft.Office.Interop.Excel.DocEvents_Event.Activate'. Using method group.

I don’t like to have warnings in my projects when I can avoid it, but I never got to look into it. After all, it was “just a warning”, so I let it go.

The answer came to me via the Carter & Lippert VSTO book (aka “The Brick”), which I finally started reading through, and highly recommend. The gist of it is that the Worksheet interface implements 2 interfaces, _Worksheet and DocEvents_Event. _Worksheet contains the properties and methods that correspond to the Worksheet, including the Activate() method, while DocEvents_Event owns the events, including Activate, and these two names collide.

To disambiguate the call, you just need to cast the Workbook to the appropriate interface, the one which owns the method you are interested in. In my case, I want to Activate the workbook, and therefore use the following code:

((Excel._Worksheet)myWorksheet).Activate();

And sure enough, the warning is gone.

by Mathias 6. November 2009 15:48

When I decided to have a 2-level horizontal menu for my professional webpage in ASP.NET, it came as a surprise to me that this wasn’t completely straightforward. I expected the standard  ASP menu control to support this, but found out that this wasn’t the case.

Fortunately, I came across a post by Peter Kellner, describing how he implemented that for the Silicon Valley Code Camp website, which was pretty much what I envisioned.

The one issue I had with his implementation, however, was that the second level menu uses multiple data sources. The Master Page handles the top-level menu, but each page contains a reference to the specific datasource used to populate the sub-menu. As a result, if you decide to add a page, you need to manually add to that page some code to define what sub-menu should show up, which is cumbersome.

The ideal solution for a lazy developer like me would be to have all the menus handled in the Master Page, so that when you add a new page to your website, you just need to add it to the Sitemap, and the right menu and sub-menu shows up.

More...

Comments

Comment RSS