MATLAB Newsgroup

I am a lerner in Stata and have some major problems replicating results from a paper.

The Lagrangian was differentated and reformulatet, gives:

(ni. - nii)/(1-pi) +(n.i - nii)/(pi) +lamda = 0 für alle i = 1,...,20

and Sum (pi) =1

n.i = sum row

ni. = sum column

nii = diagonal entries

Pii = cell-percentage of row-sum

I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.

For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance

Here i add the part of the paper i want to replicate :

http://img340.imageshack.us/img340/9861/paperu.png

> Hello

>

> I am a lerner in Stata and have some major problems replicating results from a paper.

>

> The Lagrangian was differentated and reformulatet, gives:

>

> (ni. - nii)/(1-pi) +(n.i - nii)/(pi) +lamda = 0 für alle i = 1,...,20

>

> and Sum (pi) =1

>

> n.i = sum row

> ni. = sum column

> nii = diagonal entries

> Pii = cell-percentage of row-sum

>

> I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.

>

> For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance

>

> Here i add the part of the paper i want to replicate :

>

> http://img340.imageshack.us/img340/9861/paperu.png

what have YOU done so far to solve YOUR particular problem...

(after all, it is YOUR thesis...)

us

> "Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0nnmc$cp2$1@fred.mathworks.com>...

> > Hello

> >

> > I am a lerner in Stata and have some major problems replicating results from a paper.

> >

> > The Lagrangian was differentated and reformulatet, gives:

> >

> > (ni. - nii)/(1-pi) +(n.i - nii)/(pi) +lamda = 0 für alle i = 1,...,20

> >

> > and Sum (pi) =1

> >

> > n.i = sum row

> > ni. = sum column

> > nii = diagonal entries

> > Pii = cell-percentage of row-sum

> >

> > I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.

> >

> > For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance

> >

> > Here i add the part of the paper i want to replicate :

> >

> > http://img340.imageshack.us/img340/9861/paperu.png

>

> what have YOU done so far to solve YOUR particular problem...

> (after all, it is YOUR thesis...)

>

> us

Sorry, you are right of course, i didn't write what i have so far:

clear all

A = [.....]

B = [.....]

C = [.....]

x0 = [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]

for ind =1:15

nii=A(ind)

n_i=B(ind)

ni_=C(ind)

fun = @(x) [(nii-ni_)/(1-x) + (nii-n_i)/x - L, sum(x)-1]

x(ind,: ) = fsolve(fun,x0)

I dont know if any of this code is useable. And there are obviously some parts missing.

Now my problem is still, how can i specifie the L to be a constant for every i ? and it seems that the fsolve does only solve for one variable. But i have 2 Variables x and L.

Any tipps would be appreciated. Thank you

> > "Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0nnmc$cp2$1@fred.mathworks.com>...

> > > .......

> > > (ni. - nii)/(1-pi) +(n.i - nii)/(pi) +lamda = 0 für alle i = 1,...,20

> > >

> > > and Sum (pi) =1

> > >

> > > n.i = sum row

> > > ni. = sum column

> > > nii = diagonal entries

> > > Pii = cell-percentage of row-sum

> > >

> > > I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.

> > >

> > > For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance

> > >

> > > Here i add the part of the paper i want to replicate :

> > >

> > > http://img340.imageshack.us/img340/9861/paperu.png

> .......

> clear all

> A = [.....]

> B = [.....]

> C = [.....]

> x0 = [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]

>

> for ind =1:15

> nii=A(ind)

> n_i=B(ind)

> ni_=C(ind)

> fun = @(x) [(nii-ni_)/(1-x) + (nii-n_i)/x - L, sum(x)-1]

>

> x(ind,: ) = fsolve(fun,x0)

>

> I dont know if any of this code is useable. And there are obviously some parts missing.

>

> Now my problem is still, how can i specifie the L to be a constant for every i ? and it seems that the fsolve does only solve for one variable. But i have 2 Variables x and L.

>

> Any tipps would be appreciated. Thank you

- - - - - - - - -

