1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Long idum in Modeling Financial Market

  1. Mar 15, 2017 #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.
     
  2. jcsd
  3. Mar 15, 2017 #2

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  4. Mar 15, 2017 #3

    Mark44

    Staff: Mentor

    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.
     
  5. Mar 15, 2017 #4

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    Translated except for comments. There were very well documented FORTRAN versions of random number generators.
     
  6. Mar 20, 2017 #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 (Text):

    #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: Mar 20, 2017
  7. Mar 20, 2017 #6

    Mark44

    Staff: Mentor

    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/
     
  8. Mar 21, 2017 #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.
     
  9. Mar 22, 2017 #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 (Text):


    #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 (Text):


    #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: Mar 22, 2017
  10. Mar 22, 2017 #9

    Mark44

    Staff: Mentor

    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?
     
  11. Mar 22, 2017 #10

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  12. Mar 23, 2017 #11
    [Edit]: Maybe I have finally written a code that is working!

    Code (Text):

    #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: Mar 23, 2017
  13. Mar 23, 2017 #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 :)
     
  14. Mar 23, 2017 #13

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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
     
  15. Mar 23, 2017 #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: Mar 23, 2017
  16. Mar 23, 2017 #15

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  17. Mar 24, 2017 #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 (Text):
     
    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
     
  18. Mar 24, 2017 #17

    FactChecker

    User Avatar
    Science Advisor
    Gold Member

    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.
     
  19. Mar 24, 2017 #18

    Mark44

    Staff: Mentor

    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: Mar 24, 2017
  20. Mar 24, 2017 #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.
     
  21. Mar 25, 2017 #20

    Mark44

    Staff: Mentor

    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.
    Code (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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Long idum in Modeling Financial Market
  1. Binary long division (Replies: 15)

  2. Modulo 2 long division (Replies: 1)

Loading...