Long idum in Modeling Financial Market

  • Thread starter valesdn
  • Start date
  • Tags
    Modeling
In summary: DIM; i++)for (j=0; j<DIM; j++)adj_matrix[i][j]=0;value=rand();if (value<HIGH){adj_matrix[i][j]=1;adj_matrix[j][i]=1;neighbour[i]=j;}}}In summary, the global variable idum is -9132434. It is a seed for the
  • #1
valesdn
42
1
Member advised to use the homework template for posts in the homework sections of PF.
Hi guys.

I am trying to modeling financial markets.
Looking for google, I have found this global variable:

long idum

In the main(), it is defined as

idum = -9132434

or in many other ways.

I am using the following libraries:

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <string.h>

#include "ran2.h"Could you please tell me what this variable is for?

Many thanks.
 
Physics news on Phys.org
  • #2
It's the seed of the random number generator ran2 (see http://dsec.pku.edu.cn/~tlu/na10/ran2.c )
It is a variable where ran2 can save a value after each call which is used in the next call to generate the next pseudo-random number.
If you want more than one random number sequence, you can use a different variable there for each series.
 
  • #3
FactChecker said:
It's the seed of the random number generator ran2 (see http://dsec.pku.edu.cn/~tlu/na10/ran2.c )
The code looks like it was a direct translation from Fortran 77 to C++, with no thought given to using meaningful variable names. idum? I have no idea what idum is supposed to mean. Same goes for IM, IA, AM, IQ, IR.
Long ago, Fortran had limits on the lengths of variable names, but such is not the case any longer.
 
  • Like
Likes FactChecker
  • #4
Mark44 said:
The code looks like it was a direct translation from Fortran 77 to C++, with no thought given to using meaningful variable names. idum? I have no idea what idum is supposed to mean. Same goes for IM, IA, AM, IQ, IR.
Long ago, Fortran had limits on the lengths of variable names, but such is not the case any longer.
Translated except for comments. There were very well documented FORTRAN versions of random number generators.
 
  • #5
Thank you, guys, for your replies.
Unfortunately, ran2.h doesn't work in Xcode, programming in C (it shows an error message: "ran2 file not found", due to using C, instead of using Fortran).
However, I need to write the following matrix in C, thought I used "ran2.h:

Code:
#include <stdio.h>
#include <stdlib.h>
#include "ran2.h"

int main()
{

long idum;
int i,j, DIM;
int matrix[DIM][DIM], neighbor[DIM];

idum=-9132434;

matrix();

return 0;
}

void matrix()
{
double value;
   for(i=0;i<DIM; i++){

        j=50;

        if(i!=j){

             matrix[i][j]=1;

            neighbor[i]=j;
value=ran2(&idum);
        }
    }
}

Any further idea?

In short, I have tried to populate an adjacency matrix, choosing a node and setting it as a fixed node (j=50). For each other node, I should create a link with it.
 
Last edited:
  • #6
Besides ran2 there are several other problems with your code. You are declaring matrix as an array and also as a function, which I'm pretty sure is an error that the compiler will flag. In addition, you have a variable named DIM that is never given a value. You cannot declare either the matrix array or the neighbor array to have a variable number of elements.