There are at least three things wrong with your anonymous function fun. First the expression in brackets must be a vector expression, not something that is done-one-at-a-time in a for-loop. Second, the division operators need a dot so as to avoid matrix division. Finally, you need to include the unknown L (which I presume is your -lambda) as one of the elements of vector x to be evaluated, say the last one. That means if k is the k of the paper you referenced, then x should have a length of k+1.

Let A and B be row vectors of length k and defined for each i as

A(i) = ni. - nii

B(i) = n.i - nii

in your earlier notation. Then do:

fun = @(x) [A./(1-x(1:k))+B./x(1:k)-x(k+1),sum(x(1:k))-1]

x = fsolve(fun,x0);

Also be sure to include an initial estimate for L in the k+1 element of x0.

However, I would predict that fsolve might have difficulty successfully solving the equations in this particular problem. My reasoning goes something like this. For each particular value of L, the i-th equation

A(i)/(1-x(i))+B(i)/x(i) = L

is equivalent to a quadratic equation in x(i) (where I am assuming A(i) and B(i) are always positive quantities since they derive from "n-counts".) There are two real roots, one root, or none, to this quadratic depending on whether

sqrt(A(i))+sqrt(B(i))

is less than, equal to, or greater than L, respectively. If L is greater than the maximum of this sum of two square roots for all i, then all k equations would have two roots, so there would be 2^k = 32,768 possible cases which fsolve would have to "investigate" for such L values to make sure it didn't overlook a possible solution to sum(x(1:k))-1=0. It looks like a very ugly problem when looked at from that point of view, and I can conceive of fsolve getting hopelessly stuck in these numerous blind alleys.

I can envision a process by which a single solution can certainly be found, but I see no guarantee that it would be the only possible one. It can easily be seen that as L approaches infinity, one of the roots of each of the above quadratics must approach zero while the other root must approach one. Start with a very high value of L and choose in each case the smaller of the two possible roots for x(i). If L is sufficiently large the sum of these x(i) would be less than 1. Now imagine L decreasing continuously unless reaching the greatest

sqrt(A(i))+sqrt(B(i))

at an i=i0, which would be the lowest value that L can be allowed to have. The corresponding sum(x(1:k)) would continuously increase as the roots increase. If it passes through one somewhere along the line, there is your solution. If not, then let L commence increasing again from this minimum value but choosing the greater of the two possible roots for the i0 case while the other roots remain at their lesser values. This would be a continuous process for sum(x(1:k)) with no discontinuous jumps, since you would have passed through the one-root point at i0 when making the switch in roots. Since x(i0) would now approach one as L again approaches infinity, there surely must come a point where sum(x(1:k)) crosses one, and there would be your solution.

In other words, there must always exist a solution in which either all the x(i) roots are chosen as the lesser of a pair, or else there is a solution in which just one of the x(i) is the greater of a pair and all the other roots are still at their lesser values.

However, making the transition for any other of the root pairs from the lower to the upper one would in general involve a discontinuous jump in sum(x(1:k)), so we are not sure any other solutions can be found in the L-varying process. If they do exist I foresee fsolve having a difficult time finding them.

As for the single "sure-fire" solution above, it should be possible to translate that process to something fzero, rather than fsolve, could solve in terms of the single unknown L. It can be determined in advance whether sum(x(1:k)) is above or below one for L chosen at sqrt(A(i0))+sqrt(B(i0)), and this would allow you to determine which of the above two alternatives to select for the x(i0) root in starting fzero with a pair of surrounding estimates for L.

My apologies for not going into greater detail in the previous paragraph with respect to fzero. I could not be sure you would be interested in such an approach.

Roger Stafford

> "Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0q8lh$5v8$1@fred.mathworks.com>...

> > > "Hobst Friedli" <tobias.friedli@access.uzh.ch> wrote in message <i0nnmc$cp2$1@fred.mathworks.com>...

> > > > .......

> > > > (ni. - nii)/(1-pi) +(n.i - nii)/(pi) +lamda = 0 für alle i = 1,...,20

> > > >

> > > > and Sum (pi) =1

> > > >

> > > > n.i = sum row

> > > > ni. = sum column

> > > > nii = diagonal entries

> > > > Pii = cell-percentage of row-sum

