Thursday

Matalb "out of memory" problem


If you are working and coding with Matlab, you probably encounter this problem especially when your code has many iterations or you are dealing with libraries you want to use inside of your code.

Here are some suggestions:

1) The interface of Matlab written in Java so one option could be increasing the heap size of "Java Heap Memory" (File->Preferences->Java Heap Memory).

2) Try "-nodesktop" mode. You need to go to cmd and run Matlab in the directory you want to run your program. Also in your command line type: -nodesktop. You will have just a window (like cmd) to type the code you want to run.

3) If the problem you are encountering is related to the library you are calling or using in your Matlab code, the other option could be using the MEX file. You need to create a MEX file in Matlab using that library. After compiling and creating the MEX file , you can use it like other build-in functions in Matlab. Creating the MEX file sometimes is tricky a little bit but I think its completely worth it to have your own costume function instead of loading the library each time.

4) Use machines with bigger RAMs. Many universities have HPC (High Performance Computing) lab.  The amount of RAM you can get over there is usually huge. In our university I tried to see how much RAM I can get by creating a vary big matrix (5e4*5e4), and I could get almost 21 GB RAM.



I would be happy if you let me know about other methods to deal with this problem.

Friday

Change Blindness and MCMC

You might saw the awareness test in youtube. I like this video and honestly I didn't see the bear in the first time! Right now in my research I'm using MCMC (Markov chain Monte Carlo) method, which is a sampling method and is the family of random walk and the goal is to get the target distribution by sampling. The idea of this method is very simple and first developed by people in Sandia national laboratories. 
You probably used monte carlo method for calculating the integral, now we are sampling the probability distribution (using monte carlo) based on constructing a Markov chain and in the Markov chain the current state is just dependent on previous state. Thats it! easy! 
For sure there are many details included when you want to implement the algorithm but in this post I just would like to talk about the process of our mind which is very close to Monte Carlo method. The way we are seeing the world is base of how we are sampling our surroundings. Actually we are sampling our environment and because of different backgrounds and mind states, people can see different thing when you show them a certain picture! We see things which are more important for us and we fill the rest of the picture by our mind. This ability of our mind helps us to fill the gaps. This is happening in our dreams too when we are asleep, usually we see the story and when we are part of the dream, usually we don't see ourselves, for example you don't see your arms, you are just your eyes and you see the whole thing but not your body (this is true about me ans some of my friends which I had this discussion with them, so its not general!). I'm not sure how our brain works but I just know its completing the portion we don't pay attention. The other example could be this, imaging you are in a meeting with one of your colleagues, you are sitting around a table in a coffee place and waiting for him/her, he/she comes from the front door and he/she joins you and you start talking about his/her weekend, where his/her went, how was it, how much fun he/she had, does his/her want to try these new things this time and so on; 
after the meeting if I ask you some questions you can answer them more correctly, for example: who was his/her weekend? Where did he/she go? or, what kind of coffee he/she had?
But if I ask you what was the color of his/her shoes, you probably cant answer this question, you saw him/her walking to your table and you definitely saw his/her shoes and you are sure he/she didnt come there with barefoot but this information is not important for your mind and when you are sampling the environment you are sampling thing which are more important and you mind fills the other portions, you may say, OK, so what we are seeing is illusion! I'm not sure about that but as far as I know, YES!  But its very high quality illusion!   ;)
There is a term in psychology called Change Blindness. We don't pay attention to changes which are not important for us and fill the rest by our mind (some people learned to improve the data they are capturing in one shot, if you watch some action movies you see spies have this character). There is a good example video here for change blindness.
Ok, lets back to the MCMC, how does this things even related to MCMC? So far we know that we are sampling our environment and we are sampling thing which are more important for us, the MCMC method has the same spirit, we are trying to sample the probability distribution which are more important for us. We want to construct the target distribution with less number of samples and ignore the other part of the sample space. We would like to have the best desired distribution with the perfect number of samples and use that target distribution for further analyze. Sampling methods are very good when the decision space is very big and we are limited by some constraint to see the whole picture. Ill try to have another post about different types of sampling and also different types of MCMC. 

Happy sampling! :)
       

Thursday

Star Wars and Children

When you want to start to learn something new, its very important to start from a good point. Even in optimization the starting point is very important and in many cases will change the solution you will find and also number of iteration you may have. So in many contexts starting point is crucial. Most of us had read many books and had many courses but sometimes we feel that this book is better than that or this professor describing things better than other professors. However, this feelings may vary from person to person and in some cases for some of us some books feels written in another language! O.K. lets not miss the point! 
One of my friends in EPFL is doing some experiments on children to measure level of learning during different periods of childhood and in one of their studies they tried to teach some children who learned music and notes before incorrectly (actually reteach them) and then compare them to children who have no idea about music before and just learned it for the first time. He told me that seems that its more difficult to reteach children who learned the notes inaccurate before and that reminds me the sentence from the Star Wars: "Sometimes you have to unlearn what you have learned!".
Anyways, I reached a very good book about optimization by Prof. Yang which is published in 2010 by Wiley. And as mentioned before it doesn't mean other books are not good, I just can make more connection with this book! 
You can have a look to this book here. I hope you like it!

