Practice

Time limit1sMemory limit128 MB

Summary
Given pairs (n, w), fit a logistic regression by maximizing the product of per-observation likelihoods and report the slope and intercept to four decimals.
Level

Medium7 of 10

Topics
Math, Probability, Binary search, Implementation
Solved
No attempts yet

Problem

We want to model how a team's chance of winning a programming contest depends on how much it has practiced.

Let pp be the probability that a given team wins a given contest, and let nn be the number of practice problems the team solved before the contest. We assume the two are related by the logistic model

log⁡p1−p=a+b n\log\frac{p}{1-p} = a + b\,n

for some constants aa and bb. Your task is to find the aa and bb for which this model best fits a set of observed results.

Each observation is a pair (n,w)(n, w): nn is the number of practice problems a team solved before a contest, and ww is 11 if the team won that contest and 00 otherwise.

Given aa, bb, and nn, the model yields pp, the estimated probability that w=1w = 1. The likelihood of a single observation is pp when w=1w = 1 and 1−p1 - p when w=0w = 0; the likelihood of a set of observations is the product of the likelihoods of the individual observations.

Compute the maximum-likelihood estimate of aa and bb — the values that maximize the likelihood of the given set of observations.

Input

The input contains several test cases, followed by a line containing a single 00.

Each test case begins with an integer kk (1<k≤1001 < k \le 100), the number of observations that follow. Each of the next kk lines contains two integers nn and ww (0≤n≤1000 \le n \le 100, 0≤w≤10 \le w \le 1). Within each test case there are at least two distinct values of nn and at least two distinct values of ww.

Output

For each test case, print a single line containing aa and bb, each rounded to four digits to the right of the decimal point.

Examples1

  1. Example 1

    Input
    20
    0 0
    0 0
    0 0
    0 0
    1 0
    1 0
    1 0
    1 1
    2 0
    2 0
    2 1
    2 1
    3 0
    3 1
    3 1
    3 1
    4 1
    4 1
    4 1
    4 1
    0
    
    Expected output
    -3.1748 1.5874