> > > >

> > > > I now want to solve this for the Pi's, probably with fmincon. But i just dont get how to specifie the commands.

> > > >

> > > > For every kind of help i would be very grateful... I need to do this estimation for my bachelor thesis and i cant find help anywhere... Thank you very much in advance

> > > >

> > > > Here i add the part of the paper i want to replicate :

> > > >

> > > > http://img340.imageshack.us/img340/9861/paperu.png

> > .......

> > clear all

> > A = [.....]

> > B = [.....]

> > C = [.....]

> > x0 = [0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1]

> >

> > for ind =1:15

> > nii=A(ind)

> > n_i=B(ind)

> > ni_=C(ind)

> > fun = @(x) [(nii-ni_)/(1-x) + (nii-n_i)/x - L, sum(x)-1]

> >

> > x(ind,: ) = fsolve(fun,x0)

> >

> > I dont know if any of this code is useable. And there are obviously some parts missing.

> >

> > Now my problem is still, how can i specifie the L to be a constant for every i ? and it seems that the fsolve does only solve for one variable. But i have 2 Variables x and L.

> >

> > Any tipps would be appreciated. Thank you

> - - - - - - - - -

> There are at least three things wrong with your anonymous function fun. First the expression in brackets must be a vector expression, not something that is done-one-at-a-time in a for-loop. Second, the division operators need a dot so as to avoid matrix division. Finally, you need to include the unknown L (which I presume is your -lambda) as one of the elements of vector x to be evaluated, say the last one. That means if k is the k of the paper you referenced, then x should have a length of k+1.

>

> Let A and B be row vectors of length k and defined for each i as

>

> A(i) = ni. - nii

> B(i) = n.i - nii

>

> in your earlier notation. Then do:

>

> fun = @(x) [A./(1-x(1:k))+B./x(1:k)-x(k+1),sum(x(1:k))-1]

>

> x = fsolve(fun,x0);

>

> Also be sure to include an initial estimate for L in the k+1 element of x0.

>

>

> However, I would predict that fsolve might have difficulty successfully solving the equations in this particular problem. My reasoning goes something like this. For each particular value of L, the i-th equation

>

> A(i)/(1-x(i))+B(i)/x(i) = L

>

> is equivalent to a quadratic equation in x(i) (where I am assuming A(i) and B(i) are always positive quantities since they derive from "n-counts".) There are two real roots, one root, or none, to this quadratic depending on whether

>

> sqrt(A(i))+sqrt(B(i))

>

> is less than, equal to, or greater than L, respectively. If L is greater than the maximum of this sum of two square roots for all i, then all k equations would have two roots, so there would be 2^k = 32,768 possible cases which fsolve would have to "investigate" for such L values to make sure it didn't overlook a possible solution to sum(x(1:k))-1=0. It looks like a very ugly problem when looked at from that point of view, and I can conceive of fsolve getting hopelessly stuck in these numerous blind alleys.

>

> I can envision a process by which a single solution can certainly be found, but I see no guarantee that it would be the only possible one. It can easily be seen that as L approaches infinity, one of the roots of each of the above quadratics must approach zero while the other root must approach one. Start with a very high value of L and choose in each case the smaller of the two possible roots for x(i). If L is sufficiently large the sum of these x(i) would be less than 1. Now imagine L decreasing continuously unless reaching the greatest

>

> sqrt(A(i))+sqrt(B(i))

>

> at an i=i0, which would be the lowest value that L can be allowed to have. The corresponding sum(x(1:k)) would continuously increase as the roots increase. If it passes through one somewhere along the line, there is your solution. If not, then let L commence increasing again from this minimum value but choosing the greater of the two possible roots for the i0 case while the other roots remain at their lesser values. This would be a continuous process for sum(x(1:k)) with no discontinuous jumps, since you would have passed through the one-root point at i0 when making the switch in roots. Since x(i0) would now approach one as L again approaches infinity, there surely must come a point where sum(x(1:k)) crosses one, and there would be your solution.

>

> In other words, there must always exist a solution in which either all the x(i) roots are chosen as the lesser of a pair, or else there is a solution in which just one of the x(i) is the greater of a pair and all the other roots are still at their lesser values.