Continue on Lagrange multipliers

So in the previous post I was talking about Lagrangian Multipliers and I created an exe file to show the objective function with one constraint. You can reach the file here which includes an exe file and a MCR file in case you don't have Matlab installed on your machine (the exe file compiled and runs just on windows machines). You can visualize the function and the constraint with this file and I will show you an example of that.
The example comes from one of my classes (CE 590 - Modeling and Analysis of Civil Engineering Systems) and called OMAGOSH problem.
Here is the problem description: 

"You are an engineer advising the State Department of Natural Resources as it manages Lake Omagosh. The water quality index, x, can be improved, and better fishing would result. Boat launch facilities could also be added (measure in units, y). The benefits of fishing and pleasure boating are 11x+8y-2xy, in million dollars. The costs are x+2y in million dollars. Only 17 million dollars are available. Use the Kuhn-Tucker approach to evaluate this problem. What is the meaning of the value of the Lagrange multiplier for this problem?"

First of all, lets talk abut Kuhn-Tacker conditions. Because we have inequality in our constraint and we would like to solve this problem with Lagrangian approach and since we can solve this problem with Lagrangian method if we have equality, we can change our inequality in our constraint to equality by adding a slack variable to our constraint. 
So, if we have equality constraint we will have something like: g(x,y)=b
but here we have inequality: g(x,y)<= b and in the Omagosh problem: g(x,y)<= 17.
we can change the inequality to equality as mentioned by adding a slack variable: g(x,y)+s=17.
's', here is our slack variable and its worth to mention that 's' is always positive, we can just put 's^2'. 

You can reach the solution I did by hand here and please let me know if you have any question.
  
In this picture you can see the objective function and constraint.

Wednesday

Lagrange Multipliers

If you are familiar with Lagrange multiplier you are lucky! You can make money by doing this concept and applying that since usually Econ people using this concept. You can get idea of it from the wiki page but since Im in Civil Eng. I will try to explain it in my way.
If you had some structural courses you may heard about virtual work. So in a very simple case lets say what will happen to my structure's forces (for example if we are interested to forces) here to my single bar if I just change the displacement one unit. Economist usually using this concept and they call it shadow price and in many cases it meas if I change the limitation or constraint I have over my resources just one unit, what will happen to my cost or benefit!
Something that we need to be careful about is Lagrangian multiplier's sign. The sign of the multipliers can be positive, negative or in some case we may not have any sign (Lambda = 0) (I used Lambda because usually it used for Lagrange multipliers).
Usually you are trying to maximzie or minimize a objective function, I call it here: f(x,y) 
'f' is my objective function and 'x' and 'y' are variables. I show the constraint function here by: g(x,y). The constraint function as its name shows is limited or constrained by something.
Sometimes for example when we want to go shopping we have limited amount of money and then we are constraint by the amount of money we want to spend, or in many problems we are limited by time, especially when we have exams!!  ;)
g(x,y)<b
('b' here is our constraint.)
other situation is when we are limited at the other end, for example you should be 16 at least to get the driver license, or another example is, you want to do business, you bought something for lets say for 5 box, and you want to make at least 10% benefit, so your price should be greater than $5.5, and if you can sell your thing for more than that you are lucky!
g(x,y)>b
it is worth to mention that here I just have two variables but you may have thousands of them. The concept is the same.
You may have one or more than one constraint function and also you may have more than one objective function that called multi-objective function which we will talk about that later on.

You can get information about your function by the sign of the Lambda.
Lambda is how the objective function changes if I change the limitation (constraint) one unit
Lambda = df/db

In general there are two cases:
1) we are maximizing:
     1-1)  g(x,y)<b
               Lambda is positive
      1-2) g(x,y)>b
               Lambda is negative
2) we are minimizing:
      2-1)  g(x,y)<b
               Lambda is negative
      2-2) g(x,y)>b
               Lambda is positive

so for example when we are maximizing and g(x,y)<b, we are trying to reach the max but the constraint limits us and going uphill our slope will be positive.
If you want to visualize the case 1-2, its like the case 1-1, we want to go uphill again but its like we are going backwards, so if you walk backward and look to your front your slope is negative but the truth is you are going backward toward the summit.
Cases, 2-1 and 2-2 are the same, for example for the case 2-1, we want to minimize and if you walk downhill, when you look at your feet, your slope is negative. The backward walk is again is the same for the case 2-2.