Finally, your code includes a file named ran2.h, which either you don't have or it isn't in a place where the compiler can find it. Instead of using the ran2() function, consider using the rand() function that is in the C standard library (and prototyped in stdlib.h. Be sure to seed the random number generator before calling rand(), by calling the srand() function. Here's a link to some docs on the rand() and srand() functions - http://www.cplusplus.com/reference/cstdlib/rand/ and http://www.cplusplus.com/reference/cstdlib/srand/
 
  • #7
Thank you, Mark. Yes, I know that there are several other problems with my code. I will check them asap and I will add your advices on the rand() and stand() functions.
 
  • #8
Hello Mark, I tried to re-write my C code. It doesn't work anyway, so I think there is something wrong. I have unexpectedly no error messages. Could you please check it and tell me where errors are? Many thanks. My goal is to build an undirected graph.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>#define DIM 15

#define HIGH 1 /* connectivity */int main()

{

  void  random_matrix();
    return 0;

}
void random_matrix()

{

    int i, j, adj_matrix[DIM][DIM];

    int neighbour[DIM];

    double value;

 

 

    for(i=0; i<DIM; i++)

        for(j=0; j<DIM; j++)

            adj_matrix[i][j]=0;

    for (i=1; i<DIM; i++) adj_matrix[i][i]=1;

    for (j=1; j<DIM; j++)

        if(i!=j){

            srand((unsigned int)time(NULL));

            value=rand();

            if (value<HIGH){

                adj_matrix[i][j]=1;

                adj_matrix[j][i]=1;

                neighbour[i]=j;

                neighbour[j]=i;

            }

        }

}
I have no problem with a basic code for a matrix:

Code:
#include<stdio.h>
int main()
{
int i, j, matrix[10][10] ;

printf("Enter your matrix\n");
for(i=0; i<10; i++)
  for(j=0; j<10; j++)
  {
   scanf("%d",&matrix[i][j]);
  }
printf("\n Matrix: \n"); 
for(i=0; i<10; i++) 
{
   for(j=0 ;j<10; j++)
   {
     printf("%d ",matrix[i][j]);
   }
  }
}

However the snags grow up when I use a function in the code :(
 
Last edited:
  • #9
valesdn said:
Hello Mark, I tried to re-write my C code. It doesn't work anyway, so I think there is something wrong. I have unexpectedly no error messages. Could you please check it and tell me where errors are? Many thanks. My goal is to build an undirected graph.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define DIM 15

#define HIGH 1 /* connectivity */

int main()
{
  void  random_matrix();
    return 0;
}

void random_matrix()
{
    int i, j, adj_matrix[DIM][DIM];
    int neighbour[DIM];
    double value; 

    for(i=0; i<DIM; i++)
        for(j=0; j<DIM; j++)
            adj_matrix[i][j]=0;
   for (i=1; i<DIM; i++) adj_matrix[i][i]=1;
    for (j=1; j<DIM; j++)
        if(i!=j){
            srand((unsigned int)time(NULL));
            value=rand();
            if (value<HIGH){
                adj_matrix[i][j]=1;
                adj_matrix[j][i]=1;
                neighbour[i]=j;
                neighbour[j]=i;
            }
        }
}
<snip>

However the snags grow up when I use a function in the code :(
The problem is that matrix is declared as a local variable in your random_matrix() function, so its scope is only within that function. As a local variable (i.e., allocated on the stack), matrix comes to life with the random_matrix() function is entered, values are put into it, but then when random_matrix() returns to main, matrix goes out of scope, and is not accessible any more.

If you need to use a function to put values in the matrix, a variable for the matrix needs to appear as a parameter for your function, and the matrix itself needs to be declared elsewhere, such as in main. How much do you know about parameters to functions, particularly about array-type parameters?
 
  • #10
For small programs like this, you can move the matrix declaration to the top of the file (before any '{' for a function definition) and it will be accessible to all code within the file. Then you don't have to mess with a parameter or pointer.
 
  • #11
[Edit]: Maybe I have finally written a code that is working!

Code:
#include <stdio.h>

#include <stdlib.h>/* Adjacency matrix */int V, E, visited[15], graph[15][15];

void dfs(int i)

{

    int j;

    visited[i]=1;

    printf("%d",i+1);

    for(j=0;j<V;j++)

    {

        if(graph[i][j]==1&&visited[j]==0)

            dfs(j);

    }

}int main()

{

    int i, j, vertex1, vertex2;

    printf("Directed graph\n");

    printf("Please, enter the number of edges:");

    scanf("%d", &E);

 

    printf("Please, enter the number of vertices:");

    scanf("%d", &V);

    for(i=0;i<V;i++)

    {

        for(j=0;j<V;j++)

            graph[i][j]=0;

    }

 

    /* Creating edges */

 

    for(i=0;i<E;i++)

    {

        printf("Please, enter the edges (format: vertex1 vertex2):");

        scanf("%d %d", &vertex1,&vertex2);

        graph[vertex1-1][vertex2-1]=1;

    }

    for(i=0;i<V;i++)

    {

        for(j=0;j<V;j++)

            printf("%d", graph[i][j]);

        printf("\n");

    }
}

Unfortunately, I can't enter random numbers with the srand() function.
Now I would like to compute the in-degree and the out-degree respectively of my graph.
Could you help me with the algorithm? Thank you.
 
Last edited:
  • #12
What I need is a vertex which randomly select a new possible neighbour. I would like to calculate the probability of switching from the old neighbour, k, to the new selected one. Any advices would be greatly appreciated :)
 
  • #13
valesdn said:
Unfortunately, I can't enter random numbers with the srand() function.
You do not enter random numbers with srand. You call it once before the loop that contains rand() to give it an integer seed to start the sequence of random numbers. If you don't call it, srand(1) is the default. Using that will give you the same random sequence for every run. To get a run with a different set of random numbers, give it a different odd positive integer seed. Some people use srand((unsigned) time(&t)) to get a different random sequence every time, but then you can not repeat the random series unless you print or save the value of (unsigned) time(&t) for repeat runs. (see https://www.tutorialspoint.com/c_standard_library/c_function_srand.htm )

Calls to rand() will give you random integers from 0 to RAND_MAX. The link above has an example that generates random integers from 0 to 49. A common use is to get uniform random real values from 0 to 1 with r = (double)rand()/RAND_MAX
 
  • #14
Thank you for your prompt reply, FactChecker. I read the attached link. However, I don't understand where I should put rand() in my code, considering that I need to select a vertex which randomly selects a new possible neighbour. And finally, I should calculate the probability of switching from the old neighbour, k, to the new selected one... It is easier said than done!

Thinking about that: Maybe I should fix a vertex (for example i) and set it equal to rand()%V (where V could be the number of vertices or another condition) and write a "for cycle" and an "if one". But I don't know which should be the correct conditions.
 
Last edited:
  • #15
I suggest that you first make pseudo-code or a flow chart of the logic that you want to implement. That is an engineering design step, not a programming step. Then you should try to implement that logic in some code. Each part of your code should have comments that clearly indicate what parts of the logic that section is trying to implement and how the code variables match the logic. Until you do that, it is difficult for anyone to help you on a homework problem.
 
  • Like
Likes hocphp
  • #16
Thank you, FactChecker.
I know: my code is bad written, but it is working, anyway.
However, I am having some trouble selecting a vertex of my graph in order to randomly select a neighbour.
I tried to write this function:

Code:
void random_matrix()

{

    double value;

    int i, j;

    float prob;

   

    for(i=0;i<15;i++){ // populate the adjacency matrix

        j=1; // Fixed vertex

        if(i!=j){

            graph[i][j]=1; //For each vertex that is not the fixed one, I create a link with it

            neighbour[i]=j;

            value=rand()%15; //Each vertex extracts a random value

            if(value>=prob){ //The vertex i has a link with the fixed vertex

                graph[i][j]=0;

                j=(rand()%15)*15;

                graph[i][j]=1;

                neighbour[i]=j;

            }

           

        }

    }

   

}

Could you check it, please? Thank you
 
  • #17
I don't want to guess about what the code is supposed to do. To me, a random adjacency matrix is just a square matrix with the elements of random 0's and 1's. I don't know what a "fixed vertex" is, or what properties the matrix should have. I doubt that this code does what you want it to do. For one thing, j=(rand()%15)*15; gets larger than the second dimension of graph[15][15]. It can get as large as 14*15=210.
 
  • #18
valesdn said:
Thank you, FactChecker.
I know: my code is bad written, but it is working, anyway.
However, I am having some trouble selecting a vertex of my graph in order to randomly select a neighbour.
I tried to write this function:
Mod edit: Changed [code] to [code=c] and removed many blank lines.
C:
void random_matrix()
{
    double value;
    int i, j;
    float prob;

    for(i=0;i<15;i++){ // populate the adjacency matrix
        j=1; // Fixed vertex
        if(i!=j){
            graph[i][j]=1; //For each vertex that is not the fixed one, I create a link with it
            neighbour[i]=j;
            value=rand()%15; //Each vertex extracts a random value
            if(value>=prob){ //The vertex i has a link with the fixed vertex
                graph[i][j]=0;
                j=(rand()%15)*15;
                graph[i][j]=1;
                neighbour[i]=j;
            }
        }
    }
}
Could you check it, please? Thank you
In the inner if statement you're checking to see if value >= prob. This won't work, as prob is an uninitialized variable.

Maybe if you could explain what you're trying to do with a simple example, and posting a hand-drawn graph that represents the situation, we could help you more. As it is, we don't know what you're trying to do. Comments such as "Each vertex extracts a value" might seem useful to you, but they don't mean much to me or the others here, based on the replies I'm seeing.
 
Last edited:
  • Like
Likes FactChecker
  • #19
Thanks for your reply. I need to create a directed graph where I fix a vertex and then I randomly select a new neighbour that it will be linked with the 'fixed vertex'. And so on, in order to create a connected graph.
That's all ;)
I tried to create a directed graph and my code currently is working. However, I can't write the part of the code where I should select one vertex and randomly select a new neighbour. I can't explain in other ways. I have a lot of troubles with rand(), because I don't know how it works.
 
  • #20
rand() returns a number in the range 0 through RAND_MAX, which can be different on different compilers, but is guaranteed to be at least 32,767.
C:
j=(rand()%15)*15;
In your code, copied in the line above, rand() returns an integer value between 0 and RAND_MAX.
rand() % 15 results in the remainder modulo 15, which will be an integer in the range [0, ... , 14]. Finally, your code multiplies that number by 15, which results in a value for j that will almost always be out of range for your matrix.
 
  • #21
Thank you, Mark, for your help.
So what can I do if I want to select randomly a new neighbour or vertex (after I have fixed another to attach it)?
What should the condition (if/for) be?
 
  • #22
valesdn said:
Thank you, Mark, for your help.
So what can I do if I want to select randomly a new neighbour or vertex (after I have fixed another to attach it)?
What should the condition (if/for) be?
Presumably you want a different neighbor, since you probably don't want to consider that a vertex is a neighbor to itself. You want to randomly select an index j, with j not equal to i. If so, this code should do the trick:
Code:
while (j != i)
{
    j = rand() % 15;
}
As I mentioned before rand() % 15 evaluates to a number in the range [0, ..., 14]. The while loop executes as many times as necessary to get a value for j that is different from the value for i.
 
  • #23
Thank you so much, Mark.

Actually I have this part of code that I want to "translate" in C:

Code:
for(i = 0; i < DIM; i++){

            if(ran2(&idum) <CONNECTIVITY){

                j = ran2(&idum)*DIM;

                if(i != j ){

                    matrix[i][j] = 1;

                    matrix[j][i] = 1;

                    neighbor[i] = j;

                    beighbor[j] =
}
}
}

Where DIM is my number of rows (15).
How can I "translate" the function ran2(&idum) in a way that works in C? Could what you said before suit fine for this part of code?
 
  • #24
valesdn said:
Thank you so much, Mark.

Actually I have this part of code that I want to "translate" in C:

Code:
for(i = 0; i < DIM; i++){

            if(ran2(&idum) <CONNECTIVITY){

                j = ran2(&idum)*DIM;

                if(i != j ){

                    matrix[i][j] = 1;

                    matrix[j][i] = 1;

                    neighbor[i] = j;

                    beighbor[j] =
}
}
}

Where DIM is my number of rows (15).
How can I "translate" the function ran2(&idum) in a way that works in C? Could what you said before suit fine for this part of code?
ran2() is apparently a random number generator listed in "Numerical Recipes in C." There are also versions in this series for Fortran, Pascal, Matlab, and a few other languages. From my copy of Numerical Recipes, the ran2() function returns a number between 0.0 and 1.0.

I think that the code below will be equivalent to what you're trying to do. Be sure to call srand() to seed the random number generator before the for loop executes.

C:
for( i = 0; i < DIM; i++)
{
   if (rand()/RAND_MAX < CONNECTIVITY)
   {
      j = rand() % DIM;    // j will be set to an int in the range of 0 ... 14
      if (i != j)
      {
         matrix[i][j] = 1;
         matrix[j][i] = 1;
         neighbor[i] = j;
         neighbor[j] = i;
      }
   }
}
In the first if statement, rand()/RAND_MAX will almost always be 0. If you ran this code hundreds of thousands of times, rand() might eventually return 32, 767 (or RAND_MAX), in which case rand()/RAND_MAX would be equal to 1.
 
  • #25
Thank you so much, Mark44, for helping me.
 
  • Like
Likes Mark44
  • #26
You're welcome!
 

FAQ: Long idum in Modeling Financial Market

What is long idum in modeling financial market?

Long idum is a term used to describe a long-term trend in financial markets. It refers to the idea that over a long period of time, stock prices tend to increase due to overall economic growth and productivity.

How is long idum calculated?

Long idum is not a specific calculation, but rather a concept that is observed over time. It is based on the idea that the stock market will generally increase in value over a long period of time due to economic growth and productivity.

What factors influence long idum?

Long idum is influenced by a variety of factors, including economic growth, productivity, interest rates, and overall market sentiment. It is also impacted by political and global events, as well as company-specific factors such as earnings and performance.

How does long idum affect investment decisions?

Long idum is a key consideration for long-term investors, as it suggests that over time, the stock market will generally increase in value. This can influence investment decisions to focus on long-term growth rather than short-term fluctuations.

Are there any risks associated with long idum?

While long idum can be a helpful concept for long-term investors, it is important to remember that the stock market can also experience periods of decline or volatility. It is important to diversify investments and consider potential risks when making investment decisions.

Similar threads

Replies
5
Views
2K
Replies
3
Views
1K
Replies
1
Views
2K
Replies
2
Views
2K
Replies
2
Views
9K
Back
Top