>

> However, making the transition for any other of the root pairs from the lower to the upper one would in general involve a discontinuous jump in sum(x(1:k)), so we are not sure any other solutions can be found in the L-varying process. If they do exist I foresee fsolve having a difficult time finding them.

>

> As for the single "sure-fire" solution above, it should be possible to translate that process to something fzero, rather than fsolve, could solve in terms of the single unknown L. It can be determined in advance whether sum(x(1:k)) is above or below one for L chosen at sqrt(A(i0))+sqrt(B(i0)), and this would allow you to determine which of the above two alternatives to select for the x(i0) root in starting fzero with a pair of surrounding estimates for L.

>

> My apologies for not going into greater detail in the previous paragraph with respect to fzero. I could not be sure you would be interested in such an approach.

>

> Roger Stafford

Thank you very much for your detailed answer, i really appreciate it and it helps me a lot. I will implement the suggested changes today and see if it gives me useful results. As i have the original data and results from a similar estimation, i can easily verify the results. I see the problem your mentioning.

an other idea was to not use the lamda and the sum(x)=1 at all. My thoungt is, that as long as i do not have a unknown lamda in the equation i dont need the sum=1 equation, the xi are determines by the k equations. If i set lamda =0 or zero (or whatever) the relatiive values for xi should stay the same. Then i can normalize the xi in a second step, by deviding the result vertor by sum(x).

Anyways i will try these approaches, and report back later. And again, thank you for your help.

> .......

> an other idea was to not use the lamda and the sum(x)=1 at all. My thoungt is, that as long as i do not have a unknown lamda in the equation i dont need the sum=1 equation, the xi are determines by the k equations. If i set lamda =0 or zero (or whatever) the relatiive values for xi should stay the same. Then i can normalize the xi in a second step, by deviding the result vertor by sum(x).

> .......

- - - - - - - - - -

I don't think that would give a valid solution. Multiplying all the xi by a normalizing factor could give you a sum xi of one, but in general it would also cause the quantities

(ni_-nii)/(1-xi) +(n_i-nii)/xi

to no longer be equal to one another for the various values of i. Consequently your probability values would then fail to represent a maximum likelihood. Having a common value of L along with a sum xi of one would appear to be a crucial ingredient in finding a valid solution.

After giving your problem more thought it seems likely to me that a solution using all lower quadratic roots is the only one possible. In this case, using fzero with L as the only unknown would surely be the best way of solving your equations. The use of fsolve with many unknowns may end up searching a great many blind alleys.

For the equation A/(1-x) + B/x = L, the lower root is given by

x = (L+B-A-sqrt((L-A-B)^2-4*A*B))/(2*L)

so adjusting the single variable L so that the sum of these lower roots is one, is the task that you could set fzero to solving. It should perform very well if given an appropriate initial x0 interval.

By the way, in my previous article I misstated the value of L which gives just one root in the quadratic. It should have been

(sqrt(A(i))+sqrt(B(i)))^2

instead of just

sqrt(A(i))+sqrt(B(i))

Roger Stafford

You can think of your watch list as threads that you have bookmarked.

You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.

To add items to your watch list, click the "add to watch list" link at the bottom of any page.

To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.

You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.

To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.

To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.

*No tags are associated with this thread.*

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Got questions?

Get answers.

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi test

Learn moreDiscover what MATLAB ^{®} can do for your career.

Opportunities for recent engineering grads.

Apply TodayThe newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.

Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.

Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.

There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.

**MATLAB Central**

You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.

Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.

**One Account**

Your MATLAB Central account is tied to your MathWorks Account for easy access.

**Use the Email Address of Your Choice**

The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.

**Spam Control**

Most newsgroup spam is filtered out by the MATLAB Central Newsreader.

**Tagging**

Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.

**Watch lists**

Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.

- Use a newsreader through your school, employer, or internet service provider
- Pay for newsgroup access from a commercial provider
- Use Google Groups
- Mathforum.org provides a newsreader with access to the comp.soft sys.matlab newsgroup
- Run your own server. For typical instructions, see: http://www.slyck.com/ng.php?